help C++ problem using while loop

I need to run a program that counts how many people got A, B, C, D, or F by entering the letter (note) each person got using the while loop in c++

here is what i have wrote so far but isnt working =/

#include <stdio.h>
#include <math.h>
#include <conio.h>

main ()
{


int N=0, nA = 0, nB = 0, nC = 0, nD = 0, nF = 0;
char note;
N = 0;
while(N<=25)
{
printf("\nEnter your note: ");
scanf("%C",&note);
if(note=='A' || note=='a')
nA= nA+1;
else if (note=='B' || note=='b')
nB= nB+1;
else if (note=='C' || note=='c')
nC= nC+1;
else if (note=='D' || note=='d')
nD= nD+1;
else if (note=='F' || note=='f')
nF= nF+1;
else
printf ("The note you entered is invalid");
N=N+1;
}
printf("\nTotal amount of A=%d",nA);
printf("\nTotal amount of B=%d",nB);
printf("\nTotal amount of C=%d",nC);
printf("\nTotal amount of D=%d",nD);
printf("\nTotal amount of F=%d",nF);
}
Last edited on
What isn't working?

Btw you need to have int main() { at the top.
first of all why are you scanning the address of note? and i think the C in the scan is supposed to be lower case.
Thxs, the problem is that i run the program, Enter the note for example A and the program loops again and again asking for a new note, even after i enter 25 letters the program keeps looping asking for a letter and keeps printing the "else" part ("The note you entered is invalid")

http://imageshack.us/photo/my-images/545/imagerhs.jpg/


ok ok, thanks a lot!! Aramil the program is almost almost almost! working, one of the problems was that damn C that was in upper case, but i have one last question.

The program itself is working but
-how do i avoid the duplication of the first printf (check picture in imageshack link)

http://imageshack.us/photo/my-images/837/imagektk.jpg/

-how i could tell the user an error saying the letter or number is invalid (in case he writtes any other letter or number) without using 1 cycle of the while loop

Last edited on
so to your first - can you post the code because its not loading imageshack right.
your second -: put the n = n + 1; in the if statements for example:
1
2
3
4
5
if(note == 'A' || note == 'a')
{
     nA = nA + 1;
     n = n + 1;
}


[edit]
1
2
3
4
5
6
7
else if(note == 'B' || note == 'b')
{
     nB = nB + 1;
     n = n + 1;
}

//same for c d and f   
Last edited on
here is the program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <math.h>
#include <conio.h>

int main ()
{


int N=0, nA = 0, nB = 0, nC = 0, nD = 0, nF = 0;
char note;
N = 1;
while(N<=25)
{
printf("\nEnter your note: ");
scanf("%c",&note);
if(note=='A' || note=='a')
nA= nA+1;
else if (note=='B' || note=='b')
nB= nB+1;
else if (note=='C' || note=='c')
nC= nC+1;
else if (note=='D' || note=='d')
nD= nD+1;
else if (note=='F' || note=='f')
nF= nF+1;
else
N=N+1;
}
printf("\nTotal amount of A=%d",nA);
printf("\nTotal amount of B=%d",nB);
printf("\nTotal amount of C=%d",nC);
printf("\nTotal amount of D=%d",nD);
printf("\nTotal amount of F=%d",nF);
}


PD: try clicking in the picture inside the imageshack link or else u will see a small blurry version of the image
Last edited on
so first of all (and this isnt neccesarily wrong ive just never seen it) why are you scanning the adress? and then for the last else put in a thats an invalid grade and in the ifs i already covered that
ok the assigment is to writte a program using while function that counts how many people in a classroom of 25 persons got A, B, C, D or F,

Thats why im scanning the note if thats what u mean by address but like the forum says im a complete noob hahahaha...about the second part lets see if i got it right:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include <math.h>
#include <conio.h>

int main ()
{


int N=0, nA = 0, nB = 0, nC = 0, nD = 0, nF = 0;
char note;
N = 1;
while(N<=25)
{
printf("\nEnter your note: ");
scanf("%c",&note);
if(note=='A' || note=='a')
nA= nA+1;
N=N+1;
else if (note=='B' || note=='b')
nB= nB+1;
else if (note=='C' || note=='c')
nC= nC+1;
else if (note=='D' || note=='d')
nD= nD+1;
else if (note=='F' || note=='f')
nF= nF+1;
else
printf("\nInvalid note")
}
printf("\nTotal amount of A=%d",nA);
printf("\nTotal amount of B=%d",nB);
printf("\nTotal amount of C=%d",nC);
printf("\nTotal amount of D=%d",nD);
printf("\nTotal amount of F=%d",nF);
}


btw thxs in advance for your help
Last edited on
so the first part: the '&' is the address operator. what that means is that when a variable is declared it is stored in the ram. you can see exactly where it is stored in the ram by printing &nameOfTheVariable. It will change each time the program is run and if you dont know why look up ram. so you are scanning the adress of note. so im wondering why you do that and not just scanf("%c", note)? and you want 25 grades entered so what u did for the a do it for all other grades so that way the program wont end until 25 grades are entered regardless of any invalid grades entered

[edit]
so an if with no {} after it will only execute the statement immediately after it. however if you want to execute multiple statements wrap them with {}. look at my above post to see what i mean
Last edited on
ooo ok ok i made the corrections, and the program does end entering 25 grades but it doesnt say which are valid and which arent, if i enter an invalid number he just count them as zeros
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <math.h>
#include <conio.h>

int main ()
{


int N=0, nA = 0, nB = 0, nC = 0, nD = 0, nF = 0;
char note;
N = 1;
while(N<=25)
{
printf("\nEnter your note: ");
scanf("%c",note);
if(note=='A' || note=='a')
nA= nA+1;
N=N+1;
else if (note=='B' || note=='b')
nB= nB+1;
N=N+1;
else if (note=='C' || note=='c')
nC= nC+1;
N=N+1;
else if (note=='D' || note=='d')
nD= nD+1;
N=N+1;
else if (note=='F' || note=='f')
nF= nF+1;
N=N+1;
else
printf("\nInvalid note")
}
printf("\nTotal amount of A=%d",nA);
printf("\nTotal amount of B=%d",nB);
printf("\nTotal amount of C=%d",nC);
printf("\nTotal amount of D=%d",nD);
printf("\nTotal amount of F=%d",nF);
}
have you read my edits?
i just readed all the edits and srry for not noticing them, but the program atm is still doing the same as before

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <math.h>
#include <conio.h>

int main ()
{


int N=0, nA = 0, nB = 0, nC = 0, nD = 0, nF = 0;
char note;
N = 1;
while(N<=25)
{
printf("\nEnter your note: ");
scanf("%c",note);
if(note=='A' || note=='a')
{
nA= nA+1;
N=N+1;
}
else if (note=='B' || note=='b')
{
nB= nB+1;
N=N+1;
}
else if (note=='C' || note=='c')
{
nC= nC+1;
N=N+1;
}
else if (note=='D' || note=='d')
{
nD= nD+1;
N=N+1;
}
else if (note=='F' || note=='f')
{
nF= nF+1;
N=N+1;
}
else
printf("\nInvalid note")
}
printf("\nTotal amount of A=%d",nA);
printf("\nTotal amount of B=%d",nB);
printf("\nTotal amount of C=%d",nC);
printf("\nTotal amount of D=%d",nD);
printf("\nTotal amount of F=%d",nF);
}
its ok u just probably read it before i made the edits. so what is it doing wrong exactly? The code looks fine to me
The progam isnt detecting when i introduce an invalid note, is counting all invalid notes as a 0

and the program is like double printing "Enter your note" every time i enter one note.
Topic archived. No new replies allowed.