473,385 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Making spaces show when inputted

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
2 1223
Ganon11
3,652 Expert 2GB
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
Thanks for the help. i put in the code and it worked fine. I appreciate it :)
Sep 1 '07 #3

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

Similar topics

4
by: Jay Chan | last post by:
I am trying to export data from a SQLServer database into a text file using a stored procedure. I want to be able to read it and debug it easily; therefore, I want all the columns to indent nicely....
7
by: usenet | last post by:
I would like, if it's possible, to set the value of a field in a table to a number of spaces. One space would be fine, I just want to be able to set the field to a default value that's not NULL...
6
by: phamn1985 | last post by:
I m trying to make a login page via Microsoft access. This is my table .. Table name : tblLogin PK : loginID(Auto number) Attributes : username , password. I make a login form in Microsoft...
1
by: bigmaddaz | last post by:
Ok im designing an application for working out compount interest. The user starts the page, 3 prompts appear, one asking for money invested, next asking for rate of interest, and last one asking for...
69
by: kabradley | last post by:
Alrighty Guys and Gals, I have another question that I hope you all can help me with. I have a report that uses a cross-tab query as its record source. This cross-tab query is getting all of its...
2
by: tavspamnofwd | last post by:
Referred here from the tutor list. ---------- Forwarded message ---------- From: Evert Rol Hi Tom, Ok, I wasn't on the list last year, but I was a few days ago, so persistence pays off;...
2
by: mirianCalin | last post by:
hi.. im doing a site.. i have this registration form where the user must enter some information like name, phone_number, age, etc.. an error message should appear if #1: there is an empty...
2
by: =?Utf-8?B?QnJpYW4=?= | last post by:
I have an application that is responsible for retrieving data from a variety of remote sites using all sorts of different protocols. e.g. email, web service, ftp, screen scraping etc. I want to...
8
rcollins
by: rcollins | last post by:
What I need to do is to have the login name box create the user name from the name inputted. I would like to have it create f initial l name except when that is already used, then I would like f...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.