Hay guys iam new to c programming and i was trying to do simple do you want to continue loop using do while loop
the problem is the program doesn't read the char Y/N and just simply contiune the loop and here is the code
what's wrong

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

int main()
{
    int x;
    char answer;
    do{

        printf("Please enter a number : ");
        scanf_s ("%d", &x);
        if ((x % 2) != 0) printf("\nThe number is odd\n");
        else printf("The number is even\n");
        printf("Do you want to continue (Y / N )? ");
        scanf_s ("%c", &answer);

    } while (answer != 'N');
    _getch();

}
#include <stdio.h>
#include <conio.h>
int main()
{
int x;
char answer;
do{
printf("Please enter a number : ");
scanf_s ("%d%d", &x);
if ((x % 1000) != 0) printf("\nThe number is odd\n");
else printf("The number is even\n");
printf("Do you want to continue (Y / N )? ");
scanf_s ("%c", &answer);
} while (answer != 'N');
_getch();
}
  1. Why are you using conio.h? It's an old unsupported library that you should have stopped using two decades ago. It's use is incorrect.

  2. scanf_s is only usable with Microsoft compilers. You should probably use scanf instead. Or even safer, use fgets to get a string, and examine it with sscanf.

  3. scanf on line 13 is reading in the newline left on the buffer. A simple solution would be to use getchar() to read it afterwords. Something better would be to read all characters up to the newline left on the buffer. Even better would be fgets.

  4. Again, _getch() is Microsoft specific. You should probably use getchar() instead.

aizam56 samething ..
iam using conio.h because iam using _getch function
and iam using scanf_s because yes iam using microsoft visual studio .
i have no choice
could you please write the code .. knowing that im working in visual studio ?

i have no choice

Actually, you DO have a choice. Visual Studio does not force you to use scanf_s. It will let you use the C standard scanf. So use scanf instead, it will work fine on Visual Studio.

iam using conio.h because iam using _getch function

Use getchar(); instead of _getch();. You'll get the same effect. Then you won't need to use a 20 year old unsupported and broken conio.h. I'm not sure where you even learned to use conio, but where ever it is, it's very wrong. Use an updated book or tutorial instead. Please. You'll save a lot of people, including yourself, trouble.

As I've said, your scanf on line 13 is not picking up the newline. You can use getchar(); to read it off the buffer, or you can read strings more safetly with fgets(), and parse it yourself, or with sscanf.

hay thanks for replying .. i used getchar() instead of conio okay
but i said i have no choice because when i type scanf it's undifined for visual studio it gives me error and suggest scanf_s that's how i knew scanf_S
for nw i interchanged getchar(); with _getch();
and still give me the same output .

Well, you got rid of conio.h. Good start!

Ah. You can add #define _CRT_SECURE_NO_DEPRECATE to the top of your file to get rid of that error. Apperently in the newer versions they rudely enforce it over the standard (even if scanf is dangerous), so I was wrong about it. Again, the best solution is fgets. Or you can stick with microsofts scanf_s (just keep in mind that if your using other compilers, you need to use scanf).

Again, the problem your having is the newline is left on the buffer. I've mentioned that in my last two posts, so this time I'll give you an example:

#include <stdio.h>
#include <string.h>

int
main(void)
{
    /* The name of the user. */
    char name[32];

    /* The age of the user. */
    int age;

    /* This will hold the string that the user enters for his age. */
    char age_buffer[32];

    printf("Name: ");

    /* fgets will saftly read up to sizeof(name) characters into name.
       It returns NULL if there is an error reading the name (for example,
       if stdin is somehow closed while the program is running).
       Note that this will also read in the newline character, unlike
       scanf. This is what was causing your problem. */
    if (fgets(name, sizeof(name), stdin) == NULL) {
        printf("ERROR: Cannot read name.");
        return 1;
    }

    /* Since we've read in the newline, we need to remove it so it
       prints nicely. */
    if (name[strlen(name) - 1] == '\n') {
        name[strlen(name) - 1] = '\0';
    }

    printf("Age: ");

    /* Same thing is going on here. Remember that it reads the age as a
       string however. We need to convert it into an integer. */
    if (fgets(age_buffer, sizeof(age_buffer), stdin) == NULL) {
        printf("ERROR: Cannot read age.");
        return 1;
    }

    /* sscanf is an easy way to do that. It returns the number of arguments
       succesfully read. */
    if (sscanf(age_buffer, "%d", &age) != 1) {
        printf("ERROR: Age entered is not a number.");
        return 1;
    }

    printf("Hello %s, aged %d.\n", name, age);

    /* It's nice to remind the user that he needs to press something
       to end the program. */
    printf("Press any key to continue...");
    getchar();

    return 0;
}

Edited 2 Years Ago by Hiroshe

thanks that was very helpful .. i will use it .
by the way i used _CRT_SECURE_NO_WARNINGS to use scanf
and the problem of my code was .. that i had to use this code fflush(stdin) before scanf charachter . thanks alot again for your time .

I wouldn't recomend using fflush(stdin) like that! The problem with that is it clears the entire buffer, and your prgoram could end up ignoring information.

The idea of stdin is that it can be piped to your keyboard, or anything else, like a file, or the output of another program. This is a very powerfull tool. By clearing the entire buffer, your ignoring the rest of the input that might already be in the buffer.

For example, say your teacher was going to pipe a test input file into your program. You flush the buffer, and poof, all of your teachers input is ignored, and an automarking system would mark it as incorrect. Another example might be when another program is trying to use your program as a backend. It wont work, because a lot of the input on the buffer is being ignored. By using fflush like that, your breaking this capability.

Edited 2 Years Ago by Hiroshe

This question has already been answered. Start a new discussion instead.