how many students are there?
2
how many tests did each student take?
2
What's the name of student 1
cc dd
What's the student's ID number?
1
what score did the student get on test 1
44
what score did the student get on test 2
55
student 1 average is 49
student 1 got an F
What's the name of student 2
dd ff
What's the student's ID number?
2
what score did the student get on test 1
44 66
what score did the student get on test 2
student 2 average is 104
student 2 got an A
Press any key to continue . . .
obviously the average of the second student isn't 104. I think it added the two averages together to get that. As you can see at the bottom of my code I tried to set the average back to 0 it was done going through the loop once but it didn't work. How do I fix it?
It is a little difficult to read because of the incorrect indenting but it looks like your variable "total" did not reset for the second student. Hope that helps =).
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
struct student_info // stucture to hold data
{
string name;
int idnum;
int *tests;
int average;
int grade;
};
int average; // total and average variables set
int total = 0;
student_info *students;
student_info student;// pointer variable set to structure
int main()
{
int num_of_studs = 0; // other two ncessary variables set
int num_of_tests = 0;
do
{
cout << "how many students are there?\n"; // get number of students
cin >> num_of_studs;
}
while (num_of_studs < 1); // looped to make that the input isn't 0 or a negative number
do
{
cout << "how many tests did each student take?\n";
cin >> num_of_tests; // get number of tests each student took
}
while (num_of_tests < 1); // looped to make that the input isn't 0 or a negative number
students = new student_info[num_of_studs]; // allocated array
if (students == NULL) // in case nothing in entered by the user
{
cout << "ERROR\n";
exit(0);
}
for (int count = 0; count < num_of_studs; count++)
{
cin.ignore(); // fixes issue with the getline command
do
{
cout << "What's the name of student " << (count + 1) << endl;
getline(cin, students[count].name); // get student's name
}
while (students[count].name == ""); // looped to make that something is entered by the user
do
{
cout << "What's the student's ID number?\n";
cin >> students[count].idnum; // get student's ID number
}
while (students[count].idnum <= 0); // looped to make that the input isn't a negative number
students[count].tests = newint[num_of_tests]; // allocated array
for (int counter = 0; counter < num_of_tests; counter++)
{
do
{
cout << "what score did the student get on test " << (counter + 1) << endl;
cin >> students[count].tests[counter]; // get test scores
}
while (students[count].tests[counter] <= 0);
}
for (int counter = 0; counter < num_of_tests; counter++) // loop to make sure it doesn't go out of bounds
{ // and adds the scores for a total
if (counter == num_of_tests)
{
count++;
}
total += students[count].tests[counter];
// display student info and what each student got as their grade
}
cout << "student name is " << students[count].name << endl;
cout << "student ID number is " << students[count].idnum << endl;
(*students)[count].average = total / num_of_tests;
cout << "student " << " average is " << student[count].average << endl;
if (average >= 91)
cout << "student " << " got an A\n";
elseif (average >= 81 && average <= 90)
cout << "student " << " got a B\n";
elseif ( average >= 71 && average <= 80)
cout << "student " << " got a C\n";
elseif (average >= 61 && average <= 70)
cout << "student " << " got a D\n";
else
{
cout << "student " << " got an F\n";
}
total = 0, average = 0;
}
}
I started making the changes I thought I needed to do on lines 95 and 96 but I'm getting errors and I don't know why, (I don't understand the errors) (originally I did use the -> operator but I was getting red lines from the compiler, doing it this "long way" got rid of those red lines.
p.s. I also realize that I'm going have to get rid of my int average; variable.
1> structure program.cpp
1>l:\comp sci stuff\structure program\structure program.cpp(94): error C2676: binary '[' : 'student_info' does not define this operator or a conversion to a type acceptable to the predefined operator
1>l:\comp sci stuff\structure program\structure program.cpp(94): error C2228: left of '.average' must have class/struct/union
1>l:\comp sci stuff\structure program\structure program.cpp(95): error C2676: binary '[' : 'student_info' does not define this operator or a conversion to a type acceptable to the predefined operator
1>l:\comp sci stuff\structure program\structure program.cpp(95): error C2228: left of '.average' must have class/struct/union
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
omg I'm embarrassed. Thanks. I have one more question that goes beyond my current knowledge. (at least I'm pretty sure it does) See where I ask the user to type their ID number. If there're two or more students, how do I make sure that they don't type same number? Is there a certain command that does in that I can put in the while part of the do-while loop with the && command?
You could try something like this to verify the ID are all different. I can't promise this snippet of code will work unaltered in your program, but it should be pretty close.
1 2 3 4 5 6 7 8 9 10 11 12 13
bool ok;
do
{
ok = true;
cout << "What's the student's ID number?\n";
cin >> students[count].idnum; // get student's ID number
for (int ck=0;ck<count;ck++)
{
if (students[ck].idnum == students[count].idnum)
ok = false;
}
}
while (students[count].idnum > 0 && ok); // looped to make that the input isn't a negative number
I didn't think it was that hard. I just thought there was some simple command I could put in the while part of the do-while loop. That I just haven't been taught yet. Oh well. it was necessary. I thought you guys could help me add it as an extra to my homework.
Okay science man..
I forgot that on the first check, they would be the same, so I would then add one more check. Try it this way.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
bool ok;
do
{
ok = true;
cout << "What's the student's ID number?\n";
cin >> students[count].idnum; // get student's ID number
if(count >0)
{
for (int ck=0;ck<count;ck++)
{
if (students[ck].idnum == students[count].idnum)
ok = false;
}
}
}
while (students[count].idnum > 0 && ok); // looped to make that the input isn't a negative number
same thing happened. It's ok the deadline was today. Even if that worked it would've been good for future reference. (and I don't really understand it anyway) no offense.