Program is treating a space as if I were pressing enter

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;
        };
Last edited on
Could you use getline in there? I think that gets your whole line, including spaces. cin doesn't do whitespaces, I don't think.
I am sorry, I don't know how to use getline. What is it? And how would I use it here?
Could you say:
getline(cin, places[i])

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...
No, my above was wrong, but here is how you would do it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// istream getline
#include <iostream>
using namespace std;

int main () {
  char name[256], title[256];

  cout << "Enter your name: ";
  cin.getline (name,256);

  cout << "Enter your favourite movie: ";
  cin.getline (title,256);

  cout << name << "'s favourite movie is " << title;

  return 0;
} 
Wow that works(for the most part)! Thank you!

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 saw that on the page too, but it does not work for some reason. Your first suggestion worked very well.
Is your i initially set to 1? It should be set to 0 first.
That worked!

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;
        };
Last edited on
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.
Last edited on
Try this instead:
1
2
3
4
5
6
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;
        };
Last edited on
Topic archived. No new replies allowed.