How to set up string variables for undefined number of user inputs?

Hi everyone,

I writing my first program and I need some help. The program is designed to allocate a single loss across various insurance companies who issued insurance for the insured during the period of the loss. In order to do this, you need to figure out when the loss began, when it ended, how many insurance companies insured the insured during that time, and the annual policy limits for each company.

I have written the code for the first two parameters: when the loss began and when it ended. Now I need to allow the user to enter the total number of companies that insured the insured during this time period and enter the name of each company... that's what I'm having trouble with. I need to allow the user to add the names of as many insurance companies as are required and then use those names as variables to calculate the liability for each.

There could be two insurance companies or ten, so I don't know how to handle this in terms of setting up variables. I could just set up a ridiculously high number of string variables that could then be inputted by the user, but then I'd have a bunch of unused variables. Also, this would just be sloppy and I'd like to know the right way to do it.

What's the best way to do this? Thanks,

Kevin

BTW, here's my code so far... As you can see, I initially tried to use a while loop to get the names of the insurance companies, but that won't work. Anyway, I'd welcome any comments that you may have:

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
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
using namespace std;

int getLeakBeginDate();
void setLeakBegin(int x, int y);
void setLeakEnd(int x, int y);
int getLeakEndDate();
int calculateTimeOfLeak();


class LeakCalc{

    private:
        int leakBeginY;
        int leakBeginM;
        int leakBeginDate;
        int leakEndY;
        int leakEndM;
        int leakEndDate;
        int totalTimeOfLeak;

    public:
        void setLeakBegin(int a, int b){
        leakBeginY = a;
        leakBeginM = b;
        };

        void setLeakEnd(int x, int y){
        leakEndY = x;
        leakEndM = y;
        };


        int getLeakBeginDate(){
            leakBeginDate = leakBeginY * 12 + leakBeginM;
            return leakBeginDate;
        };

        int getLeakEndDate(){
            leakEndDate = leakEndY * 12 + leakEndM;
            return leakEndDate;
        };

        int calculateTimeOfLeak(){
            totalTimeOfLeak = leakEndDate - leakBeginDate;
            return totalTimeOfLeak;
        };
};

int main()
{
    int a, b, c, x, y;
    int numInsCos;

    cout << "Enter the year the leak began" << endl;
    cin >> a;
    cout << "Enter the month of the year the leak began" << endl;
    cin >> b;

    LeakCalc LeakObject;
    LeakObject.setLeakBegin(a, b);
    LeakObject.getLeakBeginDate();

    cout << "What year was the leak discovered?" << endl;
    cin >> x;
    cout << "What month was of the year was the leak discovered?" << endl;
    cin >> y;

    LeakObject.setLeakEnd(x, y);
    LeakObject.getLeakEndDate();

    cout << "The tank leaked for a total of " << LeakObject.calculateTimeOfLeak() << " months." << endl;
    cout << "How many insurance companies  insured the property during these " << LeakObject.calculateTimeOfLeak() << " months?" << endl;
    cin >> numInsCos;

    while(c < numInsCos){
        cout << "Enter the name of the first insurance company" << endl;
        cin >>

    };

};
Hi there,

A std::vector is what you are after:

1
2
3
4
5
6
7
8
9
10
11
#include <vector>

std::vector<std::string> companies;  //Create a new vector of strings
companies.reserve(numInsCos);  //reserve just the amount of space we need, for efficiency

while(c < numInsCos){
        std::string company;  //create a string to store
        cout << "Enter the name of the first insurance company" << endl;
        cin >> company;
        companies.push_back(company); //add the string to the vector
    };


For more info on vectors:
http://cplusplus.com/reference/vector/vector/

Please do let us know if you have any further questions.

All the best,
NwN
Topic archived. No new replies allowed.