Getline

Hello,

I'm having some problems with getline function. I'm using it because I'm trying to save 2 words in my string. Now the problem is, it doesn't work. getline is located in a cycle, and if I try to relocate it out of cycle it works perfectly fine.

Here's my code of the program with the problem (it's one of the 'Begginer's exercises'):

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

int main()
{
    int n, z, c;
    string guess;    
    cout<<"Enter a number\n";
    cin>>z;
    cout<<"\n";
    z=100;
    while (1)
    {
          srand(time(NULL));
          n=rand()%z+c;
          cout<<n<<"\nToo high or too low?\n";
          getline(cin, guess);
          if (guess=="Too high"|guess=="too high")
             z=n;
             else if (guess=="Too low"|guess=="too low")
                  c=n;
                  else break;        
    }
    cout<<"You win.\n";              
    system("PAUSE");
    return EXIT_SUCCESS;
}


As you can see, I'm using getline in a cycle, as I said before. When I run the program it skips the step when I should enter what I want to enter. Could anyone explain why is that?

P.S. I know, that the whole program is messed up, I usually make the code look a little more understandable after I make the program work. Same goes for comments.
On line 11 you're getting input from the user and storing it in the variable 'z'. On line 13 you're simply clobbering the user's input (making z equal to 100).

On line 17 you're trying to use the variable 'c' before it has been initialised. I'm surprised that you even get to line 19...

Before we get to discussing 'getline', you should write down some pseudocode for your program so that you know the process you need to code. I'm not sure, but I think you're trying to write a number guessing program, right?

Post up your next attempt with these mistakes fixed and we'll go from there...
Last edited on
On line 11 I ask user to enter the number so that I won't forget it. I don't output it because when you input, you still see what you have inputted, and if I wrote to output z, I would pretty much see the same number two times. Also, that line is pretty much for myself, I would delete that line after I finish anyway.

About line 17: I moved to c++ from pascal. There if you don't define a variable's value earlier in program, it's automatically 0. Though now I remember, that I've read that you have to initialize it (in c++, I mean). Well, thanks about this.

So:

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

int main()
{
    int n, z, c=0;
    string guess;    
    cout<<"Enter a number\n";
    cin>>z;
    cout<<"\n";
    z=100;
    while (1)
    {
          srand(time(NULL));
          n=rand()%z+c;
          cout<<n<<"\nToo high or too low?\n";
          getline(cin, guess);
          if (guess=="Too high"|guess=="too high")
             z=n;
             else if (guess=="Too low"|guess=="too low")
                  c=n;
                  else break;        
    }
    cout<<"You win.\n";              
    system("PAUSE");
    return EXIT_SUCCESS;
}
Last edited on
You're welcome, but I'm guessing your program still doesn't function like you want it to, right? In order to help we need to know what you're trying to do. Are you indeed trying to create a program where you enter a number and the computer tries to guess that number? If so, you need to take a step back and write down in words what you want the program to do, step by step. Feel free to post some pseudocode here and we can discuss it...
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
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

int main()
{
    int n, z, c=0;
    string guess;    
    cout<<"Enter a number\n";
    cin>>z;
    //I ask user to enter the number so that you won't forget it in the middle of guessing process
    cout<<"\n";
    z=100;
    while (1)
    {
          srand(time(NULL));
          n=rand()%z+c; /* z is 100 and c is 0 at first, but later on, 
                             we narrow the randomization by using these identifiers*/
          //computer randomizes the number and prints it out
          cout<<n<<"\nToo high or too low?\n";
          getline(cin, guess);
          /*User inputs whether it's guess was too high, or too low. 
          If the user enter something else, the cycle ends.*/
          if (guess=="Too high"|guess=="too high")
             z=n;
             /*If the guess was too high, I narrow down the randomization 
                  making the randomisations max not to go above the last guess*/
             else if (guess=="Too low"|guess=="too low")
             /*If the guess was too low, we narrow down the range to interval (last guess;highest guess)*/
                  {
                                  c=n;
                                  z=z-n;
                  }
                  else break;        
    }
    cout<<"You win.\n";              
    system("PAUSE");
    return EXIT_SUCCESS;
}


The comments should explain everything. But the problem isn't in calculations - the problem is that getline in the cycle doesn't work. It simply skips the getline step.

Though when I did my last homework for pascal, it was the same. At home, my computer would always skip the input, while on the computer in the school, everything worked fine. If this works fine for everyone else, that means that it's my computer's fault again.


Edit: Yes, I'm trying to make the guessing program.
Last edited on
You should add cin.ignore(); between lines 21 a 22. It'll discard any characters from the input stream. There is a newline character left from the previous use of cin on line 11.

Look here for reference: http://www.cplusplus.com/reference/iostream/istream/ignore/

Another point is that you have to include <ctime> header to be able to use time().
Combining cin and getline should kind of be avoided. You press enter after every cin, which leaves newlines in the buffer. The getlines following then grab these newlines as data, skipping over your input. You can ignore() to dodge the problem, although I personally find that a bit messy.
cin.ignore() is more of a quick fix rather than a real solution. It only discards one character and there could be more than one character in the input stream. The parashift FAQs have some really excellent material on dealing with IOStream issues. There are also numerous articles on this website that discuss the issue. The following link provides example code of particular interest.
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.3

It looks like you are setting z to 100 regardless of what the user enters. I'd probably follow the pattern in that FAQ if I were you and simply loop until the user enters a valid integer and then clear everything in the input stream. cin.ignore() will not fix the problem unless you use it correctly.
What if you accidentally type the caps lock key and enter something like "too LOW"? You could consider transforming the input string into all lower case and then perform the comparison.
I know, kempofighter, I just planned to advance in improving the program after I made it work.

I'd probably follow the pattern in that FAQ if I were you and simply loop until the user enters a valid integer


It doesn't really matter, what number user inputs. The user himself decides, whether the program guessed too high or too low, and the program only randomizes a number within the range.


I think I get it what's wrong now. Though I'm still a beginner and I had to read what you've said a lot of times to understand it ;D. And I still don't understand some things. But I hope I will one day.

Anyway, what kind of input would you use yourselves here?

Topic archived. No new replies allowed.