Hey guys, I was just having an issue (I've never used arrays before and am new to programming in general).
What the program is supposed to do at this point, is basically input up to 20 marks into an array, and I wanna add an exit command 'maybe enter an x to stop inputting marks?'
I've also been trying to get it to calculate the average/min/max marks...I can figure out the average but I'm not sure how to display the minimum and maximum mark, as well as the standard deviation.
Something I've been trying to do aswell is add a letter grade to go along with these...but I kept messing the code up somehow while trying that too :/
Just having trouble with this at all, I've never had to use an array in anything before and I'm having a few issues...Any help/hints would be awesome and much appreciated.
Thanks in advance! (Here is the code I have so far)
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
void main()
{
int Counter=0;
int Assignment[20];
int Selection, student, Count;
double grade;
float GPA;
float Divisor = 20;
cout << "Please select for pay type from the following choices:\n";
cout << " 1 - Input Student Grade Data\n";
cout << " 2 - Grade Average\n";
cout << " 3 - Print Grades to Screen\n";
cout << " 0 - Exit\n";
cin >> Selection;
while (Selection != 0)
{
switch (Selection)
{
case (1):
while (Counter < 20)
{
Counter = Counter + 1;
cout << "Enter student number (1-20): \n";
cin >> student;
cout << "Enter current grade for student: \n";
cin >> grade;
Assignment[student-1] = grade;
cout << "student " << student << "'s grade is now " << grade << ".\n\n";
}
break;
case (2):
GPA = 0.0;
for (Count=0; Count <=19; Count++)
{
GPA = GPA + Assignment[Count];
}
cout << "Class average is " << GPA / Divisor << ".\n\n";
break;
Ahh thanks for that hint :D, gives me an idea of what's going on haha...I just realized another error I have going on though...When I input all of the 'marks', and use the display...they all display as -860000ish or something like that...any idea what's causing this aswell?
I copied your code and compiled it ,find something wrong!
here I will give you some ways,and you should apply them in your other code
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
void main()
{
int Counter=0;
int Assignment[20];
int Selection,student, Count;
char stu[20];//a student name is a string ,not interger!
double grade;
float GPA;
float Divisor = 20;
cout << "Please select for pay type from the following choices:\n";
cout << " 1 - Input Student Grade Data\n";
cout << " 2 - Grade Average\n";
cout << " 3 - Print Grades to Screen\n";
cout << " 0 - Exit\n";
cin >> Selection;
while (Selection != 0)
{
switch (Selection)
{
case (1):
while (Counter < 20)
{
Counter = Counter + 1;
cout << "Enter student number (1-20): \n";
cin >> stu;
if(!strcmp(stu,"x"))//if stu is 'x',then break;
{
break;
}
cout << "Enter current grade for student: \n";
cin >> grade;
Assignment[Counter] = grade;
cout << "student " << stu << "'s grade is now " << grade << ".\n\n";
}
break;
case (2):
GPA = 0.0;
for (Count=0; Count <=19; Count++)
{
GPA = GPA + Assignment[Count];
}
cout << "Class average is " << GPA / Divisor << ".\n\n";
break;
It could be the combination of your inputs and this assignment:
Assignment[student-1] = grade;
Your array is declared as Assignment[20]. Thus it's elements can be reached by Assignment[0], Assignment[1] .. Assignment[19].
Problem is that your program heavily relies on correct inputs from the user. The user have to input student numbers from 1 to 20 (each one of them only once), because if you e.g. assign to Assignment[4] twice and you forget to assign to Assignment[15] at all than value of this element would be undefined (hence the hellish number -860000ish).