Why wont My Program display the Average?
Sep 16, 2016 at 11:25pm UTC
Why wont My Program display the Average? I keep getting numbers like -1.231230e044 no matter what i do...
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
#include <iostream>
#include <string>
#include <iomanip>
const int N = 10; //# of students
using namespace std;
struct Students
{
string Name; //student name
double IDnum; //student id number
double Average; //student average
double *Tests = nullptr ; //student test scores
string Grade; //student Letter grade
};
void Print(Students *); //Function to display
int main()
{
//dynamically allocating an array of structures
Students *Class1;
Class1 = new Students[N];
//find how many test scores per student
int num_of_tests;
cout << "How many test scores per student?" << endl;
cin >> num_of_tests;
//to check if its negative or not
while (num_of_tests < 0)
{
cout << "How many test scores per student?" << endl;
cin >> num_of_tests;
}
//for loop to get student name
for (int counter = 0; counter < N; counter++)
{
cout << "What is the name of Student " << counter << "?" << endl;
cin >> Class1[counter].Name;
}
// For loop to get student ID
for (int counter = 0; counter < N; counter++)
{
cout << "What is the Student ID of " << Class1[counter].Name << endl;
cin >> Class1[counter].IDnum;
}
//for loop to declare new int
for (int counter = 0; counter < N; counter++)
{
Class1[counter].Tests = new double [num_of_tests];
}
//for loops for getting test scores
for (int i = 0; i < num_of_tests; i++)
{
for (int counter = 0; counter < N; counter++)
{
cout << "What is the test score of " << Class1[counter].Name << "?" << endl;
cin >> Class1[counter].Tests[i];
//to check if its negative or not
while (Class1[counter].Tests[i] < 0)
{
cout << "What is the test score " << Class1[counter].Name << "?" << endl;
cin >> Class1[counter].Tests[i];
}
//finding the average
Class1[counter].Average += Class1[counter].Average;
Class1[counter].Average /= num_of_tests;
}
}
cout << endl << endl;
//function call
Print(Class1);
delete [] Class1;
system("pause" );
return 0;
}
void Print(Students Class1[])
{
cout << endl << endl;
//create table
cout << "Name \t\t\t Grade" << endl;
cout << "----------------------------------------------------------" << endl;
//display grade
for (int counter = 0; counter < N; counter++)
{
if (Class1[counter].Average <= 100 && Class1[counter].Average >= 91)
{
Class1[counter].Grade = 'A' ;
cout << Class1[counter].Name << " \t\t\t " << Class1[counter].Grade << endl;
}
if (Class1[counter].Average <= 90 && Class1[counter].Average >= 81)
{
Class1[counter].Grade = 'B' ;
cout << Class1[counter].Name << " \t\t\t " << Class1[counter].Grade << endl;
}
if (Class1[counter].Average <= 80 && Class1[counter].Average >= 71)
{
Class1[counter].Grade = 'C' ;
cout << Class1[counter].Name << " \t\t\t " << Class1[counter].Grade << endl;
}
if (Class1[counter].Average <= 70 && Class1[counter].Average >= 61)
{
Class1[counter].Grade = 'D' ;
cout << Class1[counter].Name << " \t\t\t " << Class1[counter].Grade << endl;
}
if (Class1[counter].Average <= 60 && Class1[counter].Average >= 0)
{
Class1[counter].Grade = 'F' ;
cout << Class1[counter].Name << " \t\t\t " << Class1[counter].Grade << endl;
}
}
cout << endl << endl;
//create table
cout << "Name \t\t\t ID" << endl;
cout << "----------------------------------------------------------" << endl;
//for loop to display student ID
for (int counter = 0; counter < N; counter++)
{
cout << Class1[counter].Name << " \t\t\t " << Class1[counter].IDnum << endl;
}
cout << endl << endl;
//create table
cout << "Name \t\t\t Average Test Scores" << endl;
cout << "----------------------------------------------------------" << endl;
//for loop to display test score averages
for (int counter = 0; counter < N; counter++)
{
cout << Class1[counter].Name << " \t\t\t " << Class1[counter].Average << endl;
}
}
Sep 16, 2016 at 11:41pm UTC
try initializing average to zero at the start of your program.
I didn't fully read the code but it looks like some garbage value in memory is used in the display grade loop and it immediately leaves the loop as if doesn't match any if statement value.
Sep 17, 2016 at 12:12am UTC
I agree, always initialize all your variables.
Sep 17, 2016 at 12:18am UTC
Your program works fine.
How many test scores per student?
2
What is the name of Student 0?
g
What is the name of Student 1?
g
What is the name of Student 2?
g
What is the name of Student 3?
gdsf
What is the name of Student 4?
sdf
What is the name of Student 5?
sd
What is the name of Student 6?
fsd
What is the name of Student 7?
fsd
What is the name of Student 8?
f
What is the name of Student 9?
f
What is the Student ID of g
435
What is the Student ID of g
43
What is the Student ID of g
543
What is the Student ID of gdsf
534534
What is the Student ID of sdf
5
What is the Student ID of sd
34
What is the Student ID of fsd
534
What is the Student ID of fsd
5
What is the Student ID of f
34
What is the Student ID of f
5
What is the test score of g?
435345
What is the test score of g?
34
What is the test score of g?
534
What is the test score of gdsf?
5
What is the test score of sdf?
34
What is the test score of sd?
534
What is the test score of fsd?
5
What is the test score of fsd?
34456
What is the test score of f?
5476
What is the test score of f?
544
What is the test score of g?
654
What is the test score of g?
6
What is the test score of g?
45
What is the test score of gdsf?
654
What is the test score of sdf?
6
What is the test score of sd?
456
What is the test score of fsd?
54645
What is the test score of fsd?
645
What is the test score of f?
6
What is the test score of f?
4545
Name Grade
----------------------------------------------------------
g F
sdf F
Name ID
----------------------------------------------------------
g 435
g 43
g 543
gdsf 534534
sdf 5
sd 34
fsd 534
fsd 5
f 34
f 5
Name Average Test Scores
----------------------------------------------------------
g 435999
g 40
g 579
gdsf 659
sdf 40
sd 990
fsd 54650
fsd 35101
f 5482
f 5089
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
#include <iostream>
#include <string>
#include <iomanip>
const int N = 10; //# of students
using namespace std;
struct Students
{
Students() : Tests(NULL), Average(0) {}
string Name; //student name
double IDnum; //student id number
double Average; //student average
double *Tests; //student test scores
string Grade; //student Letter grade
};
void Print(Students *); //Function to display
int main()
{
//dynamically allocating an array of structures
Students *Class1;
Class1 = new Students[N];
int counter;
//find how many test scores per student
int num_of_tests;
cout << "How many test scores per student?" << endl;
cin >> num_of_tests;
//to check if its negative or not
while (num_of_tests < 0)
{
cout << "How many test scores per student?" << endl;
cin >> num_of_tests;
}
//for loop to get student name
for (counter = 0; counter < N; counter++)
{
cout << "What is the name of Student " << counter << "?" << endl;
cin >> Class1[counter].Name;
}
// For loop to get student ID
for (counter = 0; counter < N; counter++)
{
cout << "What is the Student ID of " << Class1[counter].Name << endl;
cin >> Class1[counter].IDnum;
}
//for loop to declare new int
for (counter = 0; counter < N; counter++)
{
Class1[counter].Tests = new double [num_of_tests];
}
//for loops for getting test scores
for (int i = 0; i < num_of_tests; i++)
{
for (counter = 0; counter < N; counter++)
{
cout << "What is the test score of " << Class1[counter].Name << "?" << endl;
cin >> Class1[counter].Tests[i];
//to check if its negative or not
while (Class1[counter].Tests[i] < 0)
{
cout << "What is the test score " << Class1[counter].Name << "?" << endl;
cin >> Class1[counter].Tests[i];
}
//finding the average
Class1[counter].Average += Class1[counter].Tests[i];
}
Class1[counter].Average /= num_of_tests;
}
cout << endl << endl;
//function call
Print(Class1);
delete [] Class1;
system("pause" );
return 0;
}
void Print(Students Class1[])
{
cout << endl << endl;
//create table
cout << "Name \t\t\t Grade" << endl;
cout << "----------------------------------------------------------" << endl;
//display grade
for (int counter = 0; counter < N; counter++)
{
if (Class1[counter].Average <= 100 && Class1[counter].Average >= 91)
{
Class1[counter].Grade = 'A' ;
cout << Class1[counter].Name << " \t\t\t " << Class1[counter].Grade << endl;
}
if (Class1[counter].Average <= 90 && Class1[counter].Average >= 81)
{
Class1[counter].Grade = 'B' ;
cout << Class1[counter].Name << " \t\t\t " << Class1[counter].Grade << endl;
}
if (Class1[counter].Average <= 80 && Class1[counter].Average >= 71)
{
Class1[counter].Grade = 'C' ;
cout << Class1[counter].Name << " \t\t\t " << Class1[counter].Grade << endl;
}
if (Class1[counter].Average <= 70 && Class1[counter].Average >= 61)
{
Class1[counter].Grade = 'D' ;
cout << Class1[counter].Name << " \t\t\t " << Class1[counter].Grade << endl;
}
if (Class1[counter].Average <= 60 && Class1[counter].Average >= 0)
{
Class1[counter].Grade = 'F' ;
cout << Class1[counter].Name << " \t\t\t " << Class1[counter].Grade << endl;
}
}
cout << endl << endl;
//create table
cout << "Name \t\t\t ID" << endl;
cout << "----------------------------------------------------------" << endl;
//for loop to display student ID
for (counter = 0; counter < N; counter++)
{
cout << Class1[counter].Name << " \t\t\t " << Class1[counter].IDnum << endl;
}
cout << endl << endl;
//create table
cout << "Name \t\t\t Average Test Scores" << endl;
cout << "----------------------------------------------------------" << endl;
//for loop to display test score averages
for (counter = 0; counter < N; counter++)
{
cout << Class1[counter].Name << " \t\t\t " << Class1[counter].Average << endl;
}
}
Sep 17, 2016 at 12:47am UTC
Thanks so much! I put it back into Visual C++ and it worked perfectly!
Last question:
What did you do here?
Students() : Tests(NULL), Average(0) {}
Last edited on Sep 17, 2016 at 12:47am UTC
Sep 17, 2016 at 12:49am UTC
Member initializer list.
http://en.cppreference.com/w/cpp/language/initializer_list
Topic archived. No new replies allowed.