need help with output on assignment

Object oriented C++ course. Still learning the ways. This is the assignment http://venus.cs.qc.edu/~waxman/211/Score%20the%20race%20211.pdf

I am having trouble with the output. So far I was trying the first two outputs (number of teams and number of runners on each team) so I haven't gotten to the other two outputs. I run the program and see that: 1. no matter what characters or how many I put, it doesn't change the calculations/output (number of teams or runners) and 2. something must be wrong with the while loop because the input repeats itself several times. The assignment is already almost 1 week late. Please help!

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
161
162
163
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

bool validRace(int team[], int RPT) //confirming equal amount of runners per team
{
     for (int i = 0; i < 26; i++)
     {
           if (team[i] != 0 && team[i] != RPT) return false;
     }
     return true;
}

int main()
{
     string input;
     int team[26] = {};
     int score[26] = {};
      
     cout << "Enter racers: ";
     cin >> input;
     
     while (input != "done")
     }

          for (int i = 0; i < input.length(); i++)
          {
	            switch(input[i])
                    {
                    case 'A': team[0]++;
                              score[0] += i + 1;
                              break;
            
                    case 'B': team[1]++;
                              score[1] += i + 1;
                              break;

                    case 'C': team[2]++;
                              score[2] += i + 1;
                              break;

                    case 'D': team[3]++;
                              score[3] += i + 1;
                              break;

                    case 'E': team[4]++;
                              score[4] += i + 1;
                              break;

                    case 'F': team[5]++;
                              score[5] += i + 1;
                              break;

		    case 'G': team[6]++;                  
                              score[6] += i + 1;
  			      break;

		    case 'H': team[7]++;
 			      score[7] += i + 1;
 			      break;

		    case 'I': team[8]++;
		              score[8] += i + 1;
			      break;

		    case 'J': team[9]++;
  			      score[9] += i + 1;
			      break;

	  	    case 'K': team[10]++;
			      score[10] += i + 1;
			      break;

		    case 'L' :team[11]++;
			      score[11] += i + 1;
			      break;

		    case 'M': team[12]++;
 			      score[12] += i + 1;
			      break;

		    case 'N': team[13]++;
			      score[13] += i + 1;
			      break;

		    case 'O': team[14]++;
			      score[14] += i + 1;
			      break;

		    case 'P': team[15]++;
			      score[15] += i + 1;
			      break;

		    case 'Q': team[16]++;
			      score[16] += i + 1;
			      break;

		    case 'R': team[17]++;
			      score[17] += i + 1;
			      break;

	 	    case 'S': team[18]++;
			      score[18] += i + 1;
			      break;

		    case 'T': team[19]++;
			      score[19] += i + 1;
			      break;

		    case 'U': team[20]++;
			      score[20] += i = 1;
			      break;

		    case 'V': team[21]++;
			      score[21] += i + 1;
			      break;

		    case 'W': team[22]++;
			      score[22] += i + 1;
			      break;

		    case 'X': team[23]++;
			      score[23] += i + 1;
			      break;

		    case 'Y': team[24]++;
			      score[24] += i + 1;
			      break;

		    case 'Z': team[25]++;
			      score[26] += i + 1;
			      break;
                    }
          

          int numTeams = 0;
	  for (int i = 0; i < 26; i++)
	  {
               if (team[i] != 0) numTeams++;
          
	  }

	  int runnersPerTeam = input.length() / numTeams;

          if (validRace(team, RPT)) //if equal amount of runners, outputs information
	  {
	       cout << "There are " << numTeams << " teams" << end
	       cout << "There are " << runnersPerTeam << " runners per team" << endl;
          }   
	
	  } //end of for loop

                   	  
   
     } //end of while loop


     return 0;
}

Last edited on
This is why I can't stand school. Such an impractical and verbose problem.
Give me a sec. I'll help you out.
@sezo

1. Move lines 22 and 23, inside the while statement, to where a line 27 and 28, would be.
2. You have a right parenthesis on line 26 which should be a left one '{'
3. You need another right parenthesis on line 136 and another on 137, to close the i for loop, and the while statement.
4. Without them, you restart the i loop line 139.
5. Remove lines 153 and 157.

This should get your program working.
Last edited on
Are you still working on it?
@handsomejohn I am at work now but going to try what @whitenite1 said to do and see what happens
@handsomejohn sorry for late response, didn't think i'd get such a quick reply passed midnight. Will update in a bit
quick update @here, when attempting an incorrect input, the input repeats about 4 times instead of just inquiring for input once after first error. Secondly, after i enter some characters, it loops the input about 3 times again(just the cout) then ONLY shows correct calculation after typing "done" to exit. Very odd :T
Last edited on
@sezo

It's quite difficult to figure out what the reasons are for why it's not working correctly, when we can't see what changes you've done. Are you typing only capital letters for the input, etc.? Show us the changes, and we'll check it out. Thanks..
@whitenite1 Sorry about that! I just did exactly what you suggested (thanks by the way!) Here is the code.

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

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

bool validRace(int team[], int RPT) //confirming equal amount of runners per team
{
     for (int i = 0; i < 26; i++)
     {
           if (team[i] != 0 && team[i] != RPT) return false;
     }
     return true;
}

int main()
{
     string input;
     int team[26] = {};
     int score[26] = {};
     
     while (input != "done")
     {
         
          cout << "Enter teams in the order they were placed. Each team must have equal amount of runners. 'Done' to exit: ";
          cin >> input;

          for (int i = 0; i < input.length(); i++)
          {
	            switch(input[i])
                    {
                    case 'A': team[0]++;
                              score[0] += i + 1;
                              break;
            
                    case 'B': team[1]++;
                              score[1] += i + 1;
                              break;

                    case 'C': team[2]++;
                              score[2] += i + 1;
                              break;

                    case 'D': team[3]++;
                              score[3] += i + 1;
                              break;

                    case 'E': team[4]++;
                              score[4] += i + 1;
                              break;

                    case 'F': team[5]++;
                              score[5] += i + 1;
                              break;

		    case 'G': team[6]++;                  
                              score[6] += i + 1;
  			      break;

		    case 'H': team[7]++;
 			      score[7] += i + 1;
 			      break;

		    case 'I': team[8]++;
		              score[8] += i + 1;
			      break;

		    case 'J': team[9]++;
  			      score[9] += i + 1;
			      break;

	  	    case 'K': team[10]++;
			      score[10] += i + 1;
			      break;

		    case 'L' :team[11]++;
			      score[11] += i + 1;
			      break;

		    case 'M': team[12]++;
 			      score[12] += i + 1;
			      break;

		    case 'N': team[13]++;
			      score[13] += i + 1;
			      break;

		    case 'O': team[14]++;
			      score[14] += i + 1;
			      break;

		    case 'P': team[15]++;
			      score[15] += i + 1;
			      break;

		    case 'Q': team[16]++;
			      score[16] += i + 1;
			      break;

		    case 'R': team[17]++;
			      score[17] += i + 1;
			      break;

	 	    case 'S': team[18]++;
			      score[18] += i + 1;
			      break;

		    case 'T': team[19]++;
			      score[19] += i + 1;
			      break;

		    case 'U': team[20]++;
			      score[20] += i = 1;
			      break;

		    case 'V': team[21]++;
			      score[21] += i + 1;
			      break;

		    case 'W': team[22]++;
			      score[22] += i + 1;
			      break;

		    case 'X': team[23]++;
			      score[23] += i + 1;
			      break;

		    case 'Y': team[24]++;
			      score[24] += i + 1;
			      break;

		    case 'Z': team[25]++;
			      score[26] += i + 1;
			      break;
                    }
          } //end of for loop
   
    } //end of while loop


          int numTeams = 0;
	  for (int i = 0; i < 26; i++)
	  {
               if (team[i] != 0) numTeams++;
          
	  }

	  int runnersPerTeam = input.length() / numTeams;

          if (validRace(team, runnersPerTeam)) //if equal amount of runners, outputs information
	  {
	       cout << "There are " << numTeams << " teams" << endl;
	       cout << "There are " << runnersPerTeam << " runners per team" << endl;
          }   

     return 0;
}
@sezo

I had to add a little more to your program, but it works now.

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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

bool validRace(int team[], int runnersperteam) //confirming equal amount of runners per team
{
	for (int i = 0; i < 26; i++)
	{
		if (team[i] > 0)
			if(team[i] != runnersperteam) // If any team has fewer or more runners
				return false;
	}
	return true;
}

int main()
{
	string input;
	int len;
	int team[26] = {};
	int score[26] = {};

	while (input != "done")
	{

		cout << "Enter teams in the order they were placed. Each team must have equal amount of runners. 'done' to exit: " << endl;;
		cin >> input;
		len = input.length(); // Get string length for loop
		if(input != "done")
		{
			for (int i = 0; i < len; i++)
			{

				switch(input[i])
				{
				case 'A':
					team[0]++;
					score[0] += i + 1;// Not sure why this is for scoring
					break;
				case 'B': 
					team[1]++;
					score[1] += i + 1;
					break;

				case 'C':
					team[2]++;
					score[2] += i + 1;
					break;

				case 'D':
					team[3]++;
					score[3] += i + 1;
					break;

				case 'E': 
					team[4]++;
					score[4] += i + 1;
					break;

				case 'F':
					team[5]++;
					score[5] += i + 1;
					break;

				case 'G':
					team[6]++;                  
					score[6] += i + 1;
					break;

				case 'H':
					team[7]++;
					score[7] += i + 1;
					break;

				case 'I': 
					team[8]++;
					score[8] += i + 1;
					break;

				case 'J': 
					team[9]++;
					score[9] += i + 1;
					break;

				case 'K':
					team[10]++;
					score[10] += i + 1;
					break;

				case 'L':
					team[11]++;
					score[11] += i + 1;
					break;

				case 'M':
					team[12]++;
					score[12] += i + 1;
					break;

				case 'N':
					team[13]++;
					score[13] += i + 1;
					break;

				case 'O':
					team[14]++;
					score[14] += i + 1;
					break;

				case 'P':
					team[15]++;
					score[15] += i + 1;
					break;

				case 'Q':
					team[16]++;
					score[16] += i + 1;
					break;

				case 'R': 
					team[17]++;
					score[17] += i + 1;
					break;

				case 'S':
					team[18]++;
					score[18] += i + 1;
					break;

				case 'T':
					team[19]++;
					score[19] += i + 1;
					break;

				case 'U':
					team[20]++;
					score[20] += i = 1;
					break;

				case 'V':
					team[21]++;
					score[21] += i + 1;
					break;

				case 'W':
					team[22]++;
					score[22] += i + 1;
					break;

				case 'X':
					team[23]++;
					score[23] += i + 1;
					break;

				case 'Y': team[24]++;
					score[24] += i + 1;
					break;

				case 'Z': 
					team[25]++;
					score[26] += i + 1;
					break;
				}
			}
		} //end of for loop

	} //end of while loop


	int numTeams = 0;
	for (int i = 0; i < 26; i++)
	{
		
		if (team[i] != 0)
		{
			numTeams+=team[i];
		}
	}
	int runnersPerTeam;
	
	for (int i = 0; i < 26; i++)// Check number of runners in first team of players
	{
		if (team[i] != 0)
		{
			runnersPerTeam = team[i];
			i = 26;
		}
	}
	if (validRace(team, runnersPerTeam)) //if equal amount of runners, outputs information
	{
		cout << "There are " << numTeams << " teams" << endl;
		cout << "There are " << runnersPerTeam << " runners per team" << endl;
	} 
	else
		cout << "Not all teams have an equal amount of runners.." << endl;

	return 0;
}
Topic archived. No new replies allowed.