Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to include white spaces in string input

Posted on 2008-11-07
5
Medium Priority
?
1,330 Views
Last Modified: 2012-05-05
In the code snippet below the output of the program is
Ahmed Anis

if I try to replace the line
name = "Ahmed Anis";

with
cin >> name;

and type in
Ahmed Anis

in the program input during run time it only output my first name i.e
Ahmed

It appears that white spaces are ignored in the input process, I was wondering if the was a way to include my full name with spaces in string input?

Thanks in advance
#include <iostream>
#include <string>
using namespace std;
 
void main(void)
{
	string name;
        name = "Ahmed Anis";
 
	cout << name << endl;
	
}

Open in new window

0
Comment
Question by:a_anis3000
  • 2
  • 2
5 Comments
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 2000 total points
ID: 22906493
>>>> It appears that white spaces are ignored
No, on contrary, the whitespace are recognized as terminator. >ou would need one more cin >> xxx to catch the next word.

If you want to catch a whole line you should use getline

    getline(cin, name);

Here you could type as much as you want until enter is typed.
0
 

Author Comment

by:a_anis3000
ID: 22906980
just tried getline, its the perfect workaround for the space issue. Thanks itsmeandnobodyelse. Before closing the question, I was wondering if this could be used instead of cin for other variable types or it is just for strings?
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22907054
>>>> I was wondering if this could be used instead of cin
Yes, you better make all reads using getline. However for integers or floatinng-point values you have to convert from string to int/double after. That could be done in C++ using stringstreams:

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


    string line;

    cout << "enter your age  ";
    getline(cin, line);

    int i = 0;
    istringstream iss(line);
    if (!iss >> i)
    {
           cout << line << " was not a valid input " << endl;
    }

    }
    i
0
 

Author Closing Comment

by:a_anis3000
ID: 31514446
Perfect work around. Thanks for the tips
0
 
LVL 11

Expert Comment

by:alexcohn
ID: 22907902
You can redefine the field separator with cin.setfill() function or manipulator. For example, use code

string name;
int age;
cin >> setfill(',') >> name >> setfill(' ') >> age;

to read the following line;

Ahmed Anis, 23
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

581 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