little issue

Hey guys I'm working on this last program that I have to make for my class and I'm one small issue.
Here's my code
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct student_info
{
	string name;
	int idnum;
	int *tests;
	int average;
	int grade;
};
 int average;
		int total = 0;

student_info *students;

int main()
{
	int num_of_studs = 0;
	int num_of_tests = 0;
	do
   {
   cout << "how many students are there?\n";
	cin >> num_of_studs;
   }
   while (num_of_studs < 1);
   do
   {
   cout << "how many tests did each student take?\n";
	cin >> num_of_tests;
   }
   while (num_of_tests < 1);

	students =  new student_info[num_of_studs];
   if (students == NULL)
   {
      cout << "ERROR\n";
      exit(0);
   }
   


   
	for (int count = 0; count < num_of_studs; count++)
	{
      cin.ignore();
	do
   {
    cout << "What's the name of student " << (count + 1) << endl;
		getline(cin, students[count].name);
   }
   while (students[count].name == "");


   do
   {
    cout << "What's the student's ID number?\n";
		cin >> students[count].idnum;
   }
   while (students[count].idnum <= 0);

   
  students[count].tests = new int[num_of_tests];
   
   
   
		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];
		 }
        while (students[count].tests[counter] <= 0);

		}

		for (int counter = 0; counter < num_of_tests; counter++)
		{
			if (counter == num_of_tests)
			{
				count++;
			}
      	total += students[count].tests[counter];
		


		}

		average = total / num_of_tests;
		cout << "student " << (count + 1) << " average is " << average << endl;
		if (average >= 91)
			cout << "student " << (count + 1) << " got an A\n";
		else if (average >= 81 && average <= 90)
			cout << "student "<< (count + 1) << " got a B\n";
		else if ( average >= 71 && average <= 80)
			cout << "student " << (count + 1) << " got a C\n";
		else if (average >= 61 && average <= 70)
			cout << "student " << (count + 1) << " got a D\n";
		else 
		{
			cout << "student " << (count + 1) << " got an F\n";
		
		}
                average = 0;
		}
		}


and here's the output
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

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 =).
Last edited on
How do I get it to do that?
Last edited on
Bump.

Please guys?
I tried your program. The avg is working fine. It seems that you haven't reset the total. Just do one thing in your code.
@line 108, put

total=0, average=0.

Let me know if that helps :)
Last edited on
That did it. Thank you so much!!!
I do understand why that works. It just never occurred to me.
Last edited on
Hey guys turns out my professor wants me to use members of the structure I wasn't originally using.
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

#include <iostream>
#include <string>
#include <iomanip>
using namespace 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 = new int[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";
		else if (average >= 81 && average <= 90)
			cout << "student " << " got a B\n";
		else if ( average >= 71 && average <= 80)
			cout << "student "  << " got a C\n";
		else if (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.
Last edited on
the errors are
1
2
3
4
5
6
7
1
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 ==========
You are apply the array subscript operator[] to a student_info class, which is not an array.
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?
No?


Bump.
@science man

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 
Last edited on
No it didn't work. It just made the question repeat for the first student.
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.
Last edited on
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.
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
// ...

// unary predicate for std::find_if
struct student_info_compare
{
	const student_info & student ;

	student_info_compare(const student_info & s) : student(s) {}

	bool operator()(const student_info & info ) const
	{
		return student.idnum == info.idnum ;
	}

};

// ...


		student_info* last_student = students+count-1 ;  // end iterator
		do
		{
			cout << "What's the student's ID number?\n";
			cin >> students[count].idnum; // get student's ID number
		}
		while (students[count].idnum <= 0 && last_student != std::find_if(students, last_student, student_info_compare(students[count])) );


http://www.cplusplus.com/reference/algorithm/find_if/
Topic archived. No new replies allowed.