0

Let's say the user inputs

Sarah Freshman Computer Science Major
John Sophomore Math Major

I was wondering how am I able to store these multiple inputs into a list?

Name = [Sarah, John]
Year = [Freshman, Sophomore]
Major = [Computer Science Major, Math Major]

I am able to store the first two (Sarah/Freshman & John/Sophomore) into a list, but the latter part is hard because the major is separated into spaces.

--Edit: Example Code--

I am new to C++ and am trying to create a program that asks the user personal questions.

std::vector<std::string> name, year, major;
std::cout << "Hello, what is your Name Year Major? "; //asks user first
std::cin << name;
std::cin << age;
std::cin << major;

int n;
std::cout << "How many students will you input? ";   //enter other students info
std::cin << n;

for (int a=0;a<n;a++){
    std::cout << "Please enter Name Age Major for student #" << a << ": ";
    std::string a, b, c;
    std::cin >> a;
    std::cin >> b;
    std::cin >> c; //this part throws me off
    name.push_back(a);
    age.push_back(b);
    major.push_back(c);

}
Kara
  • 765
  • 5
  • 11
  • 29
  • Why do you need major in the first place? Just take the subjects, and if at all you do need it, append it later when you are displaying it. – SoulRayder Jan 20 '14 at 06:41
  • You can disregard the word 'Major'. I just wanted to add an extra word to emphasize how if I have more than one word, it will not be stored. – Kara Jan 20 '14 at 06:41
  • An easy way to do this would be to input Computer_Science_Major instead of one with spaces and then later change _'s to spaces – Ranveer Jan 20 '14 at 06:42
  • read it using gets(). – SoulRayder Jan 20 '14 at 06:42
  • @Ranveer I want the user to input freely and not have them put "_" – Kara Jan 20 '14 at 06:43
  • Please avoid using [`gets` as it's dangerous](http://stackoverflow.com/q/1694036/183120). – legends2k Jan 20 '14 at 06:44
  • Read about structures (the `struct` keyword) and [`std::vector`](http://en.cppreference.com/w/cpp/container/vector). – Some programmer dude Jan 20 '14 at 06:45
  • @Joachim Pileborg I did use std::vector but i'm able to gather the first two words, but the last part of the user's input gets cut off. (e.g. "Computer" not "Computer Science") – Kara Jan 20 '14 at 06:46
  • How about reading the whole line as one string, then seperate that string into 3 parts at the first 2 spaces? – user1781290 Jan 20 '14 at 06:47
  • @Kara: Use the [global `getline`](http://en.cppreference.com/w/cpp/string/basic_string/getline). There's an example on doing this in the linked documentation. – legends2k Jan 20 '14 at 06:47
  • 1
    I think you need to come up with some example code, preferably a [SSCCE](http://sscce.org/) to show us what you have done, what the expected output should be, and what the actual output is. – Some programmer dude Jan 20 '14 at 06:47

3 Answers3

1

Since others have mentioned how to do this by changing your input, a way you could do it without changing your input is to check if the word you are reading is the first word of a major (IE "Computer" "Math" etc) and use getline to the end of the line if you see that it is. If your input is going to be exactly like this, checking if a word was one of the class years would probably work even better, since you don't need to do any appending and the list of words to check is much smaller.

Alternatively, if you know that the form is always "First Name", "Class Year", "Major" you can simply start a getline after the second word read.

IllusiveBrian
  • 3,105
  • 2
  • 14
  • 17
0

Suggestion 1:

Use a getline() and insert a special character after every word and tell your program to mark the input as next input only upon encountering the special character. It's even easier in your case -- whenever the program encounters the word "Major", it goes to the next input.

Suggestion 2:

Input as Computer_Science_Major and later change _ to space in you program.

Ranveer
  • 6,683
  • 8
  • 45
  • 92
0

Use the reading-and-then-parsing strategy:

vector<string> name;
vector<string> year;
vector<string> major;

string line;
while(getline(cin, line)) // 1. reading...
{
    if (line == "0") // enter 0 to finish input
        break;

    // 2. parsing...
    int first = line.find(" "); // position of the first space
    int second = line.find(" ", first+1); // position of the second space

    name.push_back(line.substr(0, first));
    year.push_back(line.substr(first+1, second-first-1));
    major.push_back(line.substr(second+1));
}
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174