Question
All the inputs in this code need to accept only positive integers. Right now that pretty much works, but inputs like 1 100 are still
All the inputs in this code need to accept only positive integers. Right now that pretty much works, but inputs like "1 100" are still accepted. I don't want that. How can I make it so inputs that have spaces in them don't get accepted? Thanks in advance.
#include #include #include #include using namespace std;
struct Person { string name; string address; }; struct Account { int accountNumber; Person ownerInfo; double balance; };
int main() { Account structure1; int menuChoice; int maxAccounts;
cout << "Enter max number of accounts: "; cin >> maxAccounts;
int i=1; while(maxAccounts < 0 || cin.fail()) { cout << "Invalid value, please reenter: "; cin.clear(); cin.ignore(32767, ' '); cin >> maxAccounts;
if(maxAccounts > 0) { break; }
if(i == 4) { cout << "Too many unsuccessful attempts, exiting" << endl; break; } i++; } cout << endl; cout << "Menu:" << endl; cout << "1->Enter data for specific account, 2->Display data for specific account" << endl; cout << "3->Display data for all accounts , 4->Quit:" << endl; cin >> menuChoice; int j=1; while(menuChoice <= 0 || cin.fail() || menuChoice > 4) { cout << "Invalid choice, please reenter: "; cin.clear(); cin.ignore(32767, ' '); cin >> menuChoice;
/*/if(menuChoice > 0) { break; } */
if(j == 4) { cout << "Too many unsuccessful attempts, exiting"; break; } j++; } if(j != 4) { if(menuChoice == 1) { cout << "Enter account number: "; cin >> structure1.accountNumber; int k=1; while(structure1.accountNumber <= 0 || cin.fail() || structure1.accountNumber > 4) { cout << "Invalid value, please reenter: "; cin.clear(); cin.ignore(32767, ' '); cin >> structure1.accountNumber;
/*/if(menuChoice > 0) { break; } */
if(k == 4) { cout << "Too many unsuccessful attempts, exiting"; break; } k++; } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started