Here is a part of my program. For this part, I want the user to be able to enter how many places they want to eat at, and then it will allow the user to enter which places they want (they will enter however many places as numbers they put).
My problem occurs when the user inputs a place that has a space in it. If it has a space, then the program pretends he hit enter instead. For example if the user chooses 4 places, and the first place he wants to eat is "jack in the box", then the program will run right to the end and use the four places as "jack", "in", "the", and "box".
How can I get the spaces to not be counted as enters?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int num_places;
string places[25];
StartOver: //a goto function option if the user mistypes how many places they want to eat at
cout << "How many places do you want to eat at today? "; //asks user how many places he/she wants to eat at
cin >> num_places;
cout << endl;
EnterPlaces: //a goto function option if the user mistypes which places they want to eat at
for (int i=1; i <= num_places; ++i) // Gets data of how many places the user wants to eat at
{
cout << "What is your number " << i << " place you want to eat at: ";
cin >> places[i];
cout << endl;
};
Honestly, I'm not sure. I don't know if you can fill in an array like that or not...I always just have to play around with things until I find what works. I'm sure someone more experienced will have a more helpful answer, but I know how frustrating it can be to wait all day and have nobody respond, so I thought I'd throw in my two cents in case it helped.
Go up to the search box at the top and type in getline. It will give you info on it there, and you can probably tweak it enough to get it to work. I think you need a #include statement for it, but I'm not sure which one...but it should be in there...
The only problem now is that it skips the first option. So if I say I want to eat at 4 places, it will ask for the first one, but then automatically put a blank there and then ask for the second one, and then everything is normal(and the spaces work). Do you know why it skips the first?
I needed it to not say "What is your number 0 place you want to eat at today", so I put an if that does nothing if it is 0, and now it works perfectly! Thank you!
1 2 3 4 5 6 7 8 9 10 11
for (int i=0; i <= num_places; ++i) // Gets data of how many places the user wants to eat at
{
if (i==0)
;
else
cout << "What is your number " << i << " place you want to eat at: ";
getline(cin, places[i]);
cout << endl;
};
Yay! Awesome! Am I on a roll today or what?! Hee hee. :)
~Note: You could also put (i + 1) in that cout statement to make it easier to read. That way it could still ask for your 1-4 places to eat, and it would still put them in array spaces 0-3.
for (int i=0; i < num_places; ++i) // Gets data of how many places the user wants to eat at
{
cout << "What is your number " << i + 1 << " place you want to eat at: ";
getline(cin, places[i]);
cout << endl;
};