Test Scores using 1 do while and the rest For loops

I have the basics of it working but I can't figure out how to make the -1 to quit not be displayed and stop the loop from going to the MaxSize(10) with random numbers after the -1.

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
# include <iostream>
# include <iomanip>
using namespace std;

//Constant Identifiers
const int MaxSize = 10;
const int Index = -1;

//Function Prototypes
void GetScores (int TestScoresf[], int Indexf);
void ComputeAvg(int TestScoresf[], int Indexf, float &Avgf);
void PrintReport(int TestScoresf[],int Indexf, float Avgf);

// main body

int main()
{
int TestScores[MaxSize];

float Avg;

GetScores (TestScores, Index);
ComputeAvg (TestScores, Index, Avg);
PrintReport (TestScores, Index, Avg);

system ("PAUSE");
return 0;
}

void GetScores(int TestScoresf[],int Indexf)
{
   do
   {
      Indexf++;             
      cout<< "Please Input Test #" << Indexf + 1 << ": <-1 to exit> \t";
      cin >> TestScoresf [Indexf];
   }while ((TestScoresf [Indexf] >= 0) && (Indexf < MaxSize - 1));
   
   return;
}
void ComputeAvg(int TestScoresf[],int Indexf, float &Avgf)
{
   int Sum = 0;
    for (int Indexf = 0; Indexf < MaxSize; Indexf++)
    {
      
      Sum += TestScoresf[Indexf];
      
      Avgf = float (Sum) / MaxSize;
    }
   
   return;
}

void PrintReport (int TestScoresf[],int Indexf, float Avgf)
{
   cout << "\n\n";
   cout << setw(48)<< "Test Scores Report";
   cout << "\n\n";
   cout << setw(28)<< "Test Number" << setw(29) << "Test Score"<<endl;
   
   for (int Indexf = 0; Indexf < MaxSize; Indexf++)
   { 
      
      cout<< setw(18)<<Indexf + 1<<setw(31)<<TestScoresf[Indexf]<<endl;
      cout<<endl<<fixed<<showpoint<<setprecision(2);
      
   }
   
   cout<<"\t\t Class Average: "<<Avgf<<endl;
   return; 
}
Last edited on
You're getting the "random" numbers because they aren't ever initialized. You could just add extra code after your do-while loop to set the uninitialized values and to erase the -1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void GetScores(int TestScoresf[],int Indexf)
{
   do
   {
      Indexf++;             
      cout<< "Please Input Test #" << Indexf + 1 << ": <-1 to exit> \t";
      cin >> TestScoresf [Indexf];
   }while ((TestScoresf [Indexf] >= 0) && (Indexf < MaxSize - 1));

   while(Indexf < MaxSize)
      TestScoresf[Indexf++] = 0;   
  
   return;
}


Note that when you compute your average, you still look at the uninitialized (now zero'd) values. That's wrong and will skew your result. I suggest using a specific number that you know is invalid to flag the end of your loops:
const int InvalidFlag = -2;

Then to set your values:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void GetScores(int TestScoresf[],int Indexf)
{
   do
   {
      Indexf++;             
      cout<< "Please Input Test #" << Indexf + 1 << ": <-1 to exit> \t";
      cin >> TestScoresf [Indexf];
   }while ((TestScoresf [Indexf] >= 0) && (Indexf < MaxSize - 1));

   while(Indexf < MaxSize)
      TestScoresf[Indexf++] = InvalidFlag;   // Invalid value
  
   return;
}


And to compute the average just change the FOR loop conditions:
1
2
3
4
5
6
7
8
9
10
11
12
13
void ComputeAvg(int TestScoresf[],int Indexf, float &Avgf)
{
   int Sum = 0;
    for (int Indexf = 0; Indexf < MaxSize; Indexf++)
    {
      if (TestScoresf[Indexf] == InvalidFlag) break; // Another exit condition
      Sum += TestScoresf[Indexf];
      
      Avgf = float (Sum) / MaxSize;
    }
   
   return;
}
Last edited on
Thank you for helping me with this. I Figured it out using another program that was similar to the one I need to complete.
Here it is complete:

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
#include <iostream>
#include <iomanip>
 
using namespace std;
 
//function prototypes
 
void GetData (int TestScoresf [], int &Indexf);
void ComputeAvg(int TestScoresf[],int Indexf, float &Avgf);
void PrintReport (int TestScoref [], int Indexf, float Avgf);
 
//constant identifier
 
const int MaxSize = 10;
 
//Main body
 
int main()
 {
 //local identifiers
 int TestScores [MaxSize];
 int Index;
 float Avg;
 
//function calls
 GetData (TestScores, Index);
 ComputeAvg (TestScores, Index,  Avg);
 PrintReport (TestScores, Index, Avg);
 
system("pause");
 return 0;
 
} //end main
 
//function deffinitions
 
void GetData (int TestScoresf [], int &Index)
 {
 int Indexf = -1;
 do
 {
 Indexf++;
 cout<<"Please enter test #"<<Indexf + 1<<": <-1 to exit>\t";
 cin>>TestScoresf[Indexf];
 Index=Indexf;
 } while ((TestScoresf[Indexf] >= 0) && (Indexf < MaxSize - 1));
 
return;
 } //end of GetData
 
void ComputeAvg(int TestScoresf[],int Indexf, float &Avgf)
 {
 
 int sum = 0;
 


for (int i=0; i < Indexf; i++)
 {
 sum += TestScoresf[i];
 }
 Avgf = float (sum) / Indexf;
 return;
 } //end ComputeAvg
 
void PrintReport (int TestScoref [], int Indexf, float Avgf)
 {
   cout << "\n\n";
   cout << setw(48)<< "Test Scores Report";
   cout << "\n\n";
   cout << setw(28)<< "Test Number" << setw(29) << "Test Score"<<endl;
 

for(int i=0;i<Indexf;i++)
 {
 cout<< setw(18)<<Indexf + 1<<setw(31)<<TestScoref[i]<<endl;
      cout<<endl<<fixed<<showpoint<<setprecision(2);
 }
 
cout<<"Class average = "<<Avgf<<endl;
 
return;
 
Last edited on
Topic archived. No new replies allowed.