hey guys, I've seemingly exhausted all the possibilities within my limited knowledge, so I'm coming here for some assistance.

heres the problem:

Read the 20 students info into an array, assign the relevant grade, find the highest score, and print out all the names/scores in addition to the student/s of the highest score.

The student names should be outputted in this format:

Lazaar, John [grade]

And the student names must be left justified.

Thanks for even giving this a look, and thanks x1000000 for taking the time to help!!!


the plentiful errors are below the code, and are hopefully obvious stuff to you guys lol.

also one last thing, does anyone here with osx know what the file path would be if a file was on the desktop? This problem originally had to do with reading a file, but I spent hours on that thing and couldn't get my file read...

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

struct studentType
{

string studentFName;
string studentLName;
int testScore;
char grade;

}

void Data (studentType student[]);
void Grade (studentType student[]);
int high (studentType student[]);
void print(studentType student[],int highest);

int main()
{

studentType student [20];
Data(student[]);
void Grade (student[]);
int high (student[]);
void print(student[], int highest);


return 0;


void Data(studentType student[])
{
for (int x = 0; x < 20; x++)
        {
        cout << "Enter the student's Last Name: ";
        cin >> student[x].studentLName;
        cout << endl;

        cout << "Enter the student's First Name: ";
        cin >> student[x].studentFName;
        cout << endl;

        cout << "Enter the student's grade: ";
        cin >> student[x].testScore;
        cout << endl;   
        }
}

void Grade(studentType student[])
{
        for (int x = 0; x < 20; x++)
        {
                if (student[x].testScore >= 90) 
                        student[x].grade = 'A';
                        else if ( student[x].testScore >= 80 )
                        student[x].grade = 'B';
                        else if ( student[x].testScore >= 70 )
                        student[x].grade = 'C';
                        else if (student[x].testScore >= 60 )
                        student[x].grade = 'D';
                else 
                student[x].grade = 'F';
        }
}

int high (studentType student[])
{

int highest = student[0].testScore;

for (int x = 0; x < 20; x++)
{
        if (student[x].testScore > highest)
                highest = student[x].testScore;  

}
return highest;          
}

void printResult(student[], highest)
{

cout << setw(20) << "Student Name" 
cout << setw(15) << "Score" 
cout << setw(10) << "Grade" << endl;

for (int x = 0; x < 20; x++)
{
cout << left << student[x].studentLName << ", " << 
student[x].studentFName << endl; 
}
 
cout << "The highest score is: " <<  highest << "." << endl;
cout << "The students with the highest scores are: " << endl; 
for (int x = 0; x < 20; x++)
{
        if (student[x].testScore == highest)

cout << left << student[x].studentLName << ", " << student[x].studentFName << 
endl;     
}
}

aatest.cpp:17: error: new types may not be defined in a return type
aatest.cpp:17: note: (perhaps a semicolon is missing after the definition of 'studentType')
aatest.cpp:17: error: two or more data types in declaration of 'Data'
aatest.cpp: In function 'int main()':
aatest.cpp:26: error: expected primary-expression before ']' token
aatest.cpp:27: error: variable or field 'Grade' declared void
aatest.cpp:27: error: expected primary-expression before ']' token
aatest.cpp:28: error: expected primary-expression before ']' token
aatest.cpp:29: error: variable or field 'print' declared void
aatest.cpp:29: error: expected primary-expression before ']' token
aatest.cpp:29: error: expected primary-expression before 'int'
aatest.cpp:29: error: initializer expression list treated as compound expression
aatest.cpp:36: error: a function-definition is not allowed here before '{' token
aatest.cpp:54: error: a function-definition is not allowed here before '{' token
aatest.cpp:71: error: a function-definition is not allowed here before '{' token
aatest.cpp:84: error: variable or field 'printResult' declared void
aatest.cpp:84: error: expected primary-expression before ']' token
aatest.cpp:84: error: 'highest' was not declared in this scope
aatest.cpp:84: error: initializer expression list treated as compound expression
aatest.cpp:85: error: expected ',' or ';' before '{' token
aatest.cpp:106: error: expected `}' at end of input

Recommended Answers

All 9 Replies

put a semicolon after the closing bracket of the declaration of the struct and recompile.

More importantly, never, ever, write the whole program and then hit the compile button. You're asking for a pain in the neck. Write a single class method or nonclass function one at a time and then compile. If declaring a user defined type always compile after writing the declaration. If not using functions always compile after writing just a few lines. It may take longer to write, but it's way easier to debug!

commented: thanks for the help man!!! +1

thanks for the response. That leaves me with

aatest.cpp: In function 'int main()':
aatest.cpp:26: error: expected primary-expression before ']' token
aatest.cpp:27: error: variable or field 'Grade' declared void
aatest.cpp:27: error: expected primary-expression before ']' token
aatest.cpp:28: error: expected primary-expression before ']' token
aatest.cpp:29: error: variable or field 'print' declared void
aatest.cpp:29: error: expected primary-expression before ']' token
aatest.cpp:29: error: expected primary-expression before 'int'
aatest.cpp:29: error: initializer expression list treated as compound expression
aatest.cpp:36: error: a function-definition is not allowed here before '{' token
aatest.cpp:54: error: a function-definition is not allowed here before '{' token
aatest.cpp:71: error: a function-definition is not allowed here before '{' token
aatest.cpp:84: error: variable or field 'printResult' declared void
aatest.cpp:84: error: expected primary-expression before ']' token
aatest.cpp:84: error: 'highest' was not declared in this scope
aatest.cpp:84: error: initializer expression list treated as compound expression
aatest.cpp:85: error: expected ',' or ';' before '{' token
aatest.cpp:106: error: expected `}' at end of input


Still a ton of stuff about function declarations and expected expressions. This code looks so legit to me...

yeah testing each function makes sense, but I always thought it would take ages since you would have to create new code that you wouldn't even use in the first place to test it. Thanks for the help man.

When passing arrays to function, just pass the name of the array. So drop the [] in this line in main().

Data(student[]);


Then review how to call functions. These:

void Grade (student[]);
int high (student[]);
void print(student[], int highest);

are function prototypes, not function calls, and as such wouldn't normally be found in the body of main().

EDIT: And how long is it taking you (me?) to debug it!

I put print result for some reason for one of the functions so I changed that, and I made my parameters consistent with adding studentType. My earlier debugging influenced the removal of that, so I guess it was a different reason for that particular error.

heres the problems at present.

aaaaa.cpp: In function 'int main()':
aaaaa.cpp:26: error: expected primary-expression before 'student'
aaaaa.cpp:36: error: a function-definition is not allowed here before '{' token
aaaaa.cpp:54: error: a function-definition is not allowed here before '{' token
aaaaa.cpp:71: error: a function-definition is not allowed here before '{' token
aaaaa.cpp:84: error: variable or field 'print' declared void
aaaaa.cpp:84: error: 'int print' redeclared as different kind of symbol
aaaaa.cpp:29: error: previous declaration of 'void print(studentType*, int)'
aaaaa.cpp:84: error: expected primary-expression before ']' token
aaaaa.cpp:84: error: 'highest' was not declared in this scope
aaaaa.cpp:106: error: expected `}' at end of input

lol sorry man, and I spent wayyyyy more time on this, for better or worse....And I'll review how to call functions again, but I was pretty sure the above would work from past experience.

never mind...I don't know how I missed that..

Grade (student[]);
high (student[]);
print(student[], int highest);

is the correct form.

Post updated code with all functions and associated code removed except for the declaration of your struct and the first two lines of main() with the associated declaration and definition of the function being called.

Also, if you would have written your code one step at a time you would have noticed you need a closing curly brace after the return line in main(). It really works, man. You should give it a try. For now I've got to get some shut eye to be functional at work tomorrow. Good luck.

heres the updated code in its entirety:

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

struct studentType
{

string studentFName;
string studentLName;
int testScore;
char grade;

};

void Data (studentType student[]);
void Grade (studentType student[]);
int high (studentType student[]);
void print(studentType student[],int highest);

int main()
{

studentType student [20];
Data(studentType student);
Grade (studentType student);
high (studentType student);
print(studentType student, int highest);


return 0;

}
void Data(studentType student[])
{
for (int x = 0; x < 20; x++)
        {
        cout << "Enter the student's Last Name: ";
        cin >> student[x].studentLName;
        cout << endl;

        cout << "Enter the student's First Name: ";
        cin >> student[x].studentFName;
        cout << endl;

        cout << "Enter the student's grade: ";
        cin >> student[x].testScore;
        cout << endl;   
        }
}

void Grade(studentType student[])
{
        for (int x = 0; x < 20; x++)
        {
                if (student[x].testScore >= 90) 
                        student[x].grade = 'A';
                        else if ( student[x].testScore >= 80 )
                        student[x].grade = 'B';
                        else if ( student[x].testScore >= 70 )
                        student[x].grade = 'C';
                        else if (student[x].testScore >= 60 )
                        student[x].grade = 'D';
                else 
                student[x].grade = 'F';
        }
}

int high (studentType student[])
{

int highest = student[0].testScore;

for (int x = 0; x < 20; x++)
{
        if (student[x].testScore > highest)
                highest = student[x].testScore;  

}
return highest;          
}

void print(student[], highest)
{

cout << setw(20) << "Student Name" 
cout << setw(15) << "Score" 
cout << setw(10) << "Grade" << endl;

for (int x = 0; x < 20; x++)
{
cout << left << student[x].studentLName << ", " << 
student[x].studentFName << endl; 
}
 
cout << "The highest score is: " <<  highest << "." << endl;
cout << "The students with the highest scores are: " << endl; 
for (int x = 0; x < 20; x++)
{
        if (student[x].testScore == highest)

cout << left << student[x].studentLName << ", " << student[x].studentFName << 
endl;     
}
}

and the snippets you wanted

struct studentType
{

string studentFName;
string studentLName;
int testScore;
char grade;

};

int main()
{

studentType student [20];
Data(studentType student);
Grade (studentType student);
high (studentType student);
print(studentType student, int highest);


return 0;

and updated problems

aatest.cpp:26: error: expected primary-expression before 'student'
aatest.cpp:27: error: expected primary-expression before 'student'
aatest.cpp:28: error: expected primary-expression before 'student'
aatest.cpp:29: error: expected primary-expression before 'student'
aatest.cpp:29: error: expected primary-expression before 'int'
aatest.cpp: At global scope:
aatest.cpp:84: error: variable or field 'print' declared void
aatest.cpp:84: error: 'int print' redeclared as different kind of symbol
aatest.cpp:20: error: previous declaration of 'void print(studentType*, int)'
aatest.cpp:84: error: 'student' was not declared in this scope
aatest.cpp:84: error: expected primary-expression before ']' token
aatest.cpp:84: error: 'highest' was not declared in this scope

Data(studentType student);
Grade (studentType student);
high (studentType student);
print(studentType student, int highest);

Again, look up how to do a function call. All of the above are wrong. Don't include data types at any point in a function call. If you write just one function at a time and then try to correct you will learn from your mistakes and not perpetuate the same mistake five times over in the same program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.