Multiple cout statements

Hi, I'm trying to write a code and at first I thought it would be incredibly easy but for some reason it's giving me trouble. I'm fairy new to C++ and learning it now in my class, so I don't really understand everything.

I'm supposed to ask the user to input the first and last name, street address and city and state all on separate lines and as strings. This is what I have, but it doesn't work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string names;
	string st;
	string ctySte;

	cout << "Enter your first and last name. \n";
	cin >> names;

	cout << "Enter your street address. \n";
	cin >> st;

	cout << "Enter your city and state. \n";
	cin >> ctySte;

	return 0;
}


It only lets me input for the first part then just shows the second two and says to press any key to continue.
Last edited on
Hi,

std::cin only reads up until the first space. Instead have a go with std::getline

http://www.cplusplus.com/reference/string/string/getline/

Also, try to use more meaningful variables names. st is not particularly good IMO. There is this general mis-concepton that one needs to abbreviate everything when coding. Better to make things clear IMO.

Good Luck !!
Thank you!!!
Topic archived. No new replies allowed.