0

Hi I am new to programming and I got a trouble when I try to make a little change to the example in the book.

/* Chapter 3 Example, C Prime Plus */
#include <stdio.h>
int main(void)
{
   char Letter, ch;
   int intValue;

   printf("Please enter a letter: \n");
   scanf("%c", &Letter);   /* user inputs character */
   printf("The code for %c is %d.\n", Letter, Letter);

   printf("Now is another we to implement the process: \n");
   printf("RN, the value of ch is %c, and the value of intValue is %d\n", ch, intValue);
   printf("Please enter a letter: \n");
   scanf("%c", &ch);
   intValue = ch;
   printf("The code for %c is %d.\n", ch, intValue);

   return 0;
}

When I run it, the outcome would be

Please enter a letter:
M
The code for M is 77.
Now is another we to implement the process:
RN, the value of ch is , and the value of intValue is 0
Please enter a letter:
The code for
is 10.

and the part
" Now is another we to implement the process:
RN, the value of ch is , and the value of intValue is 0
Please enter a letter:
The code for
is 10. " will all come out without asking me to enter a value.

I want to know why and are there any other way to implement it that is different from examples in the book?

Thank you for your time!

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Matt_C
  • 3
  • 1
  • 1
    You use the variable `ch` and `intValue` before they are initialized. Local non-static variables (like `ch` and `intValue`) will be uninitialized and have *indeterminate* values (seemingly random or garbage). – Some programmer dude Aug 21 '20 at 04:15
  • As for your problem, I know there are duplicates (but I never find them) so in summary the problem is that when you press the `Enter` key for the first input, that is added as a newline `'\n'` in the input buffer to be read by the very next `scanf`. – Some programmer dude Aug 21 '20 at 04:17
  • The basic solution is to place an space before `%c` in the `scanf` specifier string, so `" %c"` instead of '"%c"', so that all empty characters (newline, space, tabulation, etc) will be discarded instead of being matched by the `%c` specifier. – isrnick Aug 21 '20 at 04:30
  • 1
    Does this answer your question? [How to read / parse input in C? The FAQ](https://stackoverflow.com/questions/35178520/how-to-read-parse-input-in-c-the-faq) – Yunnosch Aug 21 '20 at 05:20
  • See the "When scanf() does not work as expected" part of the FAQ. – Yunnosch Aug 21 '20 at 05:21
  • Read [*Modern C*](https://modernc.gforge.inria.fr/), this [C reference](https://en.cppreference.com/w/c), the documentation of your C compiler (e.g. [GCC](http://gcc.gnu.org/)...) and the documentation of every function that you use (e.g. [scanf(3)](https://man7.org/linux/man-pages/man3/scanf.3.html), whose failure you should handle). If you use `gcc`, pass `-Wall -Wextra -g` to it then use the [GDB](https://www.gnu.org/software/gdb/) debugger – Basile Starynkevitch Aug 21 '20 at 07:20

1 Answers1

0

Hi Matt_C and welcome to SO.
First, you don't need the second bloc of printfs and the scanf, it just trying to do the same thing and there is an order error.

Second, it is tricky when you try consecutive scanf, it holds the last key pressed (enter is the last key pressed = \n). This is why it skips the second scanf.

There a little solution for that, add a space at the beginning of the scanfs. Try this:

int main() {

    char exit, letter;

    while (1) {
        printf("Please enter a letter: ");
        scanf(" %c", &letter);
        printf("\nThe code for '%c' is %d. \n\n", letter, letter);

        printf("Exit ? (y/n): ");
        scanf(" %c", &exit);

        if(exit == 'y')
        {
            break;
        }

        system("clear");    // UNIX
        //system("cls");    // DOS
    }
}

Don't forget to choose one answer that you believe is the best solution to your problem.

Sinan
  • 898
  • 1
  • 9
  • 23