I can't believe how long I've spent on this... Problem asks for user to input a 6 digit account # and only a 6 digit account #. I assume my teacher is going to attempt to put in more than 6 digits.
I've tried to prevent that by telling the user to only input one digit at a time, and verifying/forcing it to be a digit. But the user can still input more than one digit at a time. And if the user ends up inserting more than 7 digits, then the program automatically has an input stored for the next cin.
Any suggestions on how I can force the user to only input one character at a time, or a total of 6 characters? Or just a way to erase whitespace garbage that the user inputs?
My brain is pretty fried on this one. Also, we havent learned strings yet, so I don't think I am allowed to include the cstring library.
do {
char acct[7];
cout<<"Enter your 6 Digit Account #: "<<endl;
//Originally I had this as (i=1;i<7;i++) and I would always
//get a 'P' appearing in char acct. Why is that??????
for (int i=0;i<6;i++) {
//Prompts user for which digit it wants.
cout<<"Enter Digit "<<i+1<<":"<<endl;
cin>>acct[i];
if(acct[i]<'0'||acct[i]>'9'){
cout<<"Error"<<endl;
i--;
}
}
cout<<"Account #: "<<acct<<endl;
cout<<endl<<endl;
cout<<"Would you like to run again? N for No."<<endl;
cout<<"Press any other key to run again."<<endl;
cin>>exit;
} while ((exit!='n')&&(exit!='N'));
cout<<endl;
cout<<endl;
cout<<"End problem"<<endl;break;
You should discard characters instead after you get all the digits. #include <limits> and add this after reading all the input : cin.ignore( numeric_limits<streamsize>::max(), '\n' );
If you do not want to prompt for every digit, u need to use cin.get() and assign the returned value to acct[i] like ( acct[i] = cin.get() ) then check if it
is a digit.