I am still new to C programming and need to figure out why when I enter the "c" choice the program isn't printing out the grades entered in the program. I am not seeing what I am missing, can someone let me know if they see what I am missing please?
#include <stdio.h>
#include <stdlib.h>
int main()
{
//Add all of the variables and the array for the grades I want to enter.
char choice;
int gradeScore = 0;//percentage
//int gradeArray[100];//percentArrray //Comment Out
int gCount = 0,i;//count
//Allocate dynamic memory point using gradeArray.
int *gradeArray = (int*)malloc(sizeof(int));
/*The for loop is set to enable the user to enter no more than 100 grades. This is because the gradeArray variable
limit is set to 100. This will then loop through until the user has entered up to 100 grades to ensure there
is no buffering issue.*/
for (gCount = 0; gCount < 100;)
{
/*This prompts the user for a choice that enables them to either enter another grade or exit the program and
print the grades. It also reads the choice entered by the user.*/
printf("******************Enter Choice Selection in Parenthesis******************");
printf("\n\n To add grades, enter choice (a)");
printf("\n When finished entering grades, enter choice (c) \n \nEnter Choice: ");
scanf(" %c", &choice); //space is entered to ensure the compiler does not read whitespaces
/* Then I use an if with the condition set to a valid choice of 'a'. Then I prompt the user
to enter a grade, read it and move on to the next if statement.*/
if(choice == 'a')
{
printf("\nEnter grade: ");
scanf(" %d", &gradeScore);
/*If the grade entered does meet the if condition statement below it is added to the gCount
of grades entered. This will allow all of the grades entered to be printed with the exit condition.*/
if(gradeScore <= 100 && gradeScore >= 0)
{
gradeArray = realloc(gradeArray, sizeof(int) * gCount);
}
}
//The last if statement prints out each grade on a new line when the user choice is c.
if(choice == 'c')
{
break;
}
}
printf("Grades are:\n");
for(i = 0; i < gCount ; i++)
{
printf(" %d\%%\n", gradeArray[i]);
}
free(gradeArray);
return 0;
}
Thank you, Annette