command "cin.getline()" is ignored in some cases

Hi, I've just started programming in C++.
I learned how to use the command cin.getline(), and mostly it works perfectly like in this case:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
using namespace std;

int main()
{
    char waarde[256];
    cout << "Geef een waarde in:";    
    cin.getline(waarde,256);
    cout << waarde;
    return 0;
}


But not in this case, although I just copied the example, written in the textbook. Look for the text, written in bold. When the question "What would you like to do?" is asked, the command "cin.getline()" is ignored, so the char array "response" (in wich the response has to be stored) remains empty and the program ends (due to the return 1 in the else-statement).

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
#include <iostream>
#include <string>
using namespace std;

int main()
{
float balance=0;
float newBalance=0;
float adjustement=0;
string moreBankingBusiness;
cout << "Do you want to do some banking?(beta) ";
cin >> moreBankingBusiness;
for(int i=0;i<moreBankingBusiness.length();i++)
       {moreBankingBusiness[i]=toupper(moreBankingBusiness[i]);
        }
while (moreBankingBusiness == "YES")
       {char response[256];
        cout << "What would you like to do?";
        cin.getline(response,256);
        if(strlen(response)==0){cout << "You must make a selection";
                                   return 1;
                                   }
        else if(atoi(response)<1 | atoi(response)>3)
                     {cout << response << " - is not a valid banking function";
                      return 1;
                      }
        if(atoi(response)==1)
          {cout << "Enter the deposit amount: ";
           cin >> adjustement;
           newBalance=balance+adjustement;
           cout << endl << endl << "*** SMILEY NATIONAL BANK ***" << endl << endl;
           cout << "Old balance is: " << balance << endl;
           cout << "Adjustement is: +" << adjustement << endl;
           cout << "New balance is: " << newBalance << endl << endl;
           }
         if(atoi(response)==2)
           {cout << "Enter the withdrawal amount: ";
            cin >> adjustement;
            newBalance=balance - adjustement;
            cout << endl << endl << "*** SMILEY NATIONAL BANK ***" << endl << 
endl;
            cout << "Old balance is: " << balance << endl;
            cout << "Adjustement is: -" << adjustement << endl;
            cout << "New balance is: " << newBalance << endl << endl;
           }
         if(atoi(response)==3)
           {cout << endl << "*** SMILEY NATIONAL BANK ***" << endl << endl;
            cout << "Your current balance is: " << newBalance << endl << endl;
            } 
         balance=newBalance;
         cout << "Do you have more banking business? ";
         cin >> moreBankingBusiness;
         for(int i=0;i<moreBankingBusiness.length();i++)
                   {moreBankingBusiness[i]=toupper(moreBankingBusiness[i]);
                   }
          }
    cout << endl << endl << "Thanks for banking with me!";
    return 0;
}


I hope you can help me,
Nicolas
Thanks for the advice to use the getline() function! Its much more easy to handle.

However the answer doesn't explain why exactly "cin.getline" is ignored in my code...
cin >> moreBankingBusiness;
That's not removing the newline from the stream. Thus causing your getline() to immediately take an empty stream with just a newline char.

Which is why I posted that link. Don't use >>
Last edited on
Owkay, I think I'm just getting your point. I also found something useful to deal with the newline character:

cin.ignore(); //ignores the first character it encounters (i.e. the newline char)
cin.getline(response,256);
Read:
http://www.cplusplus.com/forum/articles/6046/

It doesn't use >> and it shows you how to use strings instead of character arrays. Better than both of your approaches. Also shows you how to convert types.
Topic archived. No new replies allowed.