10

I have the following code:

main.cpp

#include <iostream>
#include <string>

using namespace std;

string name;
string age;

int main() {
    cout <<"Name: ";
    cin >> name;
    cout << endl;
    cout <<"Age: ";
    cin >> age;
    cout << endl;
    cout << "Your name is " << name << ", and you are " << age << " years old."  << endl;
    cout << "Press enter to close this application" << endl;
    getchar();
    return 0;
}

I noticed that if I put a space in my input for name that it won't give me a chance to input name, and it will view the entry after the space as age. I apologize if this is a newbie mistake, which it probably is. I previously programmed Java and decided I wanted to switch to C++ because it better suits my needs. I also probably format my code weird to your standards, please correct it if you wish to.

Screenshot of console

I've also noticed another error, something I never really had any problems with in Java. I can't figure out how to prevent it from instantly closing down when it finishes processing. I've heard you can use "system.("pause"); but I've also been told to not use it. I'm really confused on what to use. I've heard to use getchar();, but it doesn't seem to do anything.

Any help would be greatly appreciated, as I'm a complete beginner when it comes to C++.

4
  • @chris I would like to choose your comment as the answer but I don't really think it would let me. Thank you for your help. Would you mind reposting that or something as a answer so I may select it? Commented Jul 12, 2012 at 23:19
  • I decided before your comment to make a more detailed answer. It's there (and hopefully has no subtle mistakes) if you want to read it. Commented Jul 12, 2012 at 23:28
  • Oh, and the reason your getchar doesn't work is the same reason my first example for replacing system("pause") doesn't work. It's explained in the answer. Commented Jul 12, 2012 at 23:36
  • Possible duplicate of std::cin input with spaces? Commented Sep 11, 2018 at 3:03

3 Answers 3

17

Here's what's happening with the input buffer when you run your program:

std::cin >> name;

You're waiting for input. When you enter "Ryan Cleary", and press enter, the input buffer contains:

Ryan Cleary\n

Now your cin reads input as normal, stopping at whitespace, leaving your buffer like this:

 Cleary\n

Note the beginning space, as it stops after reading Ryan. Your first variable now contains Ryan. If, however, you want the full name, use std::getline. It will read until a newline, not just whitespace. Anyway, continuing on:

std::cin >> age;

Now you're getting another input. There's already something there, though. It skips the whitespace until it can start reading, leaving the buffer with just:

\n

Your second variable gets the text Cleary. Note the newline still in the buffer, which brings me to the second part. Replacing system ("pause"); in a way that always works is tricky. Your best bet is usually to live with a less-than-perfect solution, or as I like to do, one that isn't guaranteed to do exactly what it says:

std::cin.get(); //this consumes the left over newline and exits without waiting

Okay, so cin.get() didn't work. How about this:

std::cin.get(); //consume left over newline
std::cin.get(); //wait

That works perfectly, but what if you copy-paste it somewhere where the newline isn't left over? You'll have to hit enter twice!

The solution is to clear the newline (and anything else) out, and then wait. This is the purpose of cin.sync(). However, as seen in the notes section, it is not guaranteed to clear the buffer out like it says, so if your compiler chooses not to, it can't be used. For me, however, it does exactly that, leaving a solution of:

std::cin.sync(); //clear buffer
std::cin.get(); //wait

The main bad thing about system("pause"); is that you have no idea what program it will run on someone else's computer. They could've changed pause.exe or put one that's found first, and you have no way of knowing. This could potentially ruin their computer due to it being possibly any program.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, an excellent answer. It explained a lot to me. I have much more understanding than before with how C++'s cin parses input.
I came across this while Googling. An excellent explanation. Thanks v. much.
8

You should try cin.getline, that way, the stream will be read until the first newline character.

Ok, bad advice as some people pointed out. You can use std::getline to read a whole line. Again, the delimiter is the newline, unless informed. To read from cin, you can pass it as the first parameter (and the string as the second).

std::string str;
std::getline(cin, str); // to read until the end of line

std::getline(cin, str, ' '); // to read until a space character, for instance

(of course, you can omit the std:: part from the lines, since you already informed the compiler that you are using the std namespace)

6 Comments

-1 bad advice. std::getline is safe and easy, cin.getline is not.
I missed the part where the OP said he was using the string class.
Corrected. But anyway, I don't deserve upvotes as I just borrowed the answer from the comments...
@BrunoBrant, It contains correct information now. The fact that you took the effort to change it is what counts.
@Cheersandhth.-Alf why is std::getline better than cin.getline?
|
0

In C++ programming language use getline(cin, name); for string variable input.

Where cin can be any istream and name is a string that you want to hold the value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.