I don't want STRING to ignore whitespace

Posted on 2006-05-26
Medium Priority
Last Modified: 2006-11-18
I know that whitespace is ignored with the string class, but is there a way to override that and tell it to not ignore whitespace? Say for instance I want to input an address. If write this:

std::string address;
cout <<"Address: ";
cin >>address;

and say the address is "1000 Roverhill Rd" then the only thing address is going to pick up is "1000". I can't just make 3 seperate strings for each partition of the address because I don't know if the address will end up being 3 words, 4 words, or however long (Ex. 1000 Roverhill Rd and 1000 Rover Hill Rd). I want to just be able to use the above code I have and in the end, all parts of the address be stored in 1 string. Is this possible? Is there perhaps another header file I can download that address this issue?

Just to further clarify what I'm trying to do, here is what I'm writing:

string name, address, city, state, zipcode, purchased, date, serial, model;
      cout <<"** ADD NEW ENTRY **"<<endl<<endl;
      cout <<"First and Last Name: ";
      cin >>name;
      cout <<"Address: ";
      cin >>address;
      cout <<"City: ";
      cin >>city;
      cout <<"State: ";
      cin >>state;
      cout <<"Zip Code: ";
      cin >>zipcode;
      cout <<"Purchased From: ";
      cin >>purchased;
      cout <<"Date Purchased: ";
      cin >>date;
      cout <<"Bow Serial Number: ";
      cin >>serial;
      cout <<"Bow Model: ";
      cin >>model;

When I go to put in a person's name, here's how my console looks:


First and Last Name: John Smith
Address: City:

As you can see, it puts "John" in the name variable and "Smith" in the address variable. There a way to stop this? Thanks for any responses.
Question by:jrobinson3k1
  • 5
  • 2

Expert Comment

ID: 16770254
use getline function for each input...
example : getline(cin, name);

Expert Comment

ID: 16770310
noo .. there is a mistake ..
there is two way to accept a string like "John Smith" ..
char input[Max_Size];

cin.getline(input, Max_Size, '\n');
then assign the value to *name* string ..


declare two strings
string fname, sname;
getline(cin, fname, ' ');
getline(cin, sname);

so you enter the first name and the program won't skip to the next getline before pressing ' '(space character) ..


Expert Comment

ID: 16770320
in the second way ..
after get the fname and sname you can do

name = fname;
name.append(" ");

regards, Ahmad;
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

ID: 16770431
If I were you I would choose the second way .. because advanced c++ programmers beleives that casting is a bad thing ..
as I told you ..in the second way the code will not step to the second getline , until the user hit the enter key '  ' .
but you still have to hit the enter key twice after entering the second name :) ..

using namespace std;

void main()
      string fname, sname, name;
                cout<<"enter the name :"
      getline(cin, fname, ' ');
                    getline(cin, sname);
      name = fname;
      name.append(" ");

hope that helps ;o)


Accepted Solution

DrAske earned 1000 total points
ID: 16770807
I have just noticed that you don't have to do all that things ..
just do
cout<<"Enter the name : ";
getline(cin, name);

and it will accept any string -the user enter - untill hitting the enter key..!!

really , I don't know what I was thinking of ???
soo sorry ..
bye ..

Author Comment

ID: 16789471
All righty, I'll try this when I get a chance. Thanks.

Author Comment

ID: 16789954
works perfectly. thanks!

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question