Spaces with string?

I'm having a bit of trouble getting spaces to work with string.

The program works perfectly if the "lotName" is only one single string with no spaces, but ends up not working if it has spaces (People vs. Hundred People). Is there a way around this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
string lotName;
double reservePrice;
cout << "Enter lot name: ";
cin >> lotName;
cout << "Reserve price: $ ";
cin >> reservePrice;
if(reservePrice < 0)
{
printErrorMessage(5);
}
	return 0;
}
Last edited on
Use std::geline to read a whole line instead of a single word.

std::getline(std::cin, lotName);
ii thing he's already using

using namespace std;

so take out the "std::"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
string lotName;
double reservePrice;
cout << "Enter lot name: ";
getline(cin, lotName);
cout << "Reserve price: $ ";
getline(cin,reservePrice);
if(reservePrice < 0)
{
printErrorMessage(5);
}
	return 0;
}

Last edited on
Topic archived. No new replies allowed.