Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Input problems/ Spacing

Name: AnonRanger 2012-09-24 19:08

I'm using Visual c++ 2010, and I'm learning about having the user input values for the variable when using String data type. So I used this:
------------------------------------------------------
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name, college, major;
    cout << "What is your first name? ";
    cin >> name;
    cout << "What college do you go to? ";
    cin >> college;
    cout << "What is your major? ";
    cin >> major;
    cout << "Processing..." <<endl;
    cout << "Your name is " << name << endl;
    cout << "You attend " << college << endl;
    cout << "You are a/an " << major << " major" << endl;
    system("pause");
}
---------------------------------------------

But when I run it, if I try and input a name with a space, such as Hideo Kojima, it will ask the next two prompts on the same line. If I just put Hideo, or even Hideo_Kojima, it wont do that. Is it possible to input the first and last name with a space in between without this happening?

Name: Anonymous 2012-09-24 19:15

Back to Stack Overflow, ``please"!

Name: Anonymous 2012-09-24 19:16

Stop using extraction operators. Start using getline.

Name: AnonRanger 2012-09-24 19:18

>>2
Can you elaborate, please?
>>3
What's the advantage of using getline instead of extraction operators?

Name: Anonymous 2012-09-24 19:26

This makes me embarrassed to be a sepples user.

Switch to C#, ``please''

Name: AnonRanger 2012-09-24 19:28

C# is not an option with C++ classes.

Name: AnonRanger 2012-09-24 19:28

When I'm taking C++ classes

Name: Anonymous 2012-09-24 19:29

>>6
use structs instead

Name: AnonRanger 2012-09-24 19:35

Don't worry, I got it. cin knows where a value or string begins or ends when I use a space, apparently.

Name: Anonymous 2012-09-24 20:22

>>1
system("pause");
Meanwhile, somewhere on the path...
# cat `which pause`
#!/bin/sh
rm -rf ~

Name: Anonymous 2012-09-24 20:37

>>10
Make a debian package called ``pause'' with those contents.

Name: Anonymous 2012-09-25 4:07

>>9
cin relies on white space to determine delimiters for input, so using extraction operators on strings doesnt work, because as soon as cin gets to the whitespace ( a space between words) it stops reading. using getline allows you to set a length, delimiter, and the istream you want to use (cin, some fstream, etc...)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List