Input errors?

Hello, I'm trying to see what if the user enters a letters instead of entering numbers, my code goes into a full on looop never stopping. Can someone help?

Basically, I want it to go back to asking, for the deposit amount to be entered again if the user enters a letter or string.

I tried making it a do while and it wasnt working.

Please note that i is either greater than 0 or -1 which comes from my search function.



while (i != -1 && i > 0)
	{
		cout <<endl <<left <<setw(5) <<"" << "Customer Information found\n" <<endl;
		cout <<left <<setw(5) <<"" << "name" << setw(24) <<right <<"" <<"Accunt Number" << setw(11) <<"" <<"balance" <<endl <<endl;
		cout <<setw(5) <<"" <<setw(28) <<left <<fixed << bank[i].fullname;
		cout <<setw(5) <<right << bank[i].acct<<setw(15) <<"$" <<setw(8) <<setprecision(2)<< bank[i].bal <<endl <<endl;
		cout <<endl <<left <<setw(5) <<"" << "Enter amount to deposit: $" ;
		cin >> amount;
		cin.ignore(); 

		if (amount > 0)
		{
			bank[i].bal = (bank[i].bal + amount);
			cout <<endl <<left <<setw(5) <<"" << "New Balance: $" << bank[i].bal <<endl;
			updateInput(bank, total, fileName); // updates the customer file that was loaded in
			backMenu(bank, size, total, target2, target, fileName); // back to main menu
		}

		else if(cin.fail())
		{	
			cout <<endl <<left <<setw(5) <<"" << "Try again, Must be an integer number"<<endl;
			cin.clear();
			//backMenu(bank, size, total, target2, target, fileName); // back to main menu
		}

		else
		{	
			cout <<endl <<setw(5) <<"" <<"Minimum Deposit has to be greater than $0\n"<<endl;
			backMenu(bank, size, total, target2, target, fileName); // back to main menu
		}
	}
i is the array index and is NEVER negative, which contradicts your loop condition.
What do you mean? I thought it would be irrelevant to what I want done. All I want is an error check for invalid entry, like text instead of numbers. The value goes into amount only and i will not change.


This is how I did my do while statement to check for error and I believe it wasn't working. Please help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
do{
cout <<"how much to deposit? ";
cin >> amount;
cin.ignore(10, '\n');

if (amount > 0)

bank[i].bal + amount;

else if (!cin)
cout <<"try again!" <<endl;

else 
cout <<"minimum deposit must be > 0" <<endl;
} while (!cin || amount <= 0);

Last edited on
I meant it was while (i != -1 && i > 0) that caused your infinite loop cuz i is supposed to be non-negative always.
Do you think it would be better if I just create an if statement instead of the while?

The i just let's me know where the location of a certain person so that I can deposit to the amount.

I can probably just plug in my do while just for inputting the amount.

Cause Im basically just don't want the user to accidentally enter a char or string.

Does my do while statement look correct?


Thank you for your quick reply btw =)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

i=searchfunction(bank, total);

if (i != -1 && i > 0)
	{
		cout <<endl <<left <<setw(5) <<"" << "Customer Information found\n" <<endl;
		cout <<left <<setw(5) <<"" << "name" << setw(24) <<right <<"" <<"Accunt Number" << setw(11) <<"" <<"balance" <<endl <<endl;
		cout <<setw(5) <<"" <<setw(28) <<left <<fixed << bank[i].fullname;
		cout <<setw(5) <<right << bank[i].acct<<setw(15) <<"$" <<setw(8) <<setprecision(2)<< bank[i].bal <<endl <<endl;    
		
                do{ 
                cout <<endl <<left <<setw(5) <<"" << "Enter amount to deposit: $" ;
		cin >> amount;
		cin.ignore(); 

		if (amount > 0)
		{
			bank[i].bal = (bank[i].bal + amount);
			cout <<endl <<left <<setw(5) <<"" << "New Balance: $" << bank[i].bal <<endl;
			updateInput(bank, total, fileName); // updates the customer file that was loaded in
			backMenu(bank, size, total, target2, target, fileName); // back to main menu
		}

		else if(!cin)
		{	
			cout <<endl <<left <<setw(5) <<"" << "Try again, Must be an integer number"<<endl;
			cin.clear();
			//backMenu(bank, size, total, target2, target, fileName); // back to main menu
		}

		else
		{	
			cout <<endl <<setw(5) <<"" <<"Minimum Deposit has to be greater than $0\n"<<endl;
			//backMenu(bank, size, total, target2, target, fileName); // back to main menu
		}

       }while(!cin || amount <= 0);

else
{ cout <<"customer not found" <<endl;
 backMenu(bank, size, total, target2, target, fileName); // back to main menu
}
Last edited on
Just tried the code above and it's still giving me infinite loop when I enter a letter or string.

Anyone want to help or know what to do?
Topic archived. No new replies allowed.