Overlooking a Simple problem, suggestions?

Im working through a book i bought learn C++ how to program 2003, and it seems most of the examples compile with zero erros or warnings, yet they dont flow correct. Iv even tried compiling the include .cpp files on the cd and they still have the same error. Can anyone spot the problem, would be nice to figure out what hes doing wrong in this loop because he uses the same loop setup in the next 2 chapters. I have a feeling its to do with char response[256], it doesnt seem to let it recieve any input , and then jumping into the if (strlen(response) etc return 1;, any help please. I also note that it would be better to use functions or atleast a switch statement, but im working through a book.

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>
using namespace std;

int main(){

	float balance = 0,
		  newBalance = 0,
		  adjustment = 0;

	char response[256];
	string moreBanking;

	cout << "Do you want to do some banking?: ";
	cin >> moreBanking;
	for (int i = 0; i < moreBanking.length(); i++){
		moreBanking[i] = toupper(moreBanking[i]);
	}
	while (moreBanking == "YES"){ 
		cout << "Please select an Option (1=DEPOSIT,2=WIDTHDRAWL,3=BALANCE): ";
		cin.getline(response, 256); // what is going wrong here

		if (strlen(response) == 0){
			cout << response << " - is an invalid option" << endl;
			return 1;
		}
		else if ((atoi(response) < 1) || (atoi(response) > 3)){
				cout << response << " - is an invalid option" << endl;
				return 1;
		}
		if (atoi(response) == 1){
			cout << "Enter the desposit ammount: ";
			cin >> adjustment;
			newBalance = balance + adjustment;
			cout << endl << endl;
			cout << "* * * * BANK OF TRUSTEE * * * *" << endl;
			cout << "Previous Balance: " << balance << endl;
			cout << "Deposit Ammount : " << adjustment << endl;
			cout << "Current Balance : " << newBalance << endl;
			cout << endl << endl;
		}
		if (atoi(response) == 2){
			cout << "Enter Ammount to Widthdrawl: ";
			cin >> adjustment;
			newBalance = balance - adjustment;
			cout << endl << endl;
			cout << "* * * * BANK OF TRUSTEE * * * *" << endl;
			cout << "Previous Balance   : " << balance << endl;
			cout << "Widthdrawl Ammount : " << adjustment << endl;
			cout << "Current Balance    : " << newBalance << endl;
			cout << endl << endl;
		}
		if (atoi(response) == 3){
			cout << endl << endl;
			cout << "* * * * BANK OF TRUSTEE * * * *" << endl;
			cout << "Current Balance : " << newBalance << endl;
			cout << endl << endl;
		}
		balance = newBalance;
		cout << "Do you want to do some banking?: ";
		cin >> moreBanking;
		for (int i = 0; i < moreBanking.length(); i++){
			moreBanking[i] = toupper(moreBanking[i]);
		}
	}
	cout << "Thanks for banking with Trustee!" << endl;
	return 0;
}
Last edited on
On line 27, the | should be ||, for one...
Yeah, still doesn't fix the problem, thanks for your input though.
If you add: cin.ignore(); between lines 18-19, and again at 64-65, it will solve the problem.

cin >> moreBanking; is leaving a \n character in the stream and when getline is called, it is finding the \n and responding as if you hit enter without typing anything else.

~psault
Thanks I see, but where is getline getting the \n from?
Last edited on
The input buffer.
When you use cin it ignores white space (\n, ' ', TAB) until it gets to some data in the input buffer. It then extracts that data until it reaches another white space character. That last white space character is left in the buffer. The next time you use cin, the process it repeated (that white space that was left behind is at first ignored.) But when you use getline, it works differently. It collects all data until it reaches the delimiter character (which defaults to '\n') which it also extracts, but does not include in the variable it is working on.

So in this case, cin left the '\n' behind and it cut your getline short.

~psault
Using cin to get user input: http://www.cplusplus.com/forum/articles/6046/
Topic archived. No new replies allowed.