d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Gpa Calculator In Basic C > Coding Is Done With A Few Errors
Add Reply New Topic New Poll
Member
Posts: 51
Joined: Feb 7 2013
Gold: 0.00
Mar 16 2013 10:19am
my assignment is:
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.

my problem is :
my program doesn't ask to calculate current gpa or another gpa otherwise it's all fine and dandy
and it also allows me to enter above 4.0 :/

Code
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
int index=-1,k=0;
int main()
{

char inp[256]="   ";
double gpa[30];
double totalgpa,avegpa;
printf("***** Welcome To GPA Calculator ***** \n");
while(inp[0]!='q')
{
printf("%s%d%s","Enter The Next GPA Value(",index+1,") or Enter q to stop \n"   );
gets(inp);
if(inp[0]=='q'|| index==29)
{
break;
}

gpa[++index]=atof(inp);
}
printf("calculating Average GPA...\n" );

for( k=0;k<index+1;k++)
{
totalgpa+=gpa[k];

}
avegpa=totalgpa/(index+1);
printf("%s%f%s%f","Total GPA Is ",totalgpa," Average GPA Is ",avegpa);

getch();
}






//code ends here
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 16 2013 12:57pm
Quote (Xarai @ Mar 16 2013 12:19pm)
my assignment is:
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.

my problem is :
my program doesn't ask to calculate current gpa or another gpa otherwise it's all fine and dandy
and it also allows me to enter above 4.0 :/

Code
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
int index=-1,k=0;
int main()
{

char inp[256]="   ";
double gpa[30];
double totalgpa,avegpa;
printf("***** Welcome To GPA Calculator ***** \n");
while(inp[0]!='q')
{
printf("%s%d%s","Enter The Next GPA Value(",index+1,") or Enter q to stop \n"   );
gets(inp);
if(inp[0]=='q'|| index==29)
{
break;
}

gpa[++index]=atof(inp);
}
printf("calculating Average GPA...\n" );

for( k=0;k<index+1;k++)
{
totalgpa+=gpa[k];

}
avegpa=totalgpa/(index+1);
printf("%s%f%s%f","Total GPA Is ",totalgpa," Average GPA Is ",avegpa);

getch();
}






//code ends here


this chunk of code is horribly made. you do not need those two global variables. your inp variable can be removed and useless and it is a sure way to get a buffer overflow. and the reason your code isnt looping over and over again is because its not in a loop. your way of using printf is horrible as well. you should be mixing your text along with your %f so taht its easier to read and understand instead of going %s%f%s%f%s%f

also your code isnt returning a integer at the end. what compiler are you using most of them will get mad at you if you do that

This post was edited by AbDuCt on Mar 16 2013 12:58pm
Member
Posts: 15,988
Joined: Nov 12 2005
Gold: 4,399.00
Mar 16 2013 01:54pm
What so difficult logic? :wacko:
Member
Posts: 51
Joined: Feb 7 2013
Gold: 0.00
Mar 16 2013 08:06pm
Quote (dimon222 @ Mar 16 2013 02:54pm)
What so difficult logic?  :wacko:


is not that bad >.>
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll