简体   繁体   中英

It won't print full address

Can somebody please tell me why it won't print my full address? The final output is "Your name is Syd and you live at 2845." Why is it doing this? I clearly state within the cout to print the address string. (by the way, I actually type 2845 SE Taylor Street)

#include <iostream>
using namespace std;

int main()
{
    string address;
    cout << "Please enter your address." << endl;
    cin >> address;
    cout << "You live at " << address << "." << endl;
    return 0;
}
cin >> address;

This reads a single word, stopping at the first white-space character. To read a whole line:

std::getline(cin, address);

The input operator reads space separated values. So if your address have a space in it then it will read just the first "word.

And worse, if you enter your full name, the first input will read the first name, and the second input will read the second name.

Try to use std::getline to read the whole line, but first do std::cin.ignore(numeric_limits<streamsize>::max(),'\\n'); to skip the newline from the last input (or use std::getline for both inputs).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM