I am creating a program which includes converting numerical grades to letter grades but I'm stuck where I have to put a counter that counts number of letters. For example if 90-100=A and 90, 96, 87, 96 are entered, to count and display something like Number of As= 4
or something similar. I am doing this for other letters such as 80s=B 70s=C etc. I have no idea how to use the counter.

I need a simple counter code to use. All I remember was something about countAs = 0;
and maybe something about 1+ somewhere in there. I am completely lost on the counters.

``````#include <iostream>

using namespace std;

main ()
{

int countAs = 0;
int countBs = 0;
int countCs = 0;
int countDs = 0;

cout << "enter student grade: ";

else if (studentgrade1 >= 80) cout<<"B";
else cout <<"failed";
//If the person enters, 95, that is an A. Then program should notice and count there's 1 a so far

cout << "enter student grade: ";

//Enters a new grade and if person enters 92, that is an A and then program should count that there's another A
else if (studentgrade1 >= 80) cout<<"B";
else cout<<"failed";

//program should then display results of how many As and Bs Etc. there were.

return 0;
}``````

I wrote this code as an example. I know I have to fill out the others for 80s, 70s and write more code and but just focusing on how I could go about it in terms of displaying how many As, Bs, Cs etc were converted? Thanks!

3
Contributors
11
Replies
14
Views
6 Years
Discussion Span
Last Post by jonsca

Try changing the

``if (studentgrade1 >= 90) cout<<"A";``

to

``````if (studentgrade1 >= 90)
{ cout<<"A";
countAs++;}``````

and just repeat for the rest.

Hey! Thanks for your reply. I tried doing as you suggest but I get an undeclared error. Anything I am doing wrong? At the end, how would it display the overall results? Thanks so much!

I am sorry about that, I will post my code again now. I'm not sure if that will display the total number of As, Bs, Cs, etc at the end.

``````#include <iostream>

using namespace std;

main ()
{

int countAs = 0;
int countBs = 0;
int countCs = 0;
int countDs = 0;

cout << "enter student grade: ";

else if (studentgrade1 >= 80) cout<<"B";
else cout <<"failed";
//If the person enters, 95, that is an A. Then program should notice and count there's 1 a so far

cout << "enter student grade: ";

//Enters a new grade and if person enters 92, that is an A and then program should count that there's another A
else if (studentgrade1 >= 80) cout<<"B";
else cout<<"failed";

//program should then display results of how many As and Bs Etc. there were.

countAs++
countBs++
countCs++
countDs++

return 0;
}``````

main() always returns an int.

Here's what your code is doing:
All the counts are zero.
You ask the user for one input.
You ask the user for another input.
You increment the counts by 1.

Is that what you want it to do?

Lance gave you a huge hint, you want to increment the counts when you've classified the grade as A,B,C,D, so you are on the right track.

Also, you want to enclose the whole business in a loop so you can get grades until the user enters -999 or something. Look into a do/while loop.

Edited by jonsca: n/a

Yes that is exactly what I want to do in terms of incrementing the counter up by 1. What I am trying to do is, (sorry if was not clear, trying to write well) after grades are entered and letter grade is shown, before the program quits, shows the person statistic results like

# of As: 7
# of Bs: 4
# of Cs: 9
# of Ds: 3

Really sorry for the confusion. And thanks a lot for the help!

I will check into the do/while loop and see if I can use it thanks again.

Edited by ProjectAce7: n/a

No, I understand exactly what you are trying to do. Your writing was fine. The issue is you need to plan out what your program is to accomplish instead of coding lines.

My point is, you're incrementing the counters once at the end of the program and the increment is not related to the grades you are inputting at all. You're also only taking in 2 grades, and presumably you want a flexible number depending only on what the user inputs.

Start out with small pieces. I've said you need a do/while loop. If you don't know what it is, read your text to see the syntax. You need something like what you have to identify the grades, but you also want to increment the count of A's at the same time as you are identifying the grade as an A.

Our posts crossed. That's a good idea. You'll need some sort of loop structure.

Thanks for your patient help again. I tried looking up do/while examples online, even some from here and I understand concept but when I try the coding itself, I get error (Each undeclared identifier is reported only once for each function it appears in.) I am completely horrible in these counters. This is what I did based on example.

``````#include <iostream>

using namespace std;

main ()
{

cout << "enter student grade: ";

else if (studentgrade1 >= 80) cout<<"B";
else cout <<"failed";
//If the person enters, 95, that is an A. Then program should notice and count there's 1 a so far

cout << "enter student grade: ";

//Assuming new Student...

else if (studentgrade1 >= 80) cout<<"B";
else if (studentgrade1 >= 70) cout<<"C";
else if (studentgrade1 >= 60) cout<<"D";
else cout<<"failed";

{count = count + 1
}

return 0;
}``````

Edited by ProjectAce7: n/a

Please continue to use the code tags.
The problem the compiler is talking about is here: `while (studentgrade!= -1)` You have studentgrade1 in all the other places.

Your loop has to wrap around your input as well. I'm not sure how you think the while loop at the bottom will count anything. `//Then program should notice and count there's 1 a so far` How is it supposed to do that?

Just set up a do/while loop that takes input until you input -1. Try that in a program without any of the other statements.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.