Hello ObiNation00,
I found that your use of pointers to be the biggest problem. The question is are you required to use pointers for your program?
In the struct "student_info", you may consider "StudentInfo" The capital "S" will let you know that it is a class, function or struct, you have
char *first;
, but in "main" you have
cin >> student[i]->first;
which leads one to believe that a letter is expected, but
char *first;
is expecting an address not a letter. When I removed the asterisk form
char *first;
and changed
cin >> student[i]->first;
to
cin >> student[i].first;
it worked. If you have to use pointers it can be fixed, but it will take more work.
In the following code look for the comments I have made:
main.cpp
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
|
#include <iostream>
#include <string>
//#include <stdio.h>
//#include <stdlib.h>
#include "student.h"
#include "bubble.h"
using namespace std;
constexpr std::size_t MAXSIZE{ 3 };
constexpr std::size_t MAXARRAYSIZE{ 19 };
//Name and class
struct classStats
{
float mean;
float min;
float max;
float medium;
char *name;
};
int main()
{
string userInput;
float sum{}; // <--- Changed from "int' to "float"
float min{};
float max{};
classStats stats;
student_info student[MAXARRAYSIZE];
//student = new student_info *[19]; //allocates memory for char pointers declared in structs
while (cin >> userInput)
{ //continues while cin reads something
for (int i = 0; i < MAXSIZE; i++) { //keep reading the input and write it to the correct variable from the struct
std::cout << "\n Enter first initial: ";
cin >> student[i].first;
std::cout << "\n Enter last initial: ";
cin >> student[i].last;
std::cout << "\n Enter first exam grade: ";
cin >> student[i].exam1;
std::cout << "\n Enter second exam grade: ";
cin >> student[i].exam2;
std::cout << "\n Enter third exam grade: ";
cin >> student[i].exam3;
//calculate the average of student i
sum = static_cast<float>(student[i].exam1) + static_cast<float>(student[i].exam2) + static_cast<float>(student[i].exam3);
student[i].mean = sum / static_cast<float>(3.0); // <--- Changed to 3.0
}
min = max = 0; // <--- Changed. Using the "," operator only set "max" to zero.
//find the min/max of all the grades
for (int i = 0; i < MAXSIZE; i++)
{
if (min > student[i].exam1 || min > student[i].exam2 || min > student[i].exam3) //if the current min is bigger than either exam 1 2 or 3 set that exam as the min
{
if (student[i].exam1 < student[i].exam2 && student[i].exam3)
{ //if exam 1 is less than exam 2 or 3 then it is the min
min = static_cast<float>(student[i].exam1);
}
if (student[i].exam2 < student[i].exam1 && student[i].exam3)
{
min = static_cast<float>(student[i].exam2);
}
if (student[i].exam3 < student[i].exam1 && student[i].exam2)
{
min = static_cast<float>(student[i].exam3);
}
}
if (max < student[i].exam1 || min > student[i].exam2 || min > student[i].exam3) //if the current max is smaller than either exam 1 2 or 3 set that exam as the max
{
if (student[i].exam1 > student[i].exam2 && student[i].exam3)
{ //if exam 1 is greater than exam 2 or 3 then it is the ax
max = static_cast<float>(student[i].exam1);
}
if (student[i].exam2 > student[i].exam1 && student[i].exam3)
{
max = static_cast<float>(student[i].exam2);
}
if (student[i].exam3 > student[i].exam1 && student[i].exam2)
{
max = static_cast<float>(student[i].exam3);
}
}
}
stats.min = min;
stats.max = max;
for (int j = 0; j < 19; j++)
{
cout << student[j].first << " " << student[j].last << " " << student[j].mean << endl;
}
delete[] student;
}
return 0;
}
|
student.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#ifndef _STUDENT_ // <--- Added include guard.
#define _STUDENT_
typedef struct student_info
{
char first;
char last;
int exam1; // <--- Should be made a "double" or at least a "float"
int exam2;
int exam3;
float mean;
};// student; // <--- Turns out that this is not really needed here, but could still be used.
#endif // end !_STUDENT_
|
bubble.h
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
|
#ifndef _BUBBLE_
#define _BUBBLE_
void bubble(student_info array[], int size)
{
int x;
int y;
student_info temp;
for (x = 0; x < size; x++)
{
for (y = 0; y < size - 1; y++)
{
if (array[y].mean > array[y + 1].mean)
{
temp = array[y + 1];
array[y + 1] = array[y];
array[y] = temp;
}
}
}
return;
}
#endif // end !_BUBBLE_
|
Notice the changes that were made to make it work. At least it accepts input. I have not tested it fully.
The while loop in "main" line 34 is not the best way to do the while condition. The while condition is waiting for input before the for loop can be executed. The problem you have no idea that you need to enter something to continue. You could try
while (true)
to create an endless loop and use
break
to break out of the while loop.
If you do not know about it yet the
#ifndef _STUDENT_
and other "#something" is called an include guard. This is a good thing to learn to minimize problems.
If you have instructions for this problem it would help if you would post them. This way answers can be better tailered to your problems.
Hope that helps,
Andy