My challenge from C Programming of for the Absolute Beginner (which accurately describes me)
Create a student GPA average calculator. The program should
prompt the user to enter up to 30 GPAs, which are stored in a
single-dimension array. Each time he or she enters a GPA, the
user should have the option to calculate the current GPA
average or enter another GPA. Sample data for this program is
shown below.
GPA: 3.5
GPA: 2.8
GPA: 3.0
GPA: 2.5
GPA: 4.0
GPA: 3.7
GPA Average: 3.25
Hint: Be careful to not calculate empty array elements into
your student GPA average.
I have really no clue what I am doing. A friend of mine who worked for a decade as a computer programmer gave me this advice, which I tried to code:
I asked him:
I am trying to write a program that does the following:
Code:
While !inputdone
{
Accepts inputs into an array
Checks for digits and rejects alpha
Once there are more than two inputs stored in the array
{
offer the option to average inputs, or
continue entering input
}
}
He said:
Code:
While !inputdone
{
Ask for grade
Check for numeric
If numeric {
Put input in array
increment array index
If two or more numbers have been entered {
ask if user wants to average
if answer is yes {
average
print result
return(0);
}
}
}
And I have this so far (note I have not yet used iInputDone, and I don't even really know how I would do so - please be gentle, I know it is riddled with mistakes, but although a competent and logical person, I am crushed under the feet of computer programming)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int checkisdigit(int), i, iInput;
char cInput;
void main(void)
{
int iGrades[30] = {0}, iInput = 0, i = 0, iDoneInput = 0;
char cAverage;
printf("ENTER STUDENT GRADES\n");
system("pause");
system("cls");
while (!iDoneInput)
{
entergrade:
system("cls");
iInput = 0;
printf("\nPlease enter a grade: ");
scanf("%c", &cInput);
if (checkisdigit(cInput))
iInput = cInput - '0';
else
goto entergrade;
for (i = 1; i <= 30; i++);
iGrades[i] = iInput;
{
if (i >= 2)
{
calculateaverage:
printf("\nCalculate (A)verage?");
scanf("%c", &cAverage);
{
if (!checkisdigit(cAverage))
{
if ((cAverage == 'A') || (cAverage == 'a'))
{
printf("\nAverage is %d", iGrades[i] += 0 / i);
return 0;
} // end if (cAverage == A)
else
goto entergrade;
} // end if (!checkisdigit)
else
goto calculateaverage;
} // end calculateaverage
} // end if i>2
} // end for
} // end while
} // end main
/*******\
Functions
********/
int checkisdigit(int iMyDigit)
{
if ((iMyDigit >= '0') && (iMyDigit <= '9'))
return 1;
else
return 0;
} // end checkisdigit
The main problem is this: In runtime I enter a value but it displays the (A)verage? question for a split second, even after the first entry. And I have no idea how to check for a digit, then accept the value into the array.
Please teach me! Thanks in advance.
David