• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

C programing question and Yes its my final project

Status
Not open for further replies.

Ophiuchus

Banned
So I have to make a simple grade book for my final project in my intro to C class.
I have to be able to enter the number of students in the class, enter their Test, Quiz, and homework grades. Average them for each student individually and then give the class overall average. Everything is working fine for the most part, but the issue I'm having is that once I input the values for one things say test grades. I cant figure out how to get the menu to repeat so I can then enter there homework and again for the quiz grades.

I thought I'd be able to make a separate function for the menu and just call it at the end of the case statements but that doesn't seem to be working for me. Another idea I had was to put the whole menu into a do while loop but again that's proving to be difficult for me with the limited knowledge I have of C so far. I'd appreciate any help. Here is my code so far


Code:
// Gradebook.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<stdio.h>

 

int tg,qg,hwg,soa,l, number,num;/*grades,loop*/
int total,numofstds,stdnum,numoftes,numofquiz,numofhw;/*total sum of all grades,number of students,student number*/
float toltestval,tolquizval,tolhwval;/*total test value,total quiz value,total hwk value*/
float tesavg,hwavg,quizavg;/*test average,hwk average*/
double stdavg[50];/*sum of all grades*/
int n, ans;
float find_avg(float, float, float);/*function that will find average*/

float find_avg(float tesavg, float quizavg, float hwavg)/*sum of all grades*/

 {
	 float tolavg;																										
	 tolavg = (.5 * tesavg + .3 * quizavg + .2 * hwavg);
	 return(tolavg);
 }

float calAve(void)
{
	int sum = 0;
	float temp;
			printf("Enter number of Grades:	");
			scanf("%i",&num);/*user input*/
			for(n = 1; n<= num; ++n)/*loop*/
			{
																														
				printf("Enter Grades");/*grades*/
				scanf("%i",&tg);/*user input*/

				sum = sum + tg;/*adds total values*/
			}
			temp = sum / (float)num;/*calculates the average*/
			return temp;
}
			

void main(void)
{
					/*intro*/                                                                                 
	printf("\n\n\n");/*space*/
	printf("Welcome to Your Gradebook\n\n");/*welcome*/
	printf("You will be asked to enter,the student Test,Quizs,Homework,Numbers and Grades  as well as the number of students, so that the average can be\n"); /*description of the program*/
	printf("calculated and the student can get a final grade.\n\n\n");/*continuation of the description of the program*/

				/*Asking for input*/
																													
	
		printf("\nEnter number of students:  ");/*number of student*/
		scanf("%i",&numofstds);/*user input*/
		
		
		printf("\n\n");/*space*/
		
		n=0;
		while(n < numofstds) /*loop*/
		{
			n = n + 1;
        printf("Please Choose a Number from The Following Menu\n\n");/*directions*/
		
		printf("1. To Enter Test Grades\n");
        
		printf("2. To Enter Quiz Grades\n");
        
		printf("3. To Enter Homework Grades\n");
        
		printf("4. For Summary Of Averages\n");
										  
		printf("5. To Quit\n\n");

		printf("\n\n");/*space*/
		
		printf("Enter a Number:   ");
		scanf("%i",&number);/*user input*/
		printf("\n");/*space*/
		switch(number){
			case 1:
				printf("Your choice is ( 1 )\n\n");
				printf("Enter Tests:	");
				tesavg = calAve();
				printf("Tests Ave\n");
				printf("%.2f",tesavg);

				break;
			case 2:
				printf("Your choice is ( 2 )\n\n");
				printf("Enter Quizs:   ");
				quizavg = calAve();
				printf("Quizs Ave\n");
				printf("%.2f",quizavg);

				break;																									
			case 3:
				printf("Your choice is ( 3 )\n\n");
				printf("Enter Homeworks:   ");
				hwavg = calAve();
                printf("Homeworks Ave\n");
				printf("%.2f",hwavg);

				break;
			case 4:
				printf("Your choice is ( 4 )\n\n");
				stdavg[n] = find_avg(tesavg, quizavg, hwavg);
				break;
			case 5:
				printf("Your choice is ( 5 )\n\n");
				printf("Will you like to Quit? 1 = Yes, 0 = NO\n");
			    scanf("%i", &number);
				if (number == 1){
					break;}
				
				break;
			default:
				printf("Please Choose a Number from the Menu...!\n\n");
				break;

			printf("\n\n")/*space*/;}
		    
		      //printf("The Student Average is %.2f  \n", stdavg[n]);/*Student average*/
			  //printf("Test ave   Quiz ave   Hwk ave   Overall ave\n\n");/*to display the table*/
			  //printf("%.2f      %.2f      %.2f      %.2f\n"tesavg, quizavg, hwavg, stdavg[n]);/*displays the value on the table*/
			
			

			  
			
			
																												
			
		}
		}
 

FreezeSSC

Member
just do a do/while loop, have a menu option of say 0, where 0 = exit, and then in the while condition put while (menuOption != 0)

or something like that.

**edit for a basic psuedo code example**

int menuOption;
do{

printf("1: enter Test grades\n");
printf("1: enter homework grades\n");
printf("1: enter quiz\n");
scanf("%d", menuOption);


.......
(insert code for doing each option here)

}
while (menuOption != 0)

im sure there's probably another way to go about it to.
 
Put all of the input handling/menu printing code in a function. Inside your main function have something like this:

Code:
while(bStillRunning)
{
    bStillRunning = display_menu();
}

display_menu would return 1 or 0 depending on if they quit, 0 meaning they quit.

EDIT: Beaten!
 

FreezeSSC

Member
jboldiga said:
Put all of the input handling/menu printing code in a function. Inside your main function have something like this:

Code:
while(bStillRunning)
{
    bStillRunning = display_menu();
}

display_menu would return 1 or 0 depending on if they quit, 0 meaning they quit.

EDIT: Beaten!


Your way is much simpler!
 

Zeppu

Member
What's been said is probably enough.

What I do suggest is that you split everything into the most basic constituents for ease of use

So:

DisplayMenu
AcceptInput


Also, all those global variables are ugly. I don't know about your syllabus but I would probably deduct marks for so many variables declared globally.

Just sayin.

Edit: Oh and CTRL+H '\n' -> '\r\n'

Edit 2: system("cls") before displaying menu :D
 

dojokun

Banned
I don't know how much time you have left, but I'd recommend concentrating on planning out your structure.

As mentioned above, don't use so many global variables. Since this is a basic class, you should be able to write the whole thing without a single global variable. Part of learning at the beginning stage is getting use to the scope of local variables.
 

Ophiuchus

Banned
Thanks for all the help so far guys. I have until Sunday night to finish this so I'll clean it up after I get all the parts functioning.

What's throwing me off now is that when you input the all the grades for one student. Instead of displaying a second line of grades. The original ones are being over written. for example instead of

Test quiz home work
90 70 66
70 60 50
50 30 70


The first line keeps getting over written no matter the number of students.
 

dojokun

Banned
Sinistar said:
Thanks for all the help so far guys. I have until Sunday night to finish this so I'll clean it up after I get all the parts functioning.

What's throwing me off now is that when you input the all the grades for one student. Instead of displaying a second line of grades. The original ones are being over written. for example instead of

Test quiz home work
90 70 66
70 60 50
50 30 70


The first line keeps getting over written no matter the number of students.
Use local variables instead of global variables. You'll have to declare them separately in each function.
 

CrankyJay

Banned
In your calAve function you'll have a divide by zero exception if the user enters 0 for the number of grades. You should put a check in there to make sure they can't enter a zero and prompt them to enter a value of at least 1.

edit: I also don't believe you need to type cast num using (float) ... if you divide a float or a double by an integer you should still get a fractional result.
 

Ophiuchus

Banned
Here is the semi final code I came to, for anyone thats interested. It all works the way its suppose to Just needs better spacing and making it run cleaner. I'm also gonna spend some time and eliminate the globals
thanks everyone for helping :)

Code:
#include<stdio.h>
#include "stdafx.h"

double classTotal;
int tg,qg,hwg,soa,l, number,num;/*grades,loop*/
int total,numofstds,stdnum,numoftes,numofquiz,numofhw;/*total sum of all grades,number of students,student number*/
float toltestval,tolquizval,tolhwval;/*total test value,total quiz value,total hwk value*/
float tesavg[50],hwavg[50],quizavg[50];/*test average,hwk average*/
double stdavg[50];/*sum of all grades*/
int ans;
float find_avg(float, float, float);/*function that will find average*/

float find_avg(float tesavg, float quizavg, float hwavg)/*sum of all grades*/

{
    float tolavg;                         
    tolavg = (.5 * tesavg + .3 * quizavg + .2 * hwavg);
    return(tolavg);
}

float calAve(void)
{
    int sum = 0;
    int n;
    float temp;
    printf("Enter number of assignments: ");
    scanf("%i",&num);/*user input*/
    for(n = 1; n<= num; ++n)/*loop*/
    {

        printf("Enter Grades");/*grades*/
        scanf("%i",&tg);/*user input*/

        sum = sum + tg;/*adds total values*/
    }
    temp = sum / (float)num;/*calculates the average*/
    return temp;
}


void main(void)
{
    /*intro*/           
    int n;
    printf("\n\n\n");/*space*/
    printf("Welcome to Your Gradebook\n\n");/*welcome*/
    printf("You will be asked to enter,the student Test,Quizs,Homework,Numbers and Grades\n"); 
	printf("as well as the number of students, so that the average can be\n"); /*description of the program*/
    printf("calculated and the student can get a final grade.\n\n\n");/*continuation of the description of the program*/

    /*Asking for input*/


    printf("\nEnter number of students:  ");/*number of student*/
    scanf("%i",&numofstds);/*user input*/

    n = 0;
    classTotal = 0;
    while (n < numofstds) {
       printf("Enter Tests: ");
       tesavg[n] = calAve();
       printf("tests Ave\n");
       printf("%.2f",tesavg);
      
       printf("Enter Quizs:   ");
       quizavg[n] = calAve();
       printf("Quizs Ave\n");
       printf("%.2f",quizavg);
      
       printf("Enter Homeworks:   ");
       hwavg[n] = calAve();
       printf("Homeworks Ave\n");
       printf("%.2f",hwavg);

       stdavg[n] = find_avg(tesavg[n], quizavg[n], hwavg[n]);
       classTotal += stdavg[n];
        printf("The Student Average is %.2f  \n", stdavg[n]);/*Student average*/
        printf("Test ave   Quiz ave   Hwk ave   Overall ave\n\n");/*to display the table*/
        printf("%.2f      %.2f      %.2f      %.2f\n",tesavg[n], quizavg[n], hwavg[n], stdavg[n]);/*displays the value on the table*/
       n++;
    }
   
    printf("Test ave   Quiz ave   Hwk ave   Overall ave\n\n");/*to display the table*/
    for (n = 0; n < numofstds; n++) {
        printf("%.2f      %.2f      %.2f      %.2f\n",tesavg[n], quizavg[n], hwavg[n], stdavg[n]);/*displays the value on the table*/
    }
    printf("class overall average: %.2f\n",(classTotal/numofstds));
}
 
Status
Not open for further replies.
Top Bottom