Need help writing my code for creating an average grade calculator with C [on hold]
$begingroup$
I'm currently a beginner at C programming. I'm trying to create a calculator where the user inputs letter grades and it outputs the average letter grade AND average GPA.
This is nearly complete except I cannot figure out why the "Average letter grade is:" output is not functioning correctly. It works for when A is the average, and when B is the average. But whenever you enter a C, D, or F average it prints out "Average letter grade is: B" no matter what.
I'm hoping someone can help out, thanks.
PS sorry if this isn't where I should be posting this. I'm new here as well.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
while (true){
unsigned int aCount = 0;
unsigned int bCount = 0;
unsigned int cCount = 0;
unsigned int dCount = 0;
unsigned int fCount = 0;
puts("Enter the letter grades.");
puts("Enter EOF to end input.");
int grade;
while ((grade = getchar()) != EOF) {
switch (grade) {
case 'A':
case 'a':
++aCount;
break;
case 'B':
case 'b':
++bCount;
break;
case 'C':
case 'c':
++cCount;
break;
case 'D':
case 'd':
++dCount;
break;
case 'F':
case 'f':
++fCount;
break;
case 'n':
case 't':
case ' ':
break;
default:
printf("%s", "Incorrect letter grade entered.n");
puts("Enter a new grade.");
break;
}
}
puts("nTotals for each letter grade are:");
printf("A: %un", aCount);
printf("B: %un", bCount);
printf("C: %un", cCount);
printf("D: %un", dCount);
printf("F: %un", fCount);
int gradeCount = aCount+bCount+cCount+dCount+fCount;
float aGPA = aCount*4.0;
float bGPA = bCount*3.0;
float cGPA = cCount*2.0;
float dGPA = dCount*1.0;
float fGPA = fCount*0.0;
float totalGPA = aGPA + bGPA + cGPA + dGPA + fGPA;
float avgGPA = totalGPA / gradeCount;
printf("Average GPA is: %fn", avgGPA);
if (avgGPA >= 3.5) {
printf("Average letter grade is: Ann");
}
else if (3.0 <= avgGPA < 3.5) {
printf("Average letter grade is: Bnn");
}
else if (2.0 <= avgGPA < 3.0) {
printf("Average letter grade is: Cnn");
}
else if (1.0 <= avgGPA < 2.0) {
printf("Average letter grade is: Dnn");
}
else {
printf("Average letter grade is: Fnn");
}
}
}
c
New contributor
$endgroup$
put on hold as off-topic by Sᴀᴍ Onᴇᴌᴀ, Vogel612♦ 6 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Sᴀᴍ Onᴇᴌᴀ, Vogel612
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
$begingroup$
I'm currently a beginner at C programming. I'm trying to create a calculator where the user inputs letter grades and it outputs the average letter grade AND average GPA.
This is nearly complete except I cannot figure out why the "Average letter grade is:" output is not functioning correctly. It works for when A is the average, and when B is the average. But whenever you enter a C, D, or F average it prints out "Average letter grade is: B" no matter what.
I'm hoping someone can help out, thanks.
PS sorry if this isn't where I should be posting this. I'm new here as well.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
while (true){
unsigned int aCount = 0;
unsigned int bCount = 0;
unsigned int cCount = 0;
unsigned int dCount = 0;
unsigned int fCount = 0;
puts("Enter the letter grades.");
puts("Enter EOF to end input.");
int grade;
while ((grade = getchar()) != EOF) {
switch (grade) {
case 'A':
case 'a':
++aCount;
break;
case 'B':
case 'b':
++bCount;
break;
case 'C':
case 'c':
++cCount;
break;
case 'D':
case 'd':
++dCount;
break;
case 'F':
case 'f':
++fCount;
break;
case 'n':
case 't':
case ' ':
break;
default:
printf("%s", "Incorrect letter grade entered.n");
puts("Enter a new grade.");
break;
}
}
puts("nTotals for each letter grade are:");
printf("A: %un", aCount);
printf("B: %un", bCount);
printf("C: %un", cCount);
printf("D: %un", dCount);
printf("F: %un", fCount);
int gradeCount = aCount+bCount+cCount+dCount+fCount;
float aGPA = aCount*4.0;
float bGPA = bCount*3.0;
float cGPA = cCount*2.0;
float dGPA = dCount*1.0;
float fGPA = fCount*0.0;
float totalGPA = aGPA + bGPA + cGPA + dGPA + fGPA;
float avgGPA = totalGPA / gradeCount;
printf("Average GPA is: %fn", avgGPA);
if (avgGPA >= 3.5) {
printf("Average letter grade is: Ann");
}
else if (3.0 <= avgGPA < 3.5) {
printf("Average letter grade is: Bnn");
}
else if (2.0 <= avgGPA < 3.0) {
printf("Average letter grade is: Cnn");
}
else if (1.0 <= avgGPA < 2.0) {
printf("Average letter grade is: Dnn");
}
else {
printf("Average letter grade is: Fnn");
}
}
}
c
New contributor
$endgroup$
put on hold as off-topic by Sᴀᴍ Onᴇᴌᴀ, Vogel612♦ 6 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Sᴀᴍ Onᴇᴌᴀ, Vogel612
If this question can be reworded to fit the rules in the help center, please edit the question.
1
$begingroup$
Welcome to Code Review! Unfortunately, this is off-topic here since we only review working code. You would need to ask this on Stack Overflow. That said, code like3.0 <= avgGPA < 3.5
doesn't work the way you think. You would need to test those conditions separately (i.e.3.0 <= avgGPA && avgGPA < 3.5
). In this case, though, you already know thatavgGPA
is less than the upper limit since the previousif
clause(s) were false, so you can just use3.0 <= avgGPA
, etc.
$endgroup$
– Null
7 hours ago
$begingroup$
Thanks that fixed it.
$endgroup$
– James C Stup Jr.
6 hours ago
add a comment |
$begingroup$
I'm currently a beginner at C programming. I'm trying to create a calculator where the user inputs letter grades and it outputs the average letter grade AND average GPA.
This is nearly complete except I cannot figure out why the "Average letter grade is:" output is not functioning correctly. It works for when A is the average, and when B is the average. But whenever you enter a C, D, or F average it prints out "Average letter grade is: B" no matter what.
I'm hoping someone can help out, thanks.
PS sorry if this isn't where I should be posting this. I'm new here as well.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
while (true){
unsigned int aCount = 0;
unsigned int bCount = 0;
unsigned int cCount = 0;
unsigned int dCount = 0;
unsigned int fCount = 0;
puts("Enter the letter grades.");
puts("Enter EOF to end input.");
int grade;
while ((grade = getchar()) != EOF) {
switch (grade) {
case 'A':
case 'a':
++aCount;
break;
case 'B':
case 'b':
++bCount;
break;
case 'C':
case 'c':
++cCount;
break;
case 'D':
case 'd':
++dCount;
break;
case 'F':
case 'f':
++fCount;
break;
case 'n':
case 't':
case ' ':
break;
default:
printf("%s", "Incorrect letter grade entered.n");
puts("Enter a new grade.");
break;
}
}
puts("nTotals for each letter grade are:");
printf("A: %un", aCount);
printf("B: %un", bCount);
printf("C: %un", cCount);
printf("D: %un", dCount);
printf("F: %un", fCount);
int gradeCount = aCount+bCount+cCount+dCount+fCount;
float aGPA = aCount*4.0;
float bGPA = bCount*3.0;
float cGPA = cCount*2.0;
float dGPA = dCount*1.0;
float fGPA = fCount*0.0;
float totalGPA = aGPA + bGPA + cGPA + dGPA + fGPA;
float avgGPA = totalGPA / gradeCount;
printf("Average GPA is: %fn", avgGPA);
if (avgGPA >= 3.5) {
printf("Average letter grade is: Ann");
}
else if (3.0 <= avgGPA < 3.5) {
printf("Average letter grade is: Bnn");
}
else if (2.0 <= avgGPA < 3.0) {
printf("Average letter grade is: Cnn");
}
else if (1.0 <= avgGPA < 2.0) {
printf("Average letter grade is: Dnn");
}
else {
printf("Average letter grade is: Fnn");
}
}
}
c
New contributor
$endgroup$
I'm currently a beginner at C programming. I'm trying to create a calculator where the user inputs letter grades and it outputs the average letter grade AND average GPA.
This is nearly complete except I cannot figure out why the "Average letter grade is:" output is not functioning correctly. It works for when A is the average, and when B is the average. But whenever you enter a C, D, or F average it prints out "Average letter grade is: B" no matter what.
I'm hoping someone can help out, thanks.
PS sorry if this isn't where I should be posting this. I'm new here as well.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
while (true){
unsigned int aCount = 0;
unsigned int bCount = 0;
unsigned int cCount = 0;
unsigned int dCount = 0;
unsigned int fCount = 0;
puts("Enter the letter grades.");
puts("Enter EOF to end input.");
int grade;
while ((grade = getchar()) != EOF) {
switch (grade) {
case 'A':
case 'a':
++aCount;
break;
case 'B':
case 'b':
++bCount;
break;
case 'C':
case 'c':
++cCount;
break;
case 'D':
case 'd':
++dCount;
break;
case 'F':
case 'f':
++fCount;
break;
case 'n':
case 't':
case ' ':
break;
default:
printf("%s", "Incorrect letter grade entered.n");
puts("Enter a new grade.");
break;
}
}
puts("nTotals for each letter grade are:");
printf("A: %un", aCount);
printf("B: %un", bCount);
printf("C: %un", cCount);
printf("D: %un", dCount);
printf("F: %un", fCount);
int gradeCount = aCount+bCount+cCount+dCount+fCount;
float aGPA = aCount*4.0;
float bGPA = bCount*3.0;
float cGPA = cCount*2.0;
float dGPA = dCount*1.0;
float fGPA = fCount*0.0;
float totalGPA = aGPA + bGPA + cGPA + dGPA + fGPA;
float avgGPA = totalGPA / gradeCount;
printf("Average GPA is: %fn", avgGPA);
if (avgGPA >= 3.5) {
printf("Average letter grade is: Ann");
}
else if (3.0 <= avgGPA < 3.5) {
printf("Average letter grade is: Bnn");
}
else if (2.0 <= avgGPA < 3.0) {
printf("Average letter grade is: Cnn");
}
else if (1.0 <= avgGPA < 2.0) {
printf("Average letter grade is: Dnn");
}
else {
printf("Average letter grade is: Fnn");
}
}
}
c
c
New contributor
New contributor
New contributor
asked 7 hours ago
James C Stup Jr.James C Stup Jr.
1
1
New contributor
New contributor
put on hold as off-topic by Sᴀᴍ Onᴇᴌᴀ, Vogel612♦ 6 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Sᴀᴍ Onᴇᴌᴀ, Vogel612
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by Sᴀᴍ Onᴇᴌᴀ, Vogel612♦ 6 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Sᴀᴍ Onᴇᴌᴀ, Vogel612
If this question can be reworded to fit the rules in the help center, please edit the question.
1
$begingroup$
Welcome to Code Review! Unfortunately, this is off-topic here since we only review working code. You would need to ask this on Stack Overflow. That said, code like3.0 <= avgGPA < 3.5
doesn't work the way you think. You would need to test those conditions separately (i.e.3.0 <= avgGPA && avgGPA < 3.5
). In this case, though, you already know thatavgGPA
is less than the upper limit since the previousif
clause(s) were false, so you can just use3.0 <= avgGPA
, etc.
$endgroup$
– Null
7 hours ago
$begingroup$
Thanks that fixed it.
$endgroup$
– James C Stup Jr.
6 hours ago
add a comment |
1
$begingroup$
Welcome to Code Review! Unfortunately, this is off-topic here since we only review working code. You would need to ask this on Stack Overflow. That said, code like3.0 <= avgGPA < 3.5
doesn't work the way you think. You would need to test those conditions separately (i.e.3.0 <= avgGPA && avgGPA < 3.5
). In this case, though, you already know thatavgGPA
is less than the upper limit since the previousif
clause(s) were false, so you can just use3.0 <= avgGPA
, etc.
$endgroup$
– Null
7 hours ago
$begingroup$
Thanks that fixed it.
$endgroup$
– James C Stup Jr.
6 hours ago
1
1
$begingroup$
Welcome to Code Review! Unfortunately, this is off-topic here since we only review working code. You would need to ask this on Stack Overflow. That said, code like
3.0 <= avgGPA < 3.5
doesn't work the way you think. You would need to test those conditions separately (i.e. 3.0 <= avgGPA && avgGPA < 3.5
). In this case, though, you already know that avgGPA
is less than the upper limit since the previous if
clause(s) were false, so you can just use 3.0 <= avgGPA
, etc.$endgroup$
– Null
7 hours ago
$begingroup$
Welcome to Code Review! Unfortunately, this is off-topic here since we only review working code. You would need to ask this on Stack Overflow. That said, code like
3.0 <= avgGPA < 3.5
doesn't work the way you think. You would need to test those conditions separately (i.e. 3.0 <= avgGPA && avgGPA < 3.5
). In this case, though, you already know that avgGPA
is less than the upper limit since the previous if
clause(s) were false, so you can just use 3.0 <= avgGPA
, etc.$endgroup$
– Null
7 hours ago
$begingroup$
Thanks that fixed it.
$endgroup$
– James C Stup Jr.
6 hours ago
$begingroup$
Thanks that fixed it.
$endgroup$
– James C Stup Jr.
6 hours ago
add a comment |
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
1
$begingroup$
Welcome to Code Review! Unfortunately, this is off-topic here since we only review working code. You would need to ask this on Stack Overflow. That said, code like
3.0 <= avgGPA < 3.5
doesn't work the way you think. You would need to test those conditions separately (i.e.3.0 <= avgGPA && avgGPA < 3.5
). In this case, though, you already know thatavgGPA
is less than the upper limit since the previousif
clause(s) were false, so you can just use3.0 <= avgGPA
, etc.$endgroup$
– Null
7 hours ago
$begingroup$
Thanks that fixed it.
$endgroup$
– James C Stup Jr.
6 hours ago