-2
#include <stdio.h>

int main() {
    
    char v1,v2,v3;
    printf("enter: ");
    scanf("%c",&v1);
    printf("enter: ");
    scanf("%c",&v2);
    printf("enter: ");
    scanf("%c",&v3);
    
}

this is my sample code and I expect an output like:

enter: a
enter: b
enter: c

but I'm getting output like:

enter: a
enter: enter:

2nd and 3rd print statements are getting executed simultaneously.

Persixty
  • 8,165
  • 2
  • 13
  • 35
  • 2
    Are you using one `printf` in each language? The C# language may have different behavior than C or C++. – Thomas Matthews Aug 21 '22 at 18:16
  • *"2nd and 3rd print statements are getting executed simultaneously."* -- I'd disagree with this assessment. If they were executed simultaneously, the second line of your output would look more like `enetnteerr: : ` (the two prompts intermixed). What you see is more likely the statements being executed sequentially, but without a pause in the middle. (No waiting on `scanf`.) – JaMiT Aug 21 '22 at 18:17
  • 1
    Are you mixing the three languages? I highly recommend not mixing the three languages. Mixing two of those three takes the program complexity up a couple of notches and makes debugging more difficult. I recommend picking one of those three languages to write the program in. – Thomas Matthews Aug 21 '22 at 18:17
  • 3
    Do not tag both C and C++ except when asking about differences or interactions between the two languages. Pick one language and delete the tag for the other one. – Eric Postpischil Aug 21 '22 at 18:18
  • A key piece of debugging information that should go with this question: what are the values of `v1`, `v2`, and `v3` at the end of your function? (I'd also recommend initializing them to some value to make it easier to see if their value had changed.) – JaMiT Aug 21 '22 at 18:19
  • The output you get seems to indicate IO error or EOF or something else strange. You should add code to check return value of `scanf`! – hyde Aug 21 '22 at 19:11
  • `scanf(" %c",&v1);` (note the addition of the `' '` (space) to read/discard any number of whitespace characters before reading the next character. (hint: `'\n'` that is left in `stdin` unread by the user pressing **Enter** -- is whtiespace) (do the same for your next two calls to `scanf()` as well...) – David C. Rankin Aug 22 '22 at 05:28

3 Answers3

1

The problem is that you're reading characters, so if what you enter is actually aEnterbEntercEnter, that is actually SIX characters, and what you read will be the first 3 (the a, the Enter, and the b)

What you can do is use a space in the scanf format to skip whitespace. If you use scanf(" %c", &v1); then any whitespace (such as Enter) will be skipped, which will cause your result to be what you expect. However, if someone enters something like spaceEnter, the program will seem to hang, waiting for non-whitespace to be entered

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
0

The problem lies in buffering. Read this question, which was already mentioned in the comments.

TL,DR: Use scanf(" %c", &v); to read chars, since it ignores all whitespace from the buffer, as well as the trailing newline.

gXLg
  • 184
  • 13
-1

Hello what I would recommend is using a library that allows you to use a function called get_char. I wrote a program using this function that does what you wanted yours to do.

#include <stdio.h>
#include <string.h>
#include <cs50.h>
    
int main(void)
{
  char v1 = get_char("enter: ");
  char v2 = get_char("enter: ");
  char v3 = get_char("enter: ");
}

So the program stores 3 char values as they are entered one after another, without all executing at once. I don't know if this is the type of answer that you are looking for or if it will help you, but I figured I would put this out there. If its not really what your looking for let me know!

This is what the terminal looks like btw.:

$ make help
$ ./help
enter: a
enter: b
enter: c
gXLg
  • 184
  • 13