Need Help fixing some bugs in my test score array program

Hey guys I need some help with my program I am getting some errors that I cannot seem to figure out if anybody could help I would appreciate it.

#include <iostream>

using namespace std;
void getScores();
void showMenu();
void getChoice();
void displayResult();

int main()
{ int n, i;
int SIZE = 5;
double testScores[SIZE];

void getScores(double testScores[SIZE]);
{
cout << " Enter 5 Test Scores: "
cin >> testScores[0];
cin >> testScores[1];
cin >> testScores[2];
cin >> testScores[3];
cin >> testScores[4];
cin >> testScores[5];
for( int i = 0; i < 6; i ++)
cout << testScores[SIZE] << "\n";
}
void showMenu();
{ cout << "A.) Calculate the average of the test scores.";
cout << "B.) Display all test scores.";
}
void getChoice(char choice);
cout << "Enter Your Choice:" << "\n";
cin >> choice;
{if( char choice == A or a )
{
cout << "The average of the test scores is:" << displayResult;

}
else if ( char choice == B or b)
{
cout << " Here are all the Test Scores:" << testScores;

}
else (char choice != 'B or b' && char choice != 'A or a')
{

cout << " Invaild Entry!"

}
}

void displayResult();
{
displayResult = (testScores[1] + testScores[2] + testScores[3] + testScores[4] + testScores[5]) /5;

}




}

============================================================================
These are the errors I am getting:


test.cpp:16:36: error: expected ';' after expression
cout << " Enter 5 Test Scores: "
^
;
test.cpp:32:9: error: use of undeclared identifier 'choice'
cin >> choice;
^
test.cpp:33:18: error: invalid '==' at end of declaration; did you mean '='?
{if( char choice == A or a )
^~
=
test.cpp:33:21: error: use of undeclared identifier 'A'
{if( char choice == A or a )
^
test.cpp:33:26: error: use of undeclared identifier 'a'
{if( char choice == A or a )
^
test.cpp:35:51: warning: address of function 'displayResult' will always
evaluate to 'true' [-Wpointer-bool-conversion]
cout << "The average of the test scores is:" << displayResult;
~~ ^~~~~~~~~~~~~
test.cpp:35:51: note: prefix with the address-of operator to silence this
warning
cout << "The average of the test scores is:" << displayResult;
^
&
test.cpp:38:24: error: invalid '==' at end of declaration; did you mean '='?
else if ( char choice == B or b)
^~
=
test.cpp:38:27: error: use of undeclared identifier 'B'
else if ( char choice == B or b)
^
test.cpp:38:32: error: use of undeclared identifier 'b'
else if ( char choice == B or b)
^
test.cpp:43:13: error: expected ')'
else (char choice != 'B or b' && char choice != 'A or a')
^
test.cpp:43:7: note: to match this '('
else (char choice != 'B or b' && char choice != 'A or a')
^
test.cpp:48:3: error: expected ';' after expression
}
^
;
test.cpp:53:16: error: non-object type 'void ()' is not assignable
displayResult = (testScores[1] + testScores[2] + testScores[3] ...
~~~~~~~~~~~~~ ^
There are too many errors in that code, it doesn't just need debugging, it needs untangling.

My suggestion. Start with just main() and one other function. Compile and test that. Then add the other functions one by one. Keep compiling and testing after each change.

See the example under Arrays as parameters in the tutorial:
http://www.cplusplus.com/doc/tutorial/arrays/

Also please use code tags to make your code legible.
http://www.cplusplus.com/articles/jEywvCM9/
Okay I redid my code but I am stuck with the errors it keeps telling me that i have not declared what Choice is, but i thought I did.


#include <iostream>

using namespace std;
void getScores();
void showMenu();
char getChoice();
double calcAvg( double testScores[ ], double avgScore, int SIZE);
void displayResult(double);

int main()
{ const int SIZE = 5;
double testScores[SIZE];
getScores();
showMenu();
char choice = getChoice();
avgScore = calcAvg();
displayResult(avgScore);

return 0;
}

void getScores()
{ int x;
double testScores[SIZE];
cout << " Enter 5 Test Scores: \n ";
for( int x = 0; x < 5; x++)
{
cin >> testScores[x];
}
for (int x = 0; x < 5; x++)
{
cout << testScores[x];

}
}
void showMenu()
{
cout << "A.) Calculate the average of the test scores.";
cout << "B.) Display all test scores.";

}

char getChoice()
{ char = choice;
cout << "Enter your choice : \n";
cin >> choice;
while( choice != 'A' || choice != 'a'|| choice != 'B'|| choice != 'b')
{
getChoice;
}
if(choice == 'A'|| choice =='a')
{
cout << displayResult;
}
if (choice == 'B' || choice == 'b')
{
cout << getScores;

} else ( choice != 'A'|| choice != 'a' || choice != 'B' || choice != 'b')
{
cout << "Invalid Entry!";
}

}

double calcAvg(double testScores[ ])
{ double avgScore = 0.0;
for(int x = 0; x < 5; x++)
{
avgScore += testScores[x];
}
return avgScore / (double)testScores;
}

void displayResult()
{
cout << "The average is:" << calcAvg << endl;
}
return 0;
}
The function char getChoice() , as all non-class functions, can see three kinds of variables:

Global variables. You don't have any of these, which is good.
Variables passed in as parameters. You also don't have any of these.
Variables created inside the function. You also don't have any of these.

char = choice; So what is choice? As far as this function is concerned, no such variable exists.

Also, char = choice; ? What? char is a kind of variable you can create. You can't assign choice to char; it makes no sense. What are you trying to do?

Did you mean char choice; ?
Last edited on
Yea that was my fault I totally looked over that. I fixed that now im down these weird errors i cant seemed to figure out.

test.cpp:16:2: error: use of undeclared identifier 'avgScore'
avgScore = calcAvg();
^
test.cpp:16:13: error: no matching function for call to 'calcAvg'
avgScore = calcAvg();
^~~~~~~
test.cpp:7:8: note: candidate function not viable: requires 3 arguments, but 0
were provided
double calcAvg( double testScores[ ], double avgScore, int SIZE);
^
test.cpp:17:17: error: use of undeclared identifier 'avgScore'
displayResult(avgScore);
^
test.cpp:24:22: error: use of undeclared identifier 'SIZE'
double testScores[SIZE];
^
test.cpp:49:3: warning: expression result unused [-Wunused-value]
getChoice;
^~~~~~~~~
test.cpp:53:11: warning: address of function 'displayResult' will always
evaluate to 'true' [-Wpointer-bool-conversion]
cout << displayResult;
~~ ^~~~~~~~~~~~~
test.cpp:53:11: note: prefix with the address-of operator to silence this
warning
cout << displayResult;
^
&
test.cpp:57:11: warning: address of function 'getScores' will always evaluate to
'true' [-Wpointer-bool-conversion]
cout << getScores;
~~ ^~~~~~~~~
test.cpp:57:11: note: prefix with the address-of operator to silence this
warning
cout << getScores;
^
&
test.cpp:59:75: error: expected ';' after expression
...else ( choice != 'A'|| choice != 'a' || choice != 'B' || choice != 'b')
^
;
test.cpp:59:58: warning: expression result unused [-Wunused-value]
...else ( choice != 'A'|| choice != 'a' || choice != 'B' || choice != 'b')
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~
test.cpp:72:21: error: C-style cast from 'double *' to 'double' is not allowed
return avgScore / (double)testScores;
^~~~~~~~~~~~~~~~~~
test.cpp:77:32: error: reference to overloaded function could not be resolved;
did you mean to call it?
cout << "The average is:" << calcAvg << endl;
^~~~~~~
test.cpp:66:8: note: possible target for call
double calcAvg(double testScores[ ])
^
test.cpp:7:8: note: possible target for call
double calcAvg( double testScores[ ], double avgScore, int SIZE);
Let's look at what is done well, and how that good start can be built upon. The main() function:
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{ 
    const int SIZE = 5;
    double testScores[SIZE];
    getScores();
    showMenu();
    char choice = getChoice();
    avgScore = calcAvg();
    displayResult(avgScore);

    return 0;
}


Thes two lines are really good, a solid basis from which to proceed:
1
2
    const int SIZE = 5;
    double testScores[SIZE];

Now, having specified that SIZE has the value 5, that should be the one and only place in the entire program where the number 5 appears.

How can you do that?
Well, take function getScores()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void getScores()
{ 
    int x;
    double testScores[SIZE];
    cout << " Enter 5 Test Scores: \n ";
    for ( int x = 0; x < 5; x++)
    {
        cin >> testScores[x];
    }
    
    for (int x = 0; x < 5; x++)
    {
        cout << testScores[x];
    }
}


As it stands, that is broken in a number of ways.
1. it doesn't use the original array defined in main()
2. SIZE is used but it is out of scope there.
3. the number 5 is used unnecessarily.

Now try it 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
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

using namespace std;

void getScores(double testScores[], int arraysize);


int main()
{ 
    const int SIZE = 5;
    double testScores[SIZE];
    
    getScores(testScores, SIZE);
    
}


void getScores(double testScores[], int arraysize)
{ 
    cout << " Enter " << arraysize << " Test Scores:\n";
    
    for (int x = 0; x < arraysize; x++)
    {
        cin >> testScores[x];
    }
    
    // following code is not really needed but it helps with testing 
    for (int x = 0; x < arraysize; x++)
    {
        cout << testScores[x] << "  ";
    }
    cout << '\n';
}


Now gradually add back in the other functions - testing as you go along.

In the char get choice () function is it okay as a char function or should it be a void function?
I think it is ok for it to return a char, as at present. Though I think the function (the most recent version I looked at) is trying to do too much. It needs to return a char, which should have the value 'A' or 'B'. Then main() can take that char value and use some logic to decide what to do.
Last edited on
What if I move while and if loops into the displayresult function and just have the char getchoice function get a A or B from the user
What if I move while and if loops into the displayresult function

It depends on the requirements. If you are working on a particular assignment or trying to give a solution to a particular question, then there should be some guidance in the original problem statement.

Otherwise, if you're just improvising and experimenting on your own, then to some extent the choice is yours. Though I'd tend to try to put the controlling logic inside main() and keep each of the other functions small and just doing one single task.
It's for an assignment I'm doing. I do see now that displayresult function should have the results for the get choice function and get choice should just be the user inputting A or B
Topic archived. No new replies allowed.