Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a program to enter the numbers till the user wants and at the end it should display the count of positive, negative and zeros entered.
In my program the compiler is not asking for y/n and just telling the number of positive, negative and zeroes. Is it that scanf is not being executed or there is some predefined value of choice?

What I have tried:

C++
#include <stdio.h>

int main()
{
    int number, positive = 0, negative = 0, zero = 0;
    char choice;

    do
    {
        printf("Enter a number :");
        scanf("%d", &number);

        if (number > 0)
        {
            positive++;
        }
        else if (number < 0)
        {
            negative++;
        }
        else
        {
            zero++;
        }

        printf("Do you want to Continue(y/n)? ");
        scanf("%c", &choice);

    }while (choice == 'y');


    printf("\nPositive Numbers :%d\nNegative Numbers :%d\nZero Numbers :%d",
        positive, negative, zero);


    return 0;
}
Posted
Updated 17-Oct-18 10:06am
v2

Add the following line before you ask if they want to continue thus:
C++
// clear the input buffer
fseek(stdin, 0, SEEK_END);

printf("Do you want to Continue(y/n)? ");
scanf("%c", &choice);
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 17-Oct-18 9:56am    
Just to make it more clear...
The new-line char from the previous input (the enter key you hit) is still in the buffer after the number, and waiting to be read...

https://gsamaras.wordpress.com/code/caution-when-reading-char-with-scanf-c/
Richard MacCutchan 17-Oct-18 9:57am    
Thanks. Now what is all this about a goose crossing a lake?
Kornfeld Eliyahu Peter 17-Oct-18 9:59am    
I can explain you that only if you speak VB.NET fluently...
Richard MacCutchan 17-Oct-18 10:03am    
:laugh:
Çok teşekkur ederim.
CPallini 17-Oct-18 10:55am    
5.
Quote:
In my program the compiler is not asking for y/n

The compiler don't ask user input because it is not its job.
The compiler compile your source code, it means that it check that source code is correct C code syntax. if correct, it generate an executable, the executable is the one that do what is in your source code. This does not mean the the executable will do what you expect.
"The cat fly high in the sky." is correct English grammar, but means nothing because cats don't fly.
The good news is that the debugger allow you to watch what is going on in the executable, it is a great learning tool.

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.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

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