Why does my code keep doing this

I have a program and i am using getline(cin, scname); but it skips the first input and gos to the second, why is that?

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
#include <iostream>
#include <ctime>
#include <random>
#include <string>

void MAINPROGRAM();

using namespace std;

int main()
{
    string choice;

    cout << "New" << endl;
    cout << "Load" << endl;
    cin >> choice;

    if(choice == "New" || choice == "new")
    {
        MAINPROGRAM();
    }
    else if(choice == "Load" || choice == "load")
    {

    }
}

void MAINPROGRAM()
{
    string scname;
    string plrname;

    cout << "Welcome Shipping Supplies" << endl;
    cout << "Please enter the name of your shipping company" << endl;
    getline(cin, scname);
    cout << "\n";
    cout << "Ok so the name of your company is " << scname << endl;
    cout << "\n";
    cout << "And what is your name?" << endl;
    getline(cin, plrname);
    cout << "Ok " << plrname << " welcome to the game! lets get started" << endl;
    cout << "\n";

}
I think it is because after entering cin >> choice; the new line character is still in the buffer.
still in the buffer? how do i fix it?
Google is your friend:
http://www.google.com/search?client=safari&rls=en&q=why+isn't+getline()+working&ie=UTF-8&oe=UTF-8

And an answer:
http://answers.yahoo.com/question/index?qid=20091206224451AAkAIji

A better version would be

1
2
3
4
if (cin.peek == '\n')
{
    cin.ignore();
}


which only ignores the next character if it's a newline character.
Last edited on
Ok i tried

1
2
3
4
if (cin.peek == '\n')
{
    cin.ignore();
}


But my compiler says


||=== Guess, Debug ===|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp||In function 'void MAINPROGRAM()':|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|33|error: invalid use of member (did you forget the '&' ?)|
||=== Build finished: 1 errors, 0 warnings ===|
bump
peek is a function. cin.peek()

In this particular case, using getline to get choice in main is probably the superior choice, then you wouldn't be mixing formatted and unformatted input.
Last edited on
Post your code (it's not all that long)
ok i got it working now. So about cin.peek(); i dont quite understand it all that well, i read this on this site:

"Reads and returns the next character without extracting it, i.e. leaving it as the next character to be extracted from the stream."

So basically when it finds whatever you specify it just skips over it?
If you're referring to the code snippet:

1
2
3
4
if (cin.peek() == '\n')
{
    cin.ignore();
}


yes.

If you're referring to cin.peek() on it's own, no. It does exactly what it says. It tells you what the next character in the stream is and leaves it there for the next extraction operation to retrieve.
1. ok so when put like it is above it checks to see if the next line contains whatever the user specifies and if it finds it, it will, in this case, just ignore it right?

2. but it \n still works, a line is still skipped so i guess i dont understand that part.

3. cin.peek(); just checks the next character in the stream, so it only fins text in a cout statement or, what code is next?
cin.peek() tells you what the next character in the stream cin is. That is all that it does. One doesn't need to refer to lines or cout, it is simply the next character that will be extracted from cin.

1
2
3
4
if (cin.peek() == '\n')  // if the next character in the stream is a newline...
{
    cin.ignore();  // extract and discard the next character in the stream.
}
Last edited on
Topic archived. No new replies allowed.