By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,855 Members | 865 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,855 IT Pros & Developers. It's quick & easy.

Making spaces show when inputted

P: 2
I need to get the code to show the address with spaces when being typed in. can someone please help me? this is the code:


Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     string firstname;
  9.     string lastname;
  10.     string address;
  11.     string city;
  12.     string state;
  13.     string zipcode;
  14.  
  15.     cout << "First name? ";
  16.     cin >> firstname;
  17.  
  18.     cout << "Last name? ";
  19.     cin >> lastname;
  20.  
  21.     cout << "Address? ";
  22.     cin  >> address;
  23.  
  24.     cout << "City? ";
  25.     cin >> city;
  26.  
  27.     cout << "State? ";
  28.     cin >> state;
  29.  
  30.     cout << "Zipcode? ";
  31.     cin >> zipcode;
  32.  
  33.     cout << firstname
  34.          << " "
  35.          << lastname <<endl;
  36.     cout << address 
  37.          <<endl;
  38.     cout << city << " "
  39.          << state << " "
  40.          << zipcode
  41.          << endl;
  42.  
  43.     system("pause");
  44.     return 0;
  45. }
if i put spaces when typing the address, it skips some parts... it only works if i put the whole address together with no spaces... someone please help :) thanks
Aug 31 '07 #1
Share this Question
Share on Google+
2 Replies


Ganon11
Expert 2.5K+
P: 3,652
Ah-hah...what you've discovered is the difference between cin and getline. You see, when determining what value has been typed in, cin uses all whitespace to separate values. Whitespace includes, well, the space character ' ' and the newline character '\n', among other things. You know when you hit the space bar, you create a ' ' character, and a '\n' is generated when you press enter. Unfortunately, this means cin can only get 1 word in a string at a time. To get the whole thing, you need to use getline.

getline is a function of this form:

Expand|Select|Wrap|Line Numbers
  1. getline(inputStream, yourStringVariable);
inputStream in this case is cin, and yourStringVariable will be address. getline will retrieve all text from the point at which inputStream begins until the next '\n' - this includes the space character!

The only problem you might encounter is any leftover '\n' from your previous inputs, since the regular cin >> calls may have left a '\n' in the stream. In that case, getline immediately sees the '\n' and thinks it is done. To avoid this problem, you need to use the following statement just before the getline statement:

Expand|Select|Wrap|Line Numbers
  1. cin.ignore(100, '\n');
This will discard either 100 characters from the input stream or discard characters until a '\n' is found, whichever comes first. This will clear the stream so that you can properly retrieve the address.

Hope that helped!
Aug 31 '07 #2

P: 2
Thanks for the help. i put in the code and it worked fine. I appreciate it :)
Sep 1 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.