I really suck at understanding the while loop, can anyone see whats wrong, i would like to write a gpa calculator as user inputs letters A,B,C,D,E,F it gives out the gpa!
Here is the code!
int main()
{ char x;
int num_grades;
float total;
printf("Please input grades (A,B,C,D or F) to calculate your GPA!\n");
scanf("%c",%26amp;x);
while (num_grades%26lt;=6)
{num_grades++;
switch (x)
{ case 'A':
total=total+4;
break;
case 'B':
total=total+3;
break;
case 'C':
total=total+2;
break;
case 'D': total=total+1;
break;
case 'F':
total=total+0;
break;
default:
printf("invalid input\n");
}
}
printf("Total number of grades inputed is
C-Programing: Writing a GPA calculator!?
There are a few issues with your code:
1. Uninitialized variables
2. scanf is outside the while loop.
3. not decrementing the count.
4. With scanf, you also have to read the rest of the input to eliminate chars until newline. So, if somebody entered 'Azx', then, you need to eliminate 'zx' and the newline.
Here is a suggestion:
#include %26lt;stdio.h%26gt;
int points[] = {4, 3, 2, 1,0};
int main()
{
char x = 'z';
char newline = 'z';
int num_grades = 0;
float total = 0.0;
while (x != 'Q')
{
printf("Enter grade (A,B,C,D or F) - Q to exit: ");
scanf("%c", %26amp;x);
/*
* Harvest and throw away the rest of the characters in the input
*/
while (newline != '\n')
{
scanf("%c", %26amp;newline);
}
newline = 'z';
x = (int) toupper(x);
if ((x%26gt;='A') %26amp;%26amp; (x%26lt;='F') %26amp;%26amp; (x!='E'))
{
num_grades++;
total = total + points[x-'A'];
}
else
{
if (x != 'Q')
printf("%c is invalid\n", x);
}
}
if (num_grades %26gt; 0)
{
printf("Number of grades: %d\n", num_grades);
printf("GPA : %.2f\n", total/num_grades);
}
return 0;
}
Reply:Can you add some more information on what is not working?
If you have to use switch, here's a mod. that shd work.
num_grades++;
switch (x)
{
case 'A':
total += 4;
break;
...
....
case 'Q':
num_grades--;
break;
default:
num_grades--;
printf("%c is invalid\n", x);
break;
} Report It
Reply:#include %26lt;iostream.h%26gt;
int main()
{
while(1){
char grade;
cout%26lt;%26lt;"Enter the grade for GPA value\n";
cin%26gt;%26gt;grade;
if(grade=='A' || grade=='a'){
cout%26lt;%26lt;"GPA value is 4\n";
}else if(grade=='B' || grade=='b'){
cout%26lt;%26lt;"GPA value is 3\n";
}else if(grade=='C' || grade=='c'){
cout%26lt;%26lt;"GPA value is 2\n";
}else if(grade=='D' || grade=='d'){
cout%26lt;%26lt;"GPA value is 1\n";
}else if(grade=='F' || grade=='f'){
cout%26lt;%26lt;"GPA value is 0\n";
}else{
cout%26lt;%26lt;"Wrong input\n";
}
}
return 0;
}
Reply:i can't read all of your code but there is some programming mistake u must initialize total,num_grades (i think both of must be zero at the declaration )
Reply:Looks like you're almost there... perhaps the end of your source was truncated?
Note that when you get an invalid input, you should subtract one from the num_grades - if not, you could produce incorrect results.
At the end, print out the number of grades input (num_grades) and then print the GPA which is the total divided by the number of grades.
Reply:KCNY1's answer is nice. It eliminates the tedious switch!
Reply:Your code assumes that num_grades and total automagically starts out as 0. It doesn't. You have to do that explicitly.
int num_grades = 0;
float total = 0.0;
Your while loop will probably never be executed because your uninitialized num_grades starts off as some big garbage number (greater than 6)
Once you get your while loop running, you can move ahead with fixing scanf. Decide whether you will have your user enter grades one at a time, or enter a list of grades. If one at a time, the user prompt and scanf should be inside your while loop. If you want them to enter a formatted list, you'll need to do some work to parse the list (find the significant letters and ignore spaces and commas, etc)
Good luck!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment