Hello guys. I'm currently having problems concerning displaying a student with the highest grade. I tried to start the calculation under totals but I'm currently having a hard time with it. May someone please point me in the right direction.

/*+*+*+*+*+*+*+* Preprocessor Directives *+*+*+*+*+*+*+*/


#include <stdio.h>             
#include <string.h>
#include <iostream>



/*+*+*+*+*+*+*+* Function Prototypes *+*+*+*+*+*+*+*/

void WelcomeInfo (void);									/* Welcomes the user */
void AskQuestion(void);										/* Asks the user a question */
void GetClass(void);											/* Gets the input */
void GetStudentGrade(void);
void NameofStudent(void);		
void PerformCalc(void);									/* Performs the calculation */								
void GradeDisplay(void);
void Totals(void);
void WeightedGrade(void);
void AskQuestion2(void);									/* Asks the user a question */
void PrintAStarLine(void);
void TotalPageDisplay(void);

/*+*+*+*+*+*+*+* Variables *+*+*+*+*+*+*+*/

void Font( const char *Input, char *Output )
{

     int Length, Size;
     Length = strlen( Input );
     
     for( Size=0; Size<Length; Size++ )
     {
          if( Input[Size]>='a' && Input[Size]<='z' )
                    Output[Size]=Input[Size]-32; 
          else
                    Output[Size]=Input[Size];    
     }

     Output[Size]=0x00;

}


float test1;
float test2;
float project1;
float project2;
float midterm;
float final;
float tests;
float projects;
float midtermexam;
float finalexam;
float studentgrade;


int bonus = 0;
float totalaverage;
int totalstudents;

int studentA = 0;
int studentB = 0;
int studentC = 0;
int studentD = 0;
int studentF = 0;

char newline;

char courseNumber[10];
char cName;
char cMore;
char Name[30];

char StudentName[30];
char StudentfirstName[15];
char StudentlastName[15];
char studentandgrade;
char HighestAverage;
char K;


/*+*+*+*+*+*+*+*^ The Main Module ^*+*+*+*+*+*+*+*/

main()
{	


	cName = 'N'; 


	do
	{
   WelcomeInfo();
   
   
   AskQuestion();
	  

   } while ((cName != 'Y') && (cName != 'y'));   
	

	cMore = 'Y';
	do
	{

	
	GetClass(); 

	NameofStudent();
	system("cls");
	GetStudentGrade();
	system("cls");
	PerformCalc();
	WeightedGrade();
	system("cls");
	GradeDisplay();
	
	AskQuestion2();
	Totals();

									
	}while ((cMore != 'N') && (cMore != 'n'));		/* It will loop until user enter N */
	
	
	TotalPageDisplay();
	
	
	
	return 0;
}

/*------------------------------------------------------------
						Welcomes the user
  ------------------------------------------------------------*/

void WelcomeInfo (void)
{
	
	
	printf("Enter your name please.   ");
	scanf( "%[^\n]%c", Name, &newline );
	
	printf("\n\n\n");
	
	printf("\t\t\t\t Welcome %s", Name);
	printf("\n\n\n");

	return;
}

/*------------------------------------------------------------
		Asks the user if the name entered is correct
  ------------------------------------------------------------*/

void AskQuestion(void)
{
	printf("\n\nIs this your name? (Y/N)");							/* prints the description */
	scanf(" %c", &cName);											/* get the input */
	fflush(stdin);													/* flush the CRLF */

    
	return;
}



/*------------------------------------------------------------
				Asks the user to enter in coursenumber
  ------------------------------------------------------------*/

void GetClass(void)
{

	/***** Course Number *****/
	


	char aCourseNumber[10];

	
	printf("\n\nEnter one of the following course numbers. ");
	printf("\n");
	printf("(ABC101, HIS220, HOT707, ECO122, PLE888)\n ");
	scanf(" %s", &aCourseNumber); 
	
	Font( aCourseNumber, courseNumber );


	if(strcmp(courseNumber,"ABC101")==0)
	{
	printf("\n			Welcome to class ABC101 \n");
	printf("\n\n\n");
	}
	else if(strcmp(courseNumber,"HIS220")==0)
	{
	printf("\n			Welcome to class HIS220 \n");
	printf("\n\n\n");
	}
	else if(strcmp(courseNumber,"HOT707")==0)
	{
	printf("\n			Welcome to class HOT707 \n");
	printf("\n\n\n");
	}
	else if(strcmp(courseNumber,"EC0122")==0)
	{
	printf("\n			Welcome to class ECO122 \n");
	printf("\n\n\n");
	}
	else if(strcmp(courseNumber,"PLE888")==0)
	{
	printf("\n			Welcome to class PLE888 \n");
	printf("\n\n\n");
	}
	else
	{  
	printf("\n	That course number does not exist, please try again. \n");
	printf("\n\n\n");
	GetClass();
	}
	return;
}


/*------------------------------------------------------------
			Performs grade calculation for each course
  ------------------------------------------------------------*/

void WeightedGrade(void)
{
	if(strcmp(courseNumber,"ABC101")==0)
	{
	studentgrade = ((tests * .23) + (projects * .27) + (midtermexam * .20) + (finalexam * .30) + (bonus));
	}
	else if(strcmp(courseNumber,"HIS220")==0)
	{
	studentgrade = ((tests * .20) + (projects * .30) + (midtermexam * .25) + (finalexam * .25) + (bonus));
	}
	else if(strcmp(courseNumber,"HOT707")==0)
	{
	studentgrade = ((tests * .25) + (projects * .50) + (midtermexam * .15) + (finalexam * .10) + (bonus));
	}
	else if(strcmp(courseNumber,"EC0122")==0)
	{
	studentgrade =((tests * .25) + (projects * .25) + (midtermexam * .20) + (finalexam * .30) + (bonus));
	}
	else if(strcmp(courseNumber,"PLE888")==0)
	{
	studentgrade =  ((tests * .30) + (projects * .20) + (midtermexam * .25) + (finalexam * .25) + (bonus));
	} 

	system("cls");

	return;

}


/*------------------------------------------------------------
		Asks the user to enter in the student's name
  ------------------------------------------------------------*/	

void NameofStudent(void)
{

	
	printf("Enter the student's First Name.   ");						
	scanf(" %13[^\n]",StudentfirstName);
	fflush(stdin);
	
	printf("Enter the student's Last Name.   ");						
	scanf(" %13[^\n]",StudentlastName);
	fflush(stdin);

	strcpy(StudentName,StudentfirstName);       
	strcat(StudentName," ");
	strcat(StudentName,StudentlastName);
	system("cls");


	return;

}


/*------------------------------------------------------------
			Asks the user to enter in students' grades
  ------------------------------------------------------------*/

void GetStudentGrade(void)
{
	printf("                     Enter the grades of %s  \n", StudentName);
	printf("\n\n\n");
	

	printf("Enter grade for Test1: ");
	scanf(" %f", &test1);
	

	printf("Enter grade for Test2: ");
	scanf(" %f", &test2);

	printf("Enter grade for Project1: ");
	scanf(" %f", &project1);

	printf("Enter grade for Project2: ");
	scanf(" %f", &project2);

	printf("Enter grade for Midterm: ");
	scanf(" %f", &midterm);

	printf("Enter grade for Final: ");
	scanf(" %f", &final);

	printf("Number of Bonus Projects: ");
	scanf(" %d", &bonus);


	fflush(stdin);
	system("cls");
	return;
}


/*------------------------------------------------------------
					Performs student calculation
  ------------------------------------------------------------*/


void PerformCalc(void)
{
	
	tests = (test1 + test2) / 2;
	projects =(project1 + project2) / 2;
	midtermexam =(midterm);  
	finalexam =(final);          
		if (bonus >= 1 && bonus <= 5)
	{

		bonus = 1;
	}
	else if (bonus >= 6 && bonus <= 9)
	{
		bonus =  3;
	}
	else if (bonus >= 10 && bonus <= 12)
	{
		bonus = 4;
	}
	else if(bonus >= 13)
	{
		bonus = 6;
	}
	else 
	{
		bonus = 0;
	}


	return;

}

/*------------------------------------------------------------
			Performs total calculation for all students
  ------------------------------------------------------------*/

void Totals(void)
{	

	 if (studentgrade > 90)
		studentA = studentA + 1;

	 else if (studentgrade >= 80 && studentgrade <= 89)
		studentB = studentB + 1;
	
	 else if (studentgrade >= 72 && studentgrade <=79)
		studentC = studentC + 1;
	

	 else if (studentgrade >= 70 && studentgrade <= 71)
		studentD = studentD + 1;
	

	 else if (studentgrade <= 69)
		studentF = studentF + 1;

	


	totalstudents = (studentA + studentB + studentC + studentD + studentF);
	totalaverage = totalaverage + studentgrade;
	
	if (studentgrade > studentgrade)
		StudentName =  HighestAverage;
	

	
	return;

}


/*------------------------------------------------------------
				Displays the students' grade
  ------------------------------------------------------------*/

void GradeDisplay(void)
{
	printf("Test Scores: %10.3f\n", tests);
	printf("Project Scores: %10.3f\n", projects);
	printf("Midterm Score: %10.3f\n", midtermexam);
	printf("Final Score: %10.3f\n", finalexam);
	printf("Bonus Points: %d\n", bonus);
	printf("Student Grade: %10.3f\n", studentgrade);
	fflush(stdin);	

	return;

}

/*------------------------------------------------------------
					Asks the user a question
  ------------------------------------------------------------*/

void AskQuestion2(void)
{
	printf("\n\nWould you like to enter in grades again? (Y/N)");		/* print description */
	scanf(" %c", &cMore);												/* get the input */
	fflush(stdin);														/* flush the CRLF */

    
	return ;
}

/*------------------------------------------------------------
					Prints A Line on Page
  ------------------------------------------------------------*/

void PrintAStarLine(void)

{

	  printf("******************************************************************************** \n");
      printf("\n\n");

	  return;
	
}

/*------------------------------------------------------------
		Displays the overall grade for each student
  ------------------------------------------------------------*/


void TotalPageDisplay(void)
{
	totalaverage =  totalaverage / (totalstudents);

	system("cls");
	PrintAStarLine();
	printf("\t\t\t\tSchool Grade Totals  \n");
	printf("\n\n");
	PrintAStarLine();
	printf("Grade\t\t\t Total Students  \n", studentandgrade);
	printf("A\t\t\t	 %d\n",  studentA);
	printf("B\t\t\t	 %d\n",  studentB);
	printf("C\t\t\t	 %d\n",  studentC);
	printf("D\t\t\t	 %d\n",  studentD);
	printf("F\t\t\t	 %d\n",  studentF);
	printf("\n\n");



	printf("__________________________________________________________________________  \n");

	printf("GrandTotal number of Students:				  %d\n", totalstudents);
	printf("\n");

	printf("Overall average of all students:			%6.2f\n", totalaverage);
	printf("\n");
	
	printf("Student with highest grade:				%s\n", HighestAverage);
	printf("\n");

	return;

}

Recommended Answers

All 16 Replies

Member Avatar for iamthwee

> I'm currently having problems concerning displaying a student with the highest grade.

It's pretty obvious.

Create a value at the start of the loop.

Go through each value and if the current value is greater than the original update it. At the end you should be left with the biggest one.

It's pretty obvious.

Just because it's obvious to you doesn't make it obvious to everyone. ;)

How should I go about doing so, may you show me an example please. Also where I have
"if (studentgrade > studentgrade)
StudentName = HighestAverage;"
doesnt work. It says cant convert from char to char.

Member Avatar for iamthwee

No, you learn nothing that way.

Get yourself a piece of paper and draw how it might look as a flow chart. Once you have done that, create a separate program to test your idea.

That way you know nothing else may be influencing the end result. If you are still stuck after that, post back here.

Hehe, the size of this code is already most of my gtk asteroids, this shows how inefficient the code may be...

Since when does size reflect efficiency? :confused:

Concerning the code, it often does...

Then why is quicksort so much better than bubblesort? :D It's at least four or five times bigger, but in efficiency there's no comparison. The amount of code doesn't reflect efficiency; the quality of the code does. Quality isn't inversely proportional to size. ;)

Member Avatar for iamthwee

There is no need to hijack other people's thread. If you want to discuss your opinons use private mail.

If one can do things shortly, then the code often does much more things than it was intended to.

Iamthwee, i don't quite understand, who owns the threads here, when they are "other people's threads" and when one can hijack them. I don't know, maybe here is some secret club who owns the threads, which i was not aware of, so far i thought that anyone can write here... Please at least avoid writing nonsense, and using offensive and inappropriate words like "hijacking". Not that i so much care, but i think it would be more pleasant for other people not to see such offensive language here, thank you.

Thanks for the help guys, I see the problem but I'm still having a problem with getting it to work. I know that the HighestAverage = StudentName is my main problem. Just really needed to know how to get a char to equal another char and I could do the rest. I'm going to ask my teacher how I should get around doing this and then repost on here the mistake I made so others that may go through the same problem can understand.

Well i'm not completely sure what you exactly want to do, the biggest problem with your code is that there's so much vertical space everywhere, it's right that it's good to distinguish parts of the code, but it's not good to do it too much. It's equally important in making the code readable, that it was easier to view the code, so that a function, or at least every distinct part of it, would have a length not more than a single screenful. There were also several other things not so right in your code, but it would be too long to write all that here... Nevertheless i guess something like this:

static int previousgrade;
if (studentgrade > previousgrade)
	strcpy (HighestAverage,
		StudentName);
previousgrade = studentgrade;

At that, HighestAverage should also be a char array of appropriate size, it was just char in your code. Also notice what static int previousgrade means. When we declare a variable static, then it means that this variable exists during all program execution. In spite that we do that inside a function, it is not the same as variables without any specifier declared inside a function, these variables exist only from calling the function until returning from it. But in spite of that, previousgrade has a block scope, because it is declared only inside the function, which means that we can have a global variable or variable inside another function, with the same name. It is good to have as few global variables as possible, but of course the local variables should not be static, whenever possible. Otherwise such static variable is the same as global variable, global variables are also static, but would have a file scope when declared as static. All static variables and array or structure members are also initialized in the beginning of the program, to zero or to null pointer. Which in that case means that the initial value of previousgrade is 0, when we don't initialize it ourselves.

In fact, i only wanted to joke, i really didn't expect that the reaction to that is the accusation in "hijacking". I thought i was among friends, but found that i'm among enemies. I'm very disappointed in you indeed, and i think that you well understand that i have a serious reason for that.

Sorry about not replying quick enough. I was able to figure the problem out thanks to your help and my teacher's. The last thing I wanted to know how to do is be able to have a character display a character. For example if I enter in a number greater than 90, I want it to display A. May someone help me out on this please. This just an example of how it should look like, I have the coding for the student grade, just didnt display it here.

char studentgrade;
char lettergrade[1];
char A[1];

main()

if (studentgrade > 90)
	 {
	lettergrade = A;
	 }
printf("%s", lettergrade);
Member Avatar for iamthwee

Maybe?

#include <stdio.h>
 
int main ( void )
{
    char lettergrade;
    lettergrade = 'A';
    printf("%c", lettergrade);
 
    getchar();
    return 0;
}
char studentgrade;
char lettergrade[1];
char A[1];

There's no point having an array of one character..

and there's no point storing seperate variables for each grade, you could just do this (note the single-quote marks)

if (studentgrade > 90)
	 {
	lettergrade = 'A';
	 }

You're on the right lines with the if (studentgrade > 90 ) stuff.

All you need is a few else-ifs for 80, 70, 60, etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.