Allowing user to choose number of variables.

Trying to keep the brain active between semesters and am writing a program that rates girls. I am having trouble finding a way for the user to choose how many girls to rate. I have changed it from comparing two girls, to three girls.

I could continue that pattern I started with, but feel it will become repetitive! There has to be a better way.

Thanks in advance!

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  #include <iostream>
#include <iomanip>
#include <string>

using namespace std;

struct girlscore
{
    string name;
    int pretty;
    int smart;
    int crazy;
    double total = 0;
};

int main()
{
    girlscore girl1;
    girlscore girl2;
    girlscore girl3;
    int numgirls;
    
    cout << "How many girls would you like to rate: ";
    cin >> numgirls;
    
    if(numgirls >= 3)
    {
    cout << "\nEnter the name of girl one: ";
    cin.ignore();
    getline(cin, girl1.name);
    
    cout << "1-100, how pretty?" << endl;
    cin >> girl1.pretty;
    cout << "1-100, how smart?" << endl;
    cin >> girl1.smart;
    cout << "1-100, how crazy?" << endl;
    cin >> girl1.crazy;
    
        girl1.total = (girl1.pretty + girl1.smart + girl1.crazy)/3;
    
    
    cout << "\nEnter the name of girl two: ";
    cin.ignore();
    getline(cin, girl2.name);
    cout << "1-100, how pretty?" << endl;
    cin >> girl2.pretty;
    cout << "1-100, how smart?" << endl;
    cin >> girl2.smart;
    cout << "1-100, how crazy?" << endl;
    cin >> girl2.crazy;
        
        girl2.total = (girl2.pretty + girl2.smart + girl2.crazy)/3;
    
    
    cout << "\nEnter the name of girl three: ";
    cin.ignore();
    getline(cin, girl3.name);
        
    cout << "1-100, how pretty?" << endl;
    cin >> girl3.pretty;
    cout << "1-100, how smart?" << endl;
    cin >> girl3.smart;
    cout << "1-100, how crazy?" << endl;
    cin >> girl3.crazy;
    
        girl3.total = (girl3.pretty + girl3.smart + girl3.crazy)/3;
        
    
    
    //checks for winner
        if(girl1.total == girl2.total && girl2.total == girl3.total)
        {cout << "\nMarry any of them, same scores!" << endl;}
        else if (girl1.total > girl2.total && girl1.total > girl3.total)
        {cout << "\nMarry girl one!" << endl;}
        else if( girl1.total < girl2.total && girl2.total > girl3.total)
        {cout << "\nMarry girl two!" << endl;}
        else if(girl3.total > girl1.total && girl3.total > girl2.total)
        {cout << "\nMarry girl three!" << endl;}
        
    
    //print stats
        cout << "\nHere are the stats:" << endl << endl;
        
        cout << girl1.name << endl;
        cout << "Pretty: " << girl1.pretty << endl;
        cout << "Smart: " << girl1.smart << endl;
        cout << "Crazy: " << girl1.crazy << endl;
        cout << "Total: " << girl1.total << endl << endl;
        
        cout << girl2.name << endl;
        cout << "Pretty: " << girl2.pretty << endl;
        cout << "Smart: " << girl2.smart << endl;
        cout << "Crazy: " << girl2.crazy << endl;
        cout << "Total: " << girl2.total << endl << endl;
        
        cout << girl3.name << endl;
        cout << "Pretty: " << girl3.pretty << endl;
        cout << "Smart: " << girl3.smart << endl;
        cout << "Crazy: " << girl3.crazy << endl;
        cout << "Total: " << girl3.total << endl;
    }
    else if(numgirls >=2)     //for two girls
            {
            cout << "\nEnter the name of girl one: ";
            cin.ignore();
            getline(cin, girl1.name);
                
            cout << "1-100, how pretty?" << endl;
            cin >> girl1.pretty;
            cout << "1-100, how smart?" << endl;
            cin >> girl1.smart;
            cout << "1-100, how crazy?" << endl;
            cin >> girl1.crazy;
            
                girl1.total = (girl1.pretty + girl1.smart + girl1.crazy)/3;
            
            
            cout << "\nEnter the name of girl two: ";
            cin.ignore();
            getline(cin, girl2.name);
                
            cout << "1-100, how pretty?" << endl;
            cin >> girl2.pretty;
            cout << "1-100, how smart?" << endl;
            cin >> girl2.smart;
            cout << "1-100, how crazy?" << endl;
            cin >> girl2.crazy;
            
                girl2.total = (girl2.pretty + girl2.smart + girl2.crazy)/3;
            
            //check for winner
                if(girl1.total == girl2.total)
                {cout << "Marry either of them!" << endl;}
                else if(girl1.total > girl2.total)
                {cout << "Marry girl one!" << endl;}
                else if(girl1.total < girl2.total)
                {cout << "Marry girl two!" << endl;}
                
                
            // prints stats
                cout << "\nHere are the stats:" << endl << endl;
                cout << girl1.name << endl;
                cout << "Pretty: " << girl1.pretty << endl;
                cout << "Smart: " << girl1.smart << endl;
                cout << "Crazy: " << girl1.crazy << endl;
                cout << "Total: " << girl1.total << endl << endl;
                
                cout << girl2.name << endl;
                cout << "Pretty: " << girl2.pretty << endl;
                cout << "Smart: " << girl2.smart << endl;
                cout << "Crazy: " << girl2.crazy << endl;
                cout << "Total: " << girl2.total << endl;
            }
    
        return 0;
}



Best is to use a std::vector and a for loop.
http://www.cplusplus.com/reference/vector/vector/
Looks like I am way ahead of my self. Vectors are not covered until well into next semester. Thanks!
What about arrays? Have you used those?
You can try array.
The easiest solution would be set a maximum number of girls allowed in your program, and use that number to create an array.

Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#define NUM_MAX_GIRLS 100

int main(void)
{
    girlscore girls[NUM_MAX_GIRLS];
    int num_girls;

    cout << "How many girls?" << endl;
    cin >> num_girls;

    if (num_girls > NUM_MAX_GIRLS)
    {
        cout << "Too many girls!" << endl;
        return 0;
    }

    for (int n = 0; n < num_girls; n++)
    {
        // TODO: gather the girl rating here
    }

    // other logic to find the best girl or something else
}
Last edited on
You could dynamically allocate an array. No limit (besides the maximum number integral types can hold).

Example:

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

int main()
{
    std::cout << "How many girls do you want to rate? - " << std::flush;
    unsigned short num_girls = 0;
    std::cin >> num_girls;
    girlscore *girls = new girlscore[num_girls];

    return 0;
}

The elements can be accessed with girls[index].
Last edited on
Liuyang, yes! I understand your answer the most from my beginners standpoint. Thanks for the tip!
Im completely lost down a rabbit hole of pointers, structures, and arrays. Could someone help me a tad farther.

I am trying to figure out a way to have the use choose a number or girls and store each of their structures where I can call them separately.

I am trying to work out a way with dynamic memory as state above. Having trouble calling them.
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
#include <iostream>

struct Girl {
	int pretty;
	int smart;
	int crazy;
	double total;
};

int main() {

	int number_of_girls = 0;
	const int max_number_of_girls = 5;

	while ((number_of_girls <= 0) || (number_of_girls > max_number_of_girls)) {
		std::cout << "Enter the number of girls: ";
		std::cin >> number_of_girls;
	}

	Girl* girls = new Girl[number_of_girls];

	for (int i = 0; i < number_of_girls; ++i) {

		std::cout << "How pretty is girl #" << (i + 1) << ": ";
		std::cin >> girls[i].pretty;
		
		std::cout << "How smart is girl #" << (i + 1) << ": ";
		std::cin >> girls[i].smart;

		std::cout << "How crazy is girl #" << (i + 1) << ": ";
		std::cin >> girls[i].crazy;

		girls[i].total = (girls[i].pretty + girls[i].smart + girls[i].crazy) / 3.0;

		std::cout << std::endl;
	}

	for (int i = 0; i < number_of_girls; ++i) {

		std::cout << "Girl #" << (i + 1) << " (total): " << girls[i].total << std::endl;

	}

	delete[] girls;
	girls = nullptr;

	return 0;
}
Last edited on
xidmn's code shows you how to use dynamically allocated array.
basically there're not too many differences between pointer and array.

a slightly modification to xidmn's code would be line 44.
delete[] girls;
Appreciate it guys.
Topic archived. No new replies allowed.