ic205
ic205

Reputation: 131

Why does the second getchar() call always trigger my switch's default case?

I'm trying to use nested switch statements to write a menu for the user do choose some option. The first switch works fine and the nested one doesn't. In the nested switch I always end up in the default option and the user can't choose.

It seems that variable d stays NULL and that's what make the switch to end up in the default option. What is preventing from the user to be able to type a char, and from the code to assign value to d?

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

/* function declaration */
int connection_func();
int main() {  
    /*make connection to sql server*/
    int connection_return = connection_func();
    printf("%d",connection_return);

       char c,d;
       printf("\n Choose one of the following options: \n");
    printf("1- DB maintenance \n");
    printf("2- Weekly schedule creation \n");
    c= getchar();
    switch(c) {
            case '1':
                    // DB maintenance
                    printf("\n DB maintenance options:: \n");
                    printf("1- ADD data to existing table \n");
                    printf("2- DELETE data from existing table \n");
                    printf("3- DISPLAY all data inside a table \n");
                    printf("4- DROP table (root only) \n");
                    d = getchar();
                    switch(d) {
                            case 'A':
                            //  User want to ADD data to the database
                            break;
                            case 'B':
                            // User want to DELETE data from database
                            break;
                            case 'C':
                            // User want to Display the tbl data
                            break;
                            case 'D':
                            // User want to DROP tables
                            break;
                            default:
                            printf("That is not a proper selection \n");
                            break;
   }
                    break;

            case '2':
                    // Weekly schedule creation
                    break;
            default:
                    printf("That is not a proper selection. \n");
                    break;
    }

return(0);
}

int connection_func() {
    MYSQL           *conn;
    MYSQL_RES       *res;
    MYSQL_ROW       row;
    char *server  = "localhost";
    char *user     = "root";
    char *password = "mypassword";
    char *database = "sc" ;

    conn = mysql_init(NULL);
    /* Connect to database */
    if ( !mysql_real_connect(conn, server, user, password, database, 0, NULL , 0)) {
            fprintf(stderr, "%s\n", mysql_error(conn));
            return(1);
    }
return(0);
}

No errors in compiling. The output from the terminal:

root@kali:# ./sc
0
 Choose one of the following options: 
1- DB maintenance
2- Weekly schedule creation 
1
(null) -d       (**** - the value of d variable ***) 
 DB maintenance options:: 
1- ADD data to existing table 
2- DELETE data from existing table 
3- DISPLAY all data inside a table 
4- DROP table (root only) 
That is not a proper selection 

Upvotes: 0

Views: 484

Answers (3)

NadavL
NadavL

Reputation: 400

After you insert '1' and clicked enter, the stdin stream has "1\n" in it, then getchar() is being called once, "1" has been removed from the stdin.

Then, the next call to getchar() doesn't prompt the user, since there is already data in the stdin stream - the newline character '\n'.

Try using

while((d = getchar()) != '\n' && d != EOF);

to spin out the useless data until the line break, usually it'll be just the '\n' char, so you can even use execute just one more call to getchar() to remove the newline from the stream.

Also, as mentioned in other comments, I'd consider fgets

Upvotes: 0

Jens
Jens

Reputation: 72707

When you enter "1", you actually feed two characters to your program, a "1" followed by, well, enter (which appears as a newline on stdin). The newline is then returned by the second call to d = getchar(). (You could enter both choices to see your program work: e.g. 12 followed by ENTER).

If you want to skip white space such as newlines, tabs and spaces, I suggest using scanf(" %c", &d). The initial space tells scanf to skip any amount of white space before converting a single character. Be sure to check that scanf returns 1, indicating it successfully converted the item to scan.

Upvotes: 0

user58697
user58697

Reputation: 7923

Notice that you didn't have a chance to even say A or B.

After you press 1 you press Enter. 1 gets into c, and d = getchar() results in d having a code for a newline.

Upvotes: 1

Related Questions