Vector or Array

Howdy, need help with this. I didn't really get any help or an answer.

original post

http://www.cplusplus.com/forum/beginner/273332/

Really would like to know how to...

Add(A) for adding information or List(L) for listing all the input.

Enter Name: (get the input)
Enter Place of birth: (get the input)
Year of birth: (get the input)
For most purposes choosing std::vector over an old school array is a good bet. Especially if you need a dynamic sized container. std::vector manages the memory for you.

I didn't really get any help or an answer.

You got a lot of help. Very good help.

You asked several different questions. Different tasks.

1. How to do data entry for 3 different forms of data (name, place of birth, year). There are several different ways to store the input. Two possible choices are: as 3 separate variables, or as a struct.

Choosing how you store the individual pieces of your data makes choosing a container for multiple sets of data another task.

Choosing the variable type for each type of data is also critical. Name and place of birth could be std::strings, year as an unsigned short/int.

2. How to loop for your data entry until all the data is entered.

3. Displaying all of the entered data.

Three separate tasks. Focus on each task one at a time until you are sure it has been coded to work without fail.

If this is classroom homework paste the exact requirements statement(s) into your code as comments. So you have constant reminders of what you have to do.
Please see the follow up post. This is what I have. http://www.cplusplus.com/forum/beginner/273332/

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
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

struct people_t {
    string name, place;
    int year;
} group [3];

void printpeople (people_t people);

int main ()
{
    string mystr;
    int n;
    char choice;
    
    while (1)
    {
        cout << "Add(A), List(L) or Quit(Q) : ";
        cin >> choice;
        switch (choice)
        {
            case 'A':
            {
    
    for (n=0; n<3; n++)
    {
        cout << "Enter Name: ";
        getline (cin,group[n].name);
        cout<<"State of Birth: ";
        getline (cin, group[n].place);
        cout << "Enter year: ";
        getline (cin,mystr);
        stringstream(mystr) >> group[n].year;
    }
                break;
            }
                
                
                case 'L':
            {
                for (n=0; n<3; n++)
                {
    cout << "\nYou have entered these movies:\n";
    for (n=0; n<3; n++)
        printpeople (group[n]);
                }
                break;
            }
            case 'Q':
                
            {
                cout << "Be Blessed! Bye!";
               return 0;
            }
                
        }
    }
}
void printpeople (people_t people)
{
    cout<<"Name: "<<people.name<<endl;
    cout<<"State of birth: "<<people.place<<endl;
    cout<<"Year of birth: "<<people.year<<endl;
}


Topic archived. No new replies allowed.