1
#include <stdio.h>
#include <stdlib.h>
void main()
{
    char choice = 'y';
    while(choice != 'n')
    {
        printf("Enter choice(y/n):- \n");
        scanf("%c", choice);
        if(choice == 'y')
        {
            printf("Choice = y");
        }
        if (choice == 'n')
        {
            printf("Choice = n");
            exit(1);
        }
    }
}

Actual output:

Enter choice(y/n):- 
y
(Program execution terminated)

Expected output:

Enter choice(y/n):-
y
Enter choice(y/n):-
y
Enter choice(y/n):-
n
(Program execution terminated)

I want to ask choice to user until he/she doesn't press 'n'. That's my question.

Barmar
  • 741,623
  • 53
  • 500
  • 612
CKJ_1630
  • 33
  • 4
  • 5
    First about your problem, doesn't your compiler issue a warning for that `scanf` call? What does your text books, tutorials or teacher say about `scanf` and how to use it? – Some programmer dude Jul 20 '23 at 13:21
  • 5
    `scanf("%c", &choice);` – OldProgrammer Jul 20 '23 at 13:23
  • 2
    Secondly, about the next problem you will get, please print out the numerical value of `choice` directly after the `scanf` call (once you fix the first problem). If you look up the values you read in an ASCII reference, what do they correspond to? Why do you think you get those values? What input could lead to the values you see? Again, a decent book, tutorial or class should have taught you a workaround for the problem you will get. – Some programmer dude Jul 20 '23 at 13:23
  • 3
    Get into the habit of using `else if` when making a series of mutually exclusive comparisons. – Barmar Jul 20 '23 at 13:23
  • And to avoid problems with newline characters you might want to to add a space before the `%c` format specifier `scanf(" %c", &choice)`. Shouldn't the expected output contain the result of the conditional `printf`s like `Choice = y`? If you don't get this in the actual output, your program is probably terminated by a segmentation fault at the wrong `scanf`. – Bodo Jul 20 '23 at 13:47
  • 1
    If your compiler did not warn you about the line `scanf("%c", choice);`, then I strongly suggest that you enable more compiler warnings. I suggest that you read this for further information: [Why should I always enable compiler warnings?](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings) – Andreas Wenzel Jul 20 '23 at 13:57
  • 2
    If `choice` actually gets equal to `n` you `exit` anyway, thus you don't need the additional check at the loop, thus I'd simply use a (seeming) endless loop: `for(;;)` – which allows you to limit scope of `choice` to the loop body then, too. – Aconcagua Jul 20 '23 at 13:58
  • Side note: If you change the loop from a `while` loop to a `do`...`while` loop, then you will not need to initialize `choice` to `'y'`. – Andreas Wenzel Jul 20 '23 at 14:22
  • Side note: How do you want the loop to terminate? As your loop is written, it will never terminate due to the condition `while(choice != 'n')` evaluating to false. Instead, it will always terminate due to `exit(1);` being called. If that is what you want, it would make more sense to write `while(1)` or `for(;;)` instead of `while(choice != 'n')`. (This issue has already been mentioned by someone else, I just formulated it in other words). – Andreas Wenzel Jul 20 '23 at 14:30
  • Once you fix the first problem regarding `scanf("%c", choice);` instead of `scanf("%c", &choice);`, the second problem is a duplicate of: [scanf() leaves the newline character in the buffer](https://stackoverflow.com/q/5240789/12149471) – Andreas Wenzel Jul 20 '23 at 15:15

2 Answers2

0

The issue in your code is with the scanf . When reading a character using scanf, you need to use the format specifier %c and provide the address of the variable where the character will be stored using the & operator :

#include <stdio.h>
#include <stdlib.h>

int main() // Changed from 'void' to 'int' as it is a good practice to return an integer from main
{
    char choice = 'y';
    while (choice != 'n')
    {
        printf("Enter choice(y/n):- \n");
        scanf(" %c", &choice); // Note the space before %c to consume any leading whitespace

        if (choice == 'y')
        {
            printf("Choice = y\n");
        }
        else if (choice == 'n') // Use 'else if' to check for 'n' after checking for 'y'
        {
            printf("Choice = n\n");
            exit(0); // Use exit(0) to indicate successful termination
        }
    }
    return 0; // Return 0 from main to indicate successful execution
}
zoldxk
  • 2,632
  • 1
  • 7
  • 29
0
#include <stdio.h>
#include <stdlib.h>
void main()
{
 char choice;
 do
  {
    printf("Enter choice(y/n):- \n");
    scanf(" %c", &choice);
    if(choice == 'y')
    {
        printf("Choice = y\n");
    }
    else if (choice == 'n')
    {
        printf("Choice = n");
        exit(0);
    }
  }while(choice != 'n');
}
  • Instead of providing a code-only answer, your answer would be of higher quality if you added some text to your answer, in which you for example explain what was wrong in OP's code and how your code solves the problem. – Andreas Wenzel Jul 20 '23 at 14:49
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 24 '23 at 15:05