Crju
Crju

Reputation: 99

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;
}

Upvotes: 1

Views: 138

Answers (2)

Mike Seymour
Mike Seymour

Reputation: 254461

cin >> address;

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

std::getline(cin, address);

Upvotes: 4

Some programmer dude
Some programmer dude

Reputation: 409176

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).

Upvotes: 2

Related Questions