PDA

View Full Version : C++ console programming using cbuilder4



fryup
04-12-2005, 12:27 PM
I have a problem with C++ console programming using cbuilde4.

I input data through overloaded operators of istream and write to a file through ofstream. Its working alright without validations.
I have a problem of how to validate inputs of numeric fields and get back to the istream and write to the file.

See below a part of code where I have tried this and relavent coding.

If any body can help on this thats great.

Thanks in advance.


istream& operator>> (istream& instr, const Details& details){
details.inputData(instr);
}

stream& Details::inputData(istream& instr){
int keyval=0;

cout << "ID Number : " << idNumber<<endl;
cout << "Name : ";
instr.ignore();
instr.getline(name,19);
cout << "Address1 : ";
instr.getline(address1,19);
cout << "Address2 : ";
instr.getline(address2,19);
char *inputString;
cout << "Postal Code : ";

//validate numeric field postalCode ***************?????

for (int i=0; i<8;i++){
keyval = getch();
if (keyval==13){ i=8;}
cout<<(char)keyval;

if(isdigit(keyval))
{
inputString[i]==(char)keyval;
}
}

*inputString >> postalCode ; //????problem is here

cout <<endl<< "City : ";
instr.ignore();
instr.getline(city,19);
cout << "Fax No : ";
instr >> fax ;
instr.ignore();

return instr;
}

D. McG
04-12-2005, 10:06 PM
Hi,

Your problems could be coming from the way the 'inputString' is read in. In the code you have shown, 'inputString' is declared as a pointer to a char [array], but the memory isn't allocated. First of all, try changing the declaration to:

char inputString[256]; //A macro or constant variable may substitute the 256

Also, when the Enter key is detected, it would be better to write a null value to the array to terminate the string. Using 'break;' instead of 'i=8;' is also cleaner and will improve readability.

If you wanted, you could also add handling code for the backspace key (\x08), though you could do lots to make your input routines absolutely bulletproof (something along the lines of 'expecting the unexpected').

Hope this helps somewhat.

-D. McG