I appreciate the feedback. I went through and played around with this a little bit. I didn't really understand too much about the input/output for functions tbh. I am taking a class on this at university now but I have a poor teacher so I am trying to gather information from other sources.
I have another question if I can tag it along to prevent having to create a new thread. For the "remove grades" function I put comment marks around the specific problem that I'm having. Whenever i try to remove a grade it always removes the first one. Sorry for the layout of the variables, I have been playing with it for hours. When it goes into that inner do while loop it shouldn't enter the if statement but it always seem to.
For example; I will run the program, press 2 to remove grades, enter 90 to remove it, enter 0 to exit. But it only removes the first grade.
Thanks in advance.
Code:
#include <stdio.h>
//List of variables & function prototypes
int menu(void);
int addGrades();
int removeGrades(void);
int printGrades(void);
int sortGrades(void);
int minMaxGrades(void);
int averageGrades(void);
int menuChoice;
int grades[14] = {60,50,50,20,75,90,0,0,0,0,0,0,0,0};
int x;
int y;
int z;
int currentTask;
//End list of variables & func. prototypes
//Beginning of main function
main()
{
do
{
menu();
switch(menuChoice)
{
case 1:
addGrades();
break;
case 2:
removeGrades();
break;
case 3:
printGrades();
break;
case 4:
sortGrades();
break;
case 5:
minMaxGrades();
break;
case 6:
averageGrades();
break;
}
int i;
for(i = 0; i<15;i++)
{
printf("%d,", grades[i]);
}
}while(menuChoice != 7);
printf("\n\nHave a nice day!\nGoodbye");
}
//End of main function
//Function to call the menu
int menu()
{
printf("\n\nWelcome to grades information system.\n");
printf("Select one of the options:\n");
printf("1) Add Grades\n");
printf("2) Remove Grades\n");
printf("3) Print Grades\n");
printf("4) Sort Grades\n");
printf("5) Find Max/Min Grades\n");
printf("6) Calculate Average\n");
printf("7) Quit\n");
printf("\n\nEnter Your Choice: ");
scanf("%d", &menuChoice);
fflush(stdin);
}
//End Menu Function
//Function for adding grades
int addGrades()
{
printf("\nYou have chosen to Add Grades.\n");
printf("\nYour current grades are:\n");
for(x = 0; x < 15; x++)
{
if(grades[x] > 0)
{
printf("%d ", grades[x]);
}
}
printf("\n\nEnter up to a total of 15 grades. Press enter after each entry.\n");
printf("Enter 0 to exit: \n");
int k = 1;
int a = 0;
do
{
if(grades[a] == 0)
{
scanf("%d", &k);
grades[a] = k;
fflush(stdin);
}
a = a + 1;
if(a>15)
{
break;
}
}while(k != 0);
printf("\n\nSending you back to the home screen..\n\n");
sleep(3);
}
//End function for adding grades
//Function for removing grades
int removeGrades()
{
printf("\nYou have chosen to Remove Grades.\n");
printf("\nYour current grades are:\n");
for(x = 0; x < 15; x++)
{
if(grades[x] > 0)
{
printf("%d ", grades[x]);
}
}
printf("\n\nEnter the grade that you want to remove. Press Enter after each input.\n");
printf("Enter 0 to exit: \n");
int c = 0;
int p;
/////////////////////////////////This is where I'm having the issue
do
{
scanf("%d", &p);
fflush(stdin);
do
{
if (grades[c] == p);
{
grades[c] = 0;
break;
}
c = c + 1;
}while(c < 15);
}while(p > 0);
//////////////////////////////////This is where I'm having the issue
printf("\nYour updated grades are:\n");
for(x = 0; x < 15; x++)
{
if(grades[x] > 0)
{
printf("%d ", grades[x]);
}
}
printf("Sending you back to the home screen..\n\n");
sleep(3);
}
//End function for removing grades
//Function for printing grades
int printGrades()
{
int i = 1;
printf("\nYour current grades are:\n");
for(x = 0; x < 15; x++)
{
if(grades[x] > 0)
{
printf("%d) %d\n", i, grades[x]);
i = i + 1;
}
}
printf("\n\nSending you back to the home screen..\n\n");
sleep(3);
}
//End function for printing grades
//Function for sorting grades
int sortGrades()
{
int i;
int j;
int k;
//Starts sorting all of the numbers
printf("\nYour current grades are:\n");
for(i = 0;i < 15; i++)
{
for(j = 0;j < 15 - i; j++)
{
if(grades[j] < grades[j+1])
{
k = grades[j];
grades[j] = grades[j+1];
grades [j+1] = k;
}
}
}
//Starts printing all of the numbers above 0
i = 1;
for(x = 0; x < 15; x++)
{
if(grades[x] > 0)
{
printf("%d) %d\n", i, grades[x]);
i = i + 1;
}
}
printf("\n\nSending you back to the home screen..\n\n");
sleep(3);
}
//End function for sorting grades
//Begin function to give min/max
int minMaxGrades()
{
int max = grades[0];
int i;
for(i = 0; i < 15; i++)
{
if(max < grades[i])
{
max = grades[i];
}
}
printf("\n\nYour max grade is %d", max);
int min = grades[0];
for(i = 0; i < 15; i++)
{
if(min > grades[i] && grades[i] != 0)
{
min = grades[i];
}
}
printf("\nYour min grade is %d", min);
printf("\n\nSending you back to the home screen..\n\n");
sleep(3);
}
//End function to give min/max
//Function to calculate average grade
int averageGrades()
{
int i;
float sum = 0;
float divide = 0;
for(i = 0; i < 15; i++)
{
if(grades[i] > 0)
{
sum = sum + grades[i];
divide = divide + 1;
}
}
printf("\n\nYour average is %.1f", sum/divide);
printf("\n\nSending you back to the home screen..\n\n");
sleep(3);
}
//End function to calculate average grade