-2

so I was trying to program my first project on my own and already have some troubles concerning the getchar()-function.

What I want to do is to get user input without having to press enter. The user-input than should "activate" the case equivalent to the input (e.g. input = case = 1). Instead of that I still have to press enter but then only the default-command gets called.

I saw, that there are some questions about it but non of them really helped me out. here is my code:

#include <stdio.h>
#include "open_txt.h"

int main (){

    printf("\n\n\tAdressbook\n\n");

    printf("Main menu: \n\n");

    printf("1. Add contact\n");
    printf("2. Edit contact\n");
    printf("3. Search contact\n");

    int ui;
    ui = getchar();

    switch(ui){

        case 1:
            printf("\n\nADD CONTACT\n\n");
            break;
        case 2:
            printf("\n\nEDIT CONTACT\n\n");
            break;
        case 3:
            printf("\n\nSEARCH CONTACT\n\n");
            break;
        default:
            printf("That's not an option!\n");
    }
    return 0;
} 
  • 1
    Use a debugger to see the value of ui. You will find that "1" is 49, "2" is 50, etc. – Dabbler Dec 12 '20 at 15:33
  • Does this answer your question? [Capture characters from standard input without waiting for enter to be pressed](https://stackoverflow.com/questions/421860/capture-characters-from-standard-input-without-waiting-for-enter-to-be-pressed) – Andreas Wenzel Dec 12 '20 at 15:36
  • Declare `char ui;` and use cases `'1'`, `'2'` and so on – anotherOne Dec 12 '20 at 15:37
  • I think you will find here some helpful answers : [https://stackoverflow.com/questions/38211864/dynamically-read-user-input-strings-in-c/38212264](https://stackoverflow.com/questions/38211864/dynamically-read-user-input-strings-in-c/38212264) – Badreddine BOUSBA Dec 12 '20 at 15:42

3 Answers3

2

The getchar function returns the key pressed as a character code. So if someone presses 1 then what gets returned is the character '1', not the value 1.

Use character constants instead of numeric constants in your switch:

    case '1':
        printf("\n\nADD CONTACT\n\n");
        break;
    case '2':
        printf("\n\nEDIT CONTACT\n\n");
        break;
    case '3':
        printf("\n\nSEARCH CONTACT\n\n");
        break;
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Oh wow, thanks! But still I have to press enter, is there any way to solve it as well? Or is getchar() the wrong path to take here? –  Dec 12 '20 at 15:37
  • then you have to use `getch` instead of `getchar`, as @Krishna said – lfalkau Dec 12 '20 at 15:38
  • It is still not working. Either way it waits until i press enter... –  Dec 12 '20 at 16:13
2

What I want to do is to get user input without having to press enter.

Then you should use getch() function. getch() reads a single character from the keyboard. But it does not use any buffer, so the entered character is immediately returned without waiting for the enter key.

Krishna Kanth Yenumula
  • 2,533
  • 2
  • 14
  • 26
  • It seems like C is making fun of me lol. So i clearly do something wrong, since it is still not working.. –  Dec 12 '20 at 15:55
  • Note that the function `getch` is not part of ISO C. It is a platforms-specific extension. However, it seems that both Microsoft Windows and Linux offer support for this function, but it will probably be unavailable on some other platforms. – Andreas Wenzel Dec 12 '20 at 15:56
0

Here ,switch statement return a caracter not a value ,so you should write like this : '1' '2' '3'

#include <stdio.h> 
#include "open_txt.h"

int main (){

printf("\n\n\tAdressbook\n\n");

printf("Main menu: \n\n");

printf("1. Add contact\n");
printf("2. Edit contact\n");
printf("3. Search contact\n");

int ui;
ui = getchar();

switch(ui)
{
  case '1':
    printf("\n\nADD CONTACT\n\n");
    break;
  case '2':
    printf("\n\nEDIT CONTACT\n\n");
    break;
  case '3':
    printf("\n\nSEARCH CONTACT\n\n");
    break;
  default:
    printf("That's not an option!\n");

   }
}
MED LDN
  • 684
  • 1
  • 5
  • 10