how to access certain elements in c++ and store multiple inputs

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    std::vector<std::string> name, age, favsinger;
    std::cout << "Hello, what is your Name Age Favorite_Singer? ";
    std::cin << name; //i want to store the user's info along with the sibling's
    std::cin << age;
    std::cin << favsinger;

    int n;
    std::cout << "How many siblings do you have? ";
    std::cin << n;

    for (int a=0;a<n;a++){
        std::cout << "Please enter Name Age Favorite_Singer for sibling #" << a << ": ";
        std::string a, b, c;
        std::cin >> a;
        std::cin >> b;
        std::cin >> c; //this part throws me off because favorite singer has a first_name SPACE last_name
        name.push_back(a);
        age.push_back(b);
        favsinger.push_back(c);

    }


Let's say that the user inputted "3" siblings with the information:

1
2
3
Michael 24 Madonna
Sam 20 Michael Jackson
Anna 18 None


i want to be able to access each of the sibling's preferences, whether it be their name, age, or favorite singer.

I tried doing

 
age[1]


but if i do

 
favsinger[1]


it gets thrown off because of the space.
Last edited on
For std::cin you should use >> instead of <<.

Since 'a' is the variable you used to loop the for-loop, you should rename the string 'a' that you use inside the loop to avoid name conflicts. And you want to declare a variable before using it(look at the first 2 lines of the for-loop again).

And C++ starts a 0, so to access the first element, you would do something like age[0].

Now on the main problem. When the user enters in "Michael Jackson", only "Michael" is stored into the string. That messes things up.

One way to fix this is using the getline function:
http://www.cplusplus.com/reference/istream/istream/getline/

To use it, you create a character char array of an arbitrary size(256 should be more than enough). And you use it kind of like this: cin.getline(name, 256) //where name is the char array mentioned

So when the user enters in a name like "Bobby John", the whole name is stored. name[0] would equal "B", name[1] = "o", etc.

But if you cout << name, , it should just print out the whole name. You don't have to print out every char individually.
Last edited on
But if I use getline, how am i supposed to do it only for the third category "favorite_singer"? I want to store it into a vector since name/age is already in it so I can access everything
You can use it only for the favorite_singer input. The other inputs don't have to change.

In the example I mentioned, name is a char[256], and not a string.

One way to change it into a string is this:
http://stackoverflow.com/questions/8960087/c-how-to-convert-a-char-array-to-a-string

string str(name);

Now it's a string, so you can store it in a vector of strings. I don't think you can store a vector of any arrays, so you would have to change it into a string.


I hope that works.

Or look at these examples on how to convert it:
http://www.cplusplus.com/forum/beginner/96227/

1
2
3
 char asd[] = "adas das das 232";

	string dsa = string(asd); 


I FORGOT TO MENTION:

You may have to clear the char array each time.
For example, if the first run was "John S", then the second run was "Jane S", the array would be "John SJane S".
Here are some ways to clear it:
http://stackoverflow.com/questions/11626262/clearing-contents-of-a-fixed-char-array-in-c-after-value-has-been-assigned
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <string>
#include <vector>

int main()
{
    std::vector<std::string> name, age, favsinger;

    std::cout << "Hello, what is your Name Age Favorite_Singer? ";

    // std::cin << name; //i want to store the user's info along with the sibling's
    std::string the_name ;

    // read an entire line typed in by the user
    std::cout << "name? " ;
    // http://www.cplusplus.com/reference/string/string/getline/
    std::getline( std::cin, the_name ) ;
    name.push_back(the_name) ; // add the name to the vector


    // std::cin << age;
    std::string a ;
    std::cout << "age? " ;
    std::getline( std::cin, a ) ;
    age.push_back(a) ;

    std::string f ;
    std::cout << "favourite singer? " ;
    std::getline( std::cin, f ) ;
    favsinger.push_back(f) ;

    int n;
    std::cout << "How many siblings do you have? ";
    std::cin >> n;

    // throw away the new-line left in the input buffer by std::cin >> n;
    // http://www.cplusplus.com/reference/istream/istream/ignore/
    // see: http://www.cplusplus.com/forum/general/69685/#msg372532
    std::cin.ignore( 100, '\n' ) ;

    for( int i=0; i<n; ++i ){
        std::cout << "Please enter Name Age Favorite_Singer for sibling #" << i << ": ";

        std::string a, b, c;

        std::cout << "name? " ;
        std::getline( std::cin, a ) ;

        std::cout << "age? " ;
        std::getline( std::cin, b ) ;

        std::cout << "favourite singer? " ;
        std::getline( std::cin, c ) ;


        name.push_back(a);
        age.push_back(b);
        favsinger.push_back(c);
    }

    // ...
}
Topic archived. No new replies allowed.