• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

I don't want STRING to ignore whitespace

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:

** ADD NEW ENTRY **

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.
0
jrobinson3k1
Asked:
jrobinson3k1
  • 5
  • 2
1 Solution
 
DrAskeCommented:
use getline function for each input...
example : getline(cin, name);
0
 
DrAskeCommented:
noo .. there is a mistake ..
there is two way to accept a string like "John Smith" ..
1)
char input[Max_Size];

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

2)

declare two strings
string fname, sname;
then
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) ..

regards;
0
 
DrAskeCommented:
in the second way ..
after get the fname and sname you can do

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

regards, Ahmad;
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

#include<iostream>
#include<string>
using namespace std;

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

}
hope that helps ;o)

bye
0
 
DrAskeCommented:
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 ..
0
 
jrobinson3k1Author Commented:
All righty, I'll try this when I get a chance. Thanks.
0
 
jrobinson3k1Author Commented:
works perfectly. thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now