I don't want STRING to ignore whitespace

Posted on 2006-05-26
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
    LVL 9

    Expert Comment

    use getline function for each input...
    example : getline(cin, name);
    LVL 9

    Expert Comment

    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) ..

    LVL 9

    Expert Comment

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

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

    regards, Ahmad;
    LVL 9

    Expert Comment

    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 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)

    LVL 9

    Accepted Solution

    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

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

    Author Comment

    works perfectly. thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    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.

    794 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now