5

It might not be a bug, but I don't know what is going wrong. My first entry is repeated for str1 on 2nd iteration, and is same way from then. Only first iteration goes good.

#include <iostream>
#include <string>
using namespace std;

int main () {

cout << " \n Enter two words. \n " ;
char c = 'y';
string str;
string str1;
while (c == 'y'){

    getline(cin,str);

    getline (cin,str1);

    cout << " \n\n str : " << str << " str1 : " << str1 ;
    cout << " \n Continue ? \n " ;
    cin >> c;
}

return 0;
}

The output is :

 Enter two words.
 hello world
this is mr


 str : hello world str1 : this is mr
 Continue ?
 y
hello world


 str :  str1 : hello world
 Continue ?
 n


Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
ani
  • 109
  • 2
  • 11
  • 2
    After you press `y` , you probably hit Enter. You should consider which part of the program that reads the newline made when you hit that Enter. – nos Jun 21 '12 at 18:55
  • @chris cin is an object, maybe you mean operator>> – Jonathan Wakely Jun 21 '12 at 19:03
  • Welcome to SO! Thank you for providing a complete short program that demonstrates your problem. That was *very* helpful on your part. Please see http://sscce.org for more information. – Robᵩ Jun 21 '12 at 19:09
  • @Robᵩ I have been skimming Stackoverflow, for quite sometime now, (This is my first Q). It is always helpful, and it was a tricky error I made, snippet probably saved hours of head bashing and ramming the Enter key... – ani Jun 22 '12 at 07:27

2 Answers2

3

Add

cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

after your

cin >> c;

Consider the following input:

    dog
    cat
    y
    owl
    fish
    n

If we examine the characters that are present in the input stream individually, we'll see:

    d o g \n c a t \n y \n o w l \n f i s h \n n \n

The first call to getline consumes dog\n; the second consumes cat\n, leaving this:

    y \n o w l \n f i s h \n n \n

The first call to cin >> c consumes only y but not the subsequent newline, leaving this:

    \n o w l \n f i s h \n n \n

Now, the fun begins: What happens during the next call to getline? Why it reads up to the next newline, of course. So the next call to getline returns an empty line, and leaves owl... in the input stream.

The solution, as I outlined above, is to consume the remainder of the (now useless) input line.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Thanks, I was thinking the same thing. You confirmed it for me. As for the solution, I am clearing the cin buffer, by using ignore(). Still it didn't work. Coz, I was putting it in the wrong position, i.e. before cin >> c. (It is a part of the project, so there is a cin >> year before I accept the choice.) – ani Jun 22 '12 at 07:24
1

As rob says.

But an alternative fix that looks nicer:

// change
char c = 'y';
....
while (c == 'y'){
....
    cin >> c;

// Into
std::string c = "y";
....
while (c == "y"){
....
    std::getline(cin, c);

When dealing with manual user input you should be careful of using the >> operator as this will always leave the '\n' on the input. Which means you can either use a method that retrieves the '\n' character (getline()) or you can manually remove it afterwords (ignore()).

Martin York
  • 257,169
  • 86
  • 333
  • 562
  • Thanks, Look at this snippet I found the right position to ignore() the cin. :-) – ani Jun 22 '12 at 07:25