Click here to Skip to main content
15,885,158 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When i was making a program of 20 students to choose their favourite subject in c
i wrote it like this

So i don't know what is the mistake i have done to get two statement of (enter choice )
why i got two scanf every time

What I have tried:

C++
#include <stdio.h>

void main()
{
    char x;
    int Acount=0,Bcount=0,Ccount=0,Dcount=0,counter=1;
    printf("A. Mathematics\nB. Economics\nC. Programming\nD. Physics\n");
    while(counter<20)
    {
        printf("enter choice:\n");
        scanf("%C",&x);
        if(x=='A')
        {
            Acount++;
        }

        if(x=='B')
        {
            Bcount++;
        }

        if(x=='C')
        {
            Ccount++;
        }

        if(x=='D')
        {
            Dcount++;
        }

        counter++;
}

    printf("number of students who chose Mathematics=%d\n",Acount);
    printf("number of students who chose Economics=%d\n",Bcount);
    printf("number of students who chose Programming=%d\n",Ccount);
    printf("number of students who chose Physics=%d\n",Dcount);
}
Posted
Updated 1-Apr-20 9:43am
v2

scanf("%C", &x) reads only one character from the input stream, but when the user enters his choice, he probably presses 'A', then presses [Enter], so the input stream is actually "A\n", where '\n' is the new-line character. You should probably do something like
C++
char x;
scanf("%c%*s", &x);  /* %*s throws away the rest of the input string

The "%C" format specifier for scanf expects a type wchar, so you should either change the definition of x to wchar, or change the format to "%c" (note uppercase vs lowercase).
What happens if the user enters 'X' or 'a'? Maybe you should do something about that?
 
Share this answer
 
v2
Comments
abdalla_30 1-Apr-20 13:37pm    
OK I got it
but when i run the program i got like this
enter choice:
A
B
enter choice:
C
enter choice:


and when i finish i get
number of students who chose Mathematics=0
number of students who chose Economics= 0
and the rest of statements =0
do you know what is the error ?
abdalla_30 1-Apr-20 13:37pm    
OK I got it
but when i run the program i got like this
enter choice:
A
B
enter choice:
C
enter choice:


and when i finish i get
number of students who chose Mathematics=0
number of students who chose Economics= 0
and the rest of statements =0
do you know what is the error ?
k5054 1-Apr-20 14:59pm    
Looks like scanf("%c%*s") doesn't quite do what I expected. Maybe try using getchar() instead. If you do that, don't forget that you need to still read the trailing '\n' on the line, and any other chars that the user may have entered. A while loop should do the trick.
Quote:
So i don't know what is the mistake i have done to get two statement of (enter choice )
why i got two scanf every time

You already got an explanation in S1 thanks to knowledge of helper.
There is another approach: the debugger to investigate yourself what is going on.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900