Thread: convert user input for use in switch

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    41

    convert user input for use in switch

    Hey guys,

    I'm just testing some code via user input. I'm trying to do a simple user input to select from 3 options, then enter a name to be entered into a linked list. I was using scanf but have since discovered obvious buffer issues and issues when mixing scanf with fgets and with using it multiple times in quick succession.

    No matter what I enter, input convert will always be '10'

    I have tried to so many approaches, including casting user_input_c and assigning it to input_convert, also using strtol to convert into user_convert.

    All I want is a nice clean way to enter a selection input and then a name.

    Code:
         int input_convert;
         char user_input_c [3];
         char contact_name[24];
        
        do {
            
            printf("\nWelcome to your address book\n\n");
            printf("1. Add a node\n");
            printf("2. Display all nodes\n");
            printf("3. Exit\n\n");
            printf("2. Enter your choice:- ");
    
    
            fgets(user_input_c,sizeof(user_input_c),stdin);
            
            input_convert = user_input_c[1] - '0';
            printf("user_input_c = %s", user_input_c);
            printf("input_convert = %d", input_convert);
            
            switch(input_convert){
                case(10):
                    printf("\n\nYou have chosen to add a new node. \n\nPlease enter a name...\n");
                    fgets(contact_name,sizeof(contact_name),stdin);
                    printf("You have entered %s\n\n", contact_name);
                    contact_create(contact_name, 0);
                    break;
                case(20):
                    list_disp();
                    break;
                case(30):
                    exit(1);
                    break;
                default:
                    printf("\n\n\t Wrong entry, try again"); 
            }
        } while (input_convert != 3);

  2. #2
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    int user_input_c = fgetc(stdin);
    switch(user_input_c){
    case '1':
    case '2':

  3. #3
    Registered User
    Join Date
    Sep 2016
    Posts
    41
    Legend. I also realised that I'm an idiot and was indexing from 1 instead of 0... doiii

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    or even if you like:

    int ch = getchar();
    switch (ch - '0')
    case 1:
    case 2:
    case 3:
    ...

    Not sure where you got that the conversion would be 10, or 20.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-11-2017, 06:34 AM
  2. Switch case User interface menu helpp
    By cnandha19 in forum C Programming
    Replies: 0
    Last Post: 05-25-2017, 06:35 AM
  3. switch input pic18f4520
    By Alex Margetts in forum C Programming
    Replies: 8
    Last Post: 03-09-2014, 10:26 PM
  4. how to convert char string to user defined data type
    By prdeepss in forum C Programming
    Replies: 5
    Last Post: 08-02-2009, 09:59 AM
  5. Firefox user switch
    By cerin in forum Tech Board
    Replies: 11
    Last Post: 03-13-2005, 12:06 AM

Tags for this Thread