I have the following code:
#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.
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++.
Inputting a string You can use cin but the cin object will skip any leading white space (spaces, tabs, line breaks), then start reading when it comes to the first non-whitespace character and then stop reading when it comes to the next white space. In other words, it only reads in one word at a time.
In the line “cin >> c” we input the letter 'Z' into variable c. However, the newline when we hit the carriage return is left in the input stream. If we use another cin, this newline is considered whitespace and is ignored.
A string can have spaces in it, but a lot of string-handling functions stop when they encounter a space. Both will read in tabs spaces and stop a newline (or other delineator if supplied).
String variables contain strings of characters. The characters can be any printable character on the keyboard, including spaces and punctuation (but not function keys, the enter key, and some others.)
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With