error

R

Richard

I got 2 error in the program code below, but I don't know what they are and
how to fix it. Can anyone put it in compile and tell me what is wrong? I
am new to c++

#include <iostream>
#include <ostream>

using namespace std;

const int StudentNumber=2;

//structure declaration
struct Student
{
char Name[20];
int StudentID;
int TestScores[4];
float AverageTestScore;
char Grade;

}; Student
StudentInfo[StudentNumber];
// funciton phototype declaration
int GetStudentInfo(void);
void CalculateAverage(int &);
void DisplayStudentRecord(void);

int sum[];
void main()
{ //int sum=0;
GetStudentInfo();
CalculateAverage(sum[StudentNumber]);

DisplayStudentRecord();


}


int GetStudentInfo(void)
{

for(int count =0; count < StudentNumber; count++)
{

cout<<"What is the name of Student "<<count+1<<"?: ";
cin>>StudentInfo[count].Name;
cout<<"What is "<<StudentInfo[count].Name<<"'s ID number?: ";
cin>>StudentInfo[count].StudentID;
cout<<"Enter 4 test scores for "<<StudentInfo[count].Name<<":
"<<endl;
for (int count2=0; count2 < 4; count2++)
{
cin>>StudentInfo[count].TestScores[count2];
sum[count] += StudentInfo[count].TestScores[count2];

}

}
return sum;


}
void DisplayStudentRecord(void)
{
cout<<"Student Name\t"<<"StudentID\t"<<"Test Score
Average\t"<<"Grade\t"<<endl<<endl;
for (int count=0; count < StudentNumber; count++)
{

cout<<StudentInfo[count].Name<<"\t\t"<<StudentInfo[count].StudentID<<"\t\t"<<

StudentInfo[count].AverageTestScore<<"\t\t\t"<<StudentInfo[count].Grade<<endl;
}

}

void CalculateAverage(int &sum)
{ for(int count =0; count < StudentNumber; count++)
{
StudentInfo[count].AverageTestScore = float(sum[count])/4;
if (StudentInfo[count].AverageTestScore >= 90 &&
StudentInfo[count].AverageTestScore <=100)
{
StudentInfo[count].Grade = 'A';
}
if (StudentInfo[count].AverageTestScore >= 80 &&
StudentInfo[count].AverageTestScore <=89)
{
StudentInfo[count].Grade = 'B';
}
if (StudentInfo[count].AverageTestScore >= 70 &&
StudentInfo[count].AverageTestScore <=79)
{
StudentInfo[count].Grade = 'C';
}
if (StudentInfo[count].AverageTestScore >= 60 &&
StudentInfo[count].AverageTestScore <=69)
{
StudentInfo[count].Grade = 'D';
}
if (StudentInfo[count].AverageTestScore <59)
{
StudentInfo[count].Grade = 'F';
}


}

}
 
M

Mike Wahler

Richard said:
I got 2 error in the program code below, but I don't know what they are and
how to fix it. Can anyone put it in compile and tell me what is wrong?


IMO the biggest thing wrong is that you're trying
to write way too much code at once. Start much
smaller, testing as you go. Don't add more code
till what you have works correctly.

A couple remarks below (I didn't analyze the whole thing).
I am new to c++

#include <iostream>
#include <ostream>

using namespace std;

const int StudentNumber=2;

//structure declaration
struct Student
{
char Name[20];
int StudentID;
int TestScores[4];
float AverageTestScore;
char Grade;

}; Student StudentInfo[StudentNumber];
// funciton phototype declaration
int GetStudentInfo(void);
void CalculateAverage(int &);
void DisplayStudentRecord(void);

int sum[];

This is invalid. A size for the array must be specified.
You should be using a container (e.g. vector) instead of
an array anyway.
void main()

'main()' *must* return type 'int'. Always.
{ //int sum=0;
GetStudentInfo();
CalculateAverage(sum[StudentNumber]);

DisplayStudentRecord();


}


int GetStudentInfo(void)
{

for(int count =0; count < StudentNumber; count++)
{

cout<<"What is the name of Student "<<count+1<<"?: ";
cin>>StudentInfo[count].Name;
cout<<"What is "<<StudentInfo[count].Name<<"'s ID number?: ";
cin>>StudentInfo[count].StudentID;
cout<<"Enter 4 test scores for "<<StudentInfo[count].Name<<":
"<<endl;
for (int count2=0; count2 < 4; count2++)
{
cin>>StudentInfo[count].TestScores[count2];
sum[count] += StudentInfo[count].TestScores[count2];

}

}
return sum;

Above you've tried to define 'sum' as an array. Here you
try to return it as an 'int'.
}
void DisplayStudentRecord(void)
{
cout<<"Student Name\t"<<"StudentID\t"<<"Test Score
Average\t"<<"Grade\t"<<endl<<endl;
for (int count=0; count < StudentNumber; count++)
{

cout<<StudentInfo[count].Name<<"\t\t"<<StudentInfo[count].StudentID<<"\t\t"<<

StudentInfo[count].AverageTestScore<<"\t\t\t"<<StudentInfo[count].Grade<<endl;
}

}

void CalculateAverage(int &sum)
{ for(int count =0; count < StudentNumber; count++)
{
StudentInfo[count].AverageTestScore = float(sum[count])/4;

'sum' is not an array. The [] operator needs an array (or
some type which defines that operator).
if (StudentInfo[count].AverageTestScore >= 90 &&
StudentInfo[count].AverageTestScore <=100)
{
StudentInfo[count].Grade = 'A';
}
if (StudentInfo[count].AverageTestScore >= 80 &&
StudentInfo[count].AverageTestScore <=89)
{
StudentInfo[count].Grade = 'B';
}
if (StudentInfo[count].AverageTestScore >= 70 &&
StudentInfo[count].AverageTestScore <=79)
{
StudentInfo[count].Grade = 'C';
}
if (StudentInfo[count].AverageTestScore >= 60 &&
StudentInfo[count].AverageTestScore <=69)
{
StudentInfo[count].Grade = 'D';
}
if (StudentInfo[count].AverageTestScore <59)
{
StudentInfo[count].Grade = 'F';
}


}

}

-Mike
 
J

Jonathan Mcdougall

Richard said:
I got 2 error in the program code below, but I don't know what they are and
how to fix it. Can anyone put it in compile and tell me what is wrong? I
am new to c++

Always use spaces, not tabs, when you post to a newsgroup.
#include <iostream>
#include <ostream>

using namespace std;


const int StudentNumber=2;

//structure declaration
struct Student
{
char Name[20];
int StudentID;
int TestScores[4];
float AverageTestScore;
char Grade;
};

Student StudentInfo[StudentNumber];

// funciton phototype declaration
int GetStudentInfo(void);

Don't write void in an empty parameter list, it is redundant.
void CalculateAverage(int &);

Take the habit of writing the parameter name, event in function
declarations.
void DisplayStudentRecord(void);

int sum[];

Illegal, arrays must have a size. Also, don't make it global. Define it
in main() and pass it around. I think you mean

int sum[StudentNumber];
void main()

main() returns an int

http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.3
{
//int sum=0;
GetStudentInfo();
CalculateAverage(sum[StudentNumber]);

This makes no sense and is illegal is sum is defined as above.
DisplayStudentRecord();
}


int GetStudentInfo(void)
{
for(int count =0; count < StudentNumber; count++)

Make that ++count.

http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.15
{
cout<<"What is the name of Student "<<count+1<<"?: ";
cin>>StudentInfo[count].Name;
cout<<"What is "<<StudentInfo[count].Name<<"'s ID number?: ";
cin>>StudentInfo[count].StudentID;
cout<<"Enter 4 test scores for "<<StudentInfo[count].Name<<": "<<endl;
for (int count2=0; count2 < 4; count2++)

cout2++

Also, don't use names like count and count2. Either given them
descriptive names (student_count and score_count) or single letter
names (i and j).
{
cin>>StudentInfo[count].TestScores[count2];
sum[count] += StudentInfo[count].TestScores[count2];
}
}

return sum;

That's illegal. GetStudentInfo() returns an int and sum is an array of
ints. What do you want that function to return exactly?
}

void DisplayStudentRecord(void)
{
cout<<"Student Name\t"<<"StudentID\t"<<"Test Score
Average\t"<<"Grade\t"<<endl<<endl;
for (int count=0; count < StudentNumber; count++)
{
cout<<StudentInfo[count].Name<<"\t\t"<<StudentInfo[count].StudentID<<"\t\t"<<
StudentInfo[count].AverageTestScore<<"\t\t\t"<<StudentInfo[count].Grade<<endl;

Break your lines!

cout << StudentInfo[count].Name << "\t\t"
<< StudentInfo[count].StudentID << "\t\t"
<< StudentInfo[count].AverageTestScore << "\t\t\t";
<< StudentInfo[count].Grade << endl;
}
}

void CalculateAverage(int &sum)
{
for(int count =0; count < StudentNumber; count++)
++count

{
StudentInfo[count].AverageTestScore = float(sum[count])/4;
if (StudentInfo[count].AverageTestScore >= 90 &&
StudentInfo[count].AverageTestScore <=100)
{
StudentInfo[count].Grade = 'A';
}

put an else here
if (StudentInfo[count].AverageTestScore >= 80 &&
StudentInfo[count].AverageTestScore <=89)
{
StudentInfo[count].Grade = 'B';
}
if (StudentInfo[count].AverageTestScore >= 70 &&
StudentInfo[count].AverageTestScore <=79)
{
StudentInfo[count].Grade = 'C';
}
if (StudentInfo[count].AverageTestScore >= 60 &&
StudentInfo[count].AverageTestScore <=69)
{
StudentInfo[count].Grade = 'D';
}
if (StudentInfo[count].AverageTestScore <59)

What happens if I get 59?
{
StudentInfo[count].Grade = 'F';
}
}
}

Now, have a look at this.

# include <iostream>
# include <string>

struct StudentInfo
{
std::string name;
int student_id;
int test_scores[4];
float average_test_score;
char grade;
int sum;
};

const int StudentNumber = 2;

void get_student_info(StudentInfo s[StudentNumber]);
void calculate_average(StudentInfo s[StudentNumber]);
void display_student_record(StudentInfo s[StudentNumber]);

int main()
{
StudentInfo students[StudentNumber];

get_student_info(students);
calculate_average(students);
display_student_record(students);
}

void get_student_info(StudentInfo s[StudentNumber])
{
for (int i=0; i<StudentNumber; ++i)
{
std::cout << "What is the name of Student " << (i+1) << "?: ";
std::cin >> s.name;
std::cout << "What is " << s.name<<"'s ID number?: ";
std::cin >> s.student_id;
std::cout << "Enter 4 test scores for " << s.name << ": " <<
std::endl;

for (int j=0; j<4; ++j)
{
std::cin >> s.test_scores[j];
s.sum += s.test_scores[j];
}
}
}

void display_student_record(StudentInfo s[StudentNumber])
{
std::cout << "Student Name\t"
<< "StudentID\t"
<< "Test Score Average\t"
<< "Grade\t" << std::endl << std::endl;

for (int i=0; i<StudentNumber; ++i)
{
std::cout << s.name << "\t\t"
<< s.student_id << "\t\t"
<< s.average_test_score << "\t\t\t"
<< s.grade << std::endl;
}
}

void calculate_average(StudentInfo s[StudentNumber])
{
for(int i=0; i<StudentNumber; ++i)
{
s.average_test_score = float(s.sum)/4.0f;

float t = s.average_test_score;

if (t >= 90 && t <=100)
s.grade = 'A';
else if (t >= 80 && t <=89)
s.grade = 'B';
else if (t >= 70 && t <=79)
s.grade = 'C';
else if (t >= 60 && t <=69)
s.grade = 'D';
else
s.grade = 'F';
}
}

1) make sure your functions have only one responsability (does
CalculateAverage() really only calculates the average?)
2) learn to use std::vector (from <vector>) instead of arrays
3) learn to shorten your variable names
4) indent and format your code correctly
5) try not to use globals
6) use std::string
7) buy a good book


Jonathan
 
R

Richard

I am not familiar with passing array into function and returning or passing
vector into function and returning it. What are the correct syntax for
doing this? I look into the my book to the chapters where they talk about
array and vector, but they don't show me how to pass array and vector into
function and returning them. I modified the code, but I don't understand
what the errors are. They are 3 errors this time. Here is the code again.
Please help!

#include <iostream>

#include <ostream>

#include <vector>

using namespace std;

const int StudentNumber=2;

//structure declaration

struct Student

{

char Name[20];

int StudentID;

int TestScores[4];

float AverageTestScore;

char Grade;

};

Student StudentInfo[StudentNumber];

// funciton phototype declaration

vector<int> GetStudentInfo(void);

void CalculateAverage(void);

void DisplayStudentRecord(void);

vector <int> sum[StudentNumber];

void main()

{ //int sum=0;

GetStudentInfo();

CalculateAverage();


DisplayStudentRecord();


}



vector<int>GetStudentInfo(void)

{


for(int count =0; count < StudentNumber; count++)

{


cout<<"What is the name of Student "<<count+1<<"?: ";

cin>>StudentInfo[count].Name;

cout<<"What is "<<StudentInfo[count].Name<<"'s ID number?: ";

cin>>StudentInfo[count].StudentID;

cout<<"Enter 4 test scores for "<<StudentInfo[count].Name<<": "<<endl;

for (int count2=0; count2 < 4; count2++)

{

cin>>StudentInfo[count].TestScores[count2];

sum[count] += StudentInfo[count].TestScores[count2];

}


}

return sum;



}

void CalculateAverage(void)

{ for(int count =0; count < StudentNumber; count++)

{

StudentInfo[count].AverageTestScore = float(sum[count])/4;

if (StudentInfo[count].AverageTestScore >= 90 &&
StudentInfo[count].AverageTestScore <=100)

{

StudentInfo[count].Grade = 'A';

}

if (StudentInfo[count].AverageTestScore >= 80 &&
StudentInfo[count].AverageTestScore <=89)

{

StudentInfo[count].Grade = 'B';

}

if (StudentInfo[count].AverageTestScore >= 70 &&
StudentInfo[count].AverageTestScore <=79)

{

StudentInfo[count].Grade = 'C';

}

if (StudentInfo[count].AverageTestScore >= 60 &&
StudentInfo[count].AverageTestScore <=69)

{

StudentInfo[count].Grade = 'D';

}

if (StudentInfo[count].AverageTestScore <59)

{

StudentInfo[count].Grade = 'F';

}


}


}

void DisplayStudentRecord(void)

{

cout<<"Student Name\t"<<"StudentID\t"<<"Test Score
Average\t"<<"Grade\t"<<endl<<endl;

for (int count=0; count < StudentNumber; count++)

{


cout<<StudentInfo[count].Name<<"\t\t"<<StudentInfo[count].StudentID<<"\t\t"<<

StudentInfo[count].AverageTestScore<<"\t\t\t"<<StudentInfo[count].Grade<<endl;

}

}
 
J

John Harrison

Richard said:
I am not familiar with passing array into function and returning or passing
vector into function and returning it. What are the correct syntax for
doing this? I look into the my book to the chapters where they talk about
array and vector, but they don't show me how to pass array and vector into
function and returning them. I modified the code, but I don't understand
what the errors are. They are 3 errors this time. Here is the code again.
Please help!

The problem is that you are writing too much and making too many
mistakes. You have code which has dozens of mistakes and misconceptions
and you cannot possible fix them all at once.

Follow Mike's advice, write small amounts of code and test. Get each
piece of code working before you write any more. For instance you say
you don't know how to write a function that returns vector. Well try
writing a small piece of code that does that, and nothing else. Just one
function, making up some values for a vector and see if you can return
them from a function. Once you have that program working then you have
mastered that technique, and you can move on to something else. Write
small pieces of code and get them working before moving on! I've been
coding for over twenty years and I still work this way. So does every
other programmer I know.

Throw this code away and start again. It will be much easier if you
follow this advice.

There are really too many different interconnected things wrong to say
fix this line, fix that line.

john
 
D

Default User

Richard said:
I am not familiar with passing array into function and returning or
passing vector into function and returning it. What are the correct
syntax for doing this? I look into the my book to the chapters where
they talk about array and vector, but they don't show me how to pass
array and vector into function and returning them.

What book are you using? It's hard to believe that it doesn't at least
cover how arrays are dealt with as function parameters.

Also, when you post code that generates errors, tell us what the errors
are.



Brian
 
R

Richard

Thank You.


John Harrison said:
The problem is that you are writing too much and making too many mistakes.
You have code which has dozens of mistakes and misconceptions and you
cannot possible fix them all at once.

Follow Mike's advice, write small amounts of code and test. Get each piece
of code working before you write any more. For instance you say you don't
know how to write a function that returns vector. Well try writing a small
piece of code that does that, and nothing else. Just one function, making
up some values for a vector and see if you can return them from a
function. Once you have that program working then you have mastered that
technique, and you can move on to something else. Write small pieces of
code and get them working before moving on! I've been coding for over
twenty years and I still work this way. So does every other programmer I
know.

Throw this code away and start again. It will be much easier if you follow
this advice.

There are really too many different interconnected things wrong to say fix
this line, fix that line.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top