Okay, I have a program that moves across a bunch of records stored into memory and reports information about each (number, adress, some initial values).
Code:
#include <iostream>
#include <conio.h>
#include <fstream>
#include <math.h>
#include <iomanip>
using namespace std;
ifstream::pos_type size;
char *cache;
unsigned char unsign(char in)
{
if (in>=0) return in;
else return in+256;
}
int readshint(unsigned __int64 o=0)
{
unsigned int out=0;
for (int i=0; i<=1; i++)
{ out+=int(unsign(cache[i+o])*pow(256, i));}
return out;
}
void report (unsigned long int record, unsigned long int offset)
{
cout << "\nRecord " << setfill('0') << setw(2) << dec << record
<< " at " << setw(4) << hex << offset << "h : "
<< dec << readshint(offset) << " x " << readshint(offset+2)
<< " pixels.\n";
}
unsigned char countrecords (unsigned short int offset=0)
{
if (readshint(offset+4)==0)
{ return 1; }
else
{ return countrecords(offset+readshint(offset+4))+1; }
}
unsigned short int taketarget (unsigned short int records)
{
int target=0;
cout << "\nEnter target record number:\n";
while (target<1 || target>records)
{ cin >> target;
/*if (target<1 || target>records)
{ cout << "Enter a number between 1 and " << records << ".\n"; }*/
}
return target;
}
void save (unsigned short int offset)
{
cout << "\nNOT IMPLEMENTED YET!!!\n";
return;
}
main () {
ifstream infile ("in.chp", ios::in|ios::binary|ios::ate);
if (infile.is_open())
{
size = infile.tellg();
cache = new unsigned (unsign(char [size]));
infile.seekg (0, ios::beg);
infile.read (cache, size);
infile.close();
cout << "CHP read to memory.\n";
unsigned short int records=countrecords(), offset=0, target=1;
cout << dec << records << " records found in file.\n";
char trigger;
do {
/*cout << "Enter target record number...\n";
do {
cin >> target;
if (target>records || target==0)
cout << "Enter a value between 1 and " << records << ".\n";
} while (target>records || target<1);*/
for (int recind=1; recind<target; recind++)
{ offset+=readshint(offset+4); }
report(target, offset);
cout << "[N]ext, [P]revious, [i]ndex, [s]ave, [E]xit?\n";
do {
trigger=tolower(getch());
} while (trigger!='n' && trigger!='p' && trigger!='i' && trigger!='s' && trigger!='e');
switch (tolower(trigger))
{
case 'n': if (target<records-1) target++; else target=records; goto end;
case 'p': if (target>1) target--; goto end;
case 'i': target=taketarget(records); goto end;
case 's': save(offset); goto end;
end:;
}
} while (tolower(trigger)!='e');
delete[] cache;
}
else cout << "Unable to open input file\n";
cout << "\n";
system("PAUSE");
}
Full sourcecode
It (mostly) works, but the following function doesn't work as intended.
Code:
unsigned short int taketarget (unsigned short int records)
{
int target=0;
cout << "\nEnter target record number:\n";
while (target<1 || target>records)
{ cin >> target;
if (target<1 || target>records)
{ cout << "Enter a number between 1 and " << records << ".\n"; }
}
return target;
}
It's supposed to keep prompting the user until valid input is entered.
It does just fine if the user enters numeric values - but if a character is entered, the function loops infinitely without giving the user any way to enter new input.
What may be the cause of this behavior?
__________________ "God. Can't you people see I'm trying to commit a crime against science and nature here?"
-- Reed Richards