Like Tree2Likes
  • 1 Post By Salem
  • 1 Post By Salem

Switch - Case char problem

This is a discussion on Switch - Case char problem within the C Programming forums, part of the General Programming Boards category; Hi everyone. I have a problem with switch - case structure. Actually , this program almost works as i want ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    Question Switch - Case char problem

    Hi everyone. I have a problem with switch - case structure. Actually , this program almost works as i want , but the only problem is that if smo enters a character as a question number , the program closes down . However , i just want to see the "ERROR.. " message when whatever i enter except for [1 . . . 10] . Is there anyone helping me ?


    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main (void)
    {
          
          int qnumber,i;
    
            
                 for (i=0;i<1000;i++) {
                
                printf("Please enter the question number : ");
                scanf ("%d",&qnumber);
                
                switch (qnumber) {
                       
                       case 1:
                            printf ("Question 1\n");
                            continue;
                       case 2:
                            printf ("Question 2\n");
                            continue;
                       case 3:
                            printf ("Question 3\n");
                            continue;
                       case 4:
                            printf ("Questin 4\n");
                            continue;
                       case 5:
                            printf ("Question 5\n");
                            continue;
                       case 6:
                            printf ("Question 6\n");
                            continue;
                       case 7:
                            printf ("Question 7\n");
                            continue;
                       case 8:
                            printf ("Question 8\n");
                            continue;
                       case 9:
                            printf ("Question 9\n");
                            continue;
                       case 10:
                            printf ("Question 10\n");
                            continue;
                       default:
                            printf ("ERROR!! There is no question like this.\n");
                            continue;
                               
                       
     
                       }
                
    }
          
          getch ();
          return 0 ;
    
          }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    You can't type in letters to scanf "%d"

    It would go something like this
    Code:
    int r;
    if ( (r=scanf("%d",&qnumber)) == 1 ) {
        // do your thing
    } else if ( r == 0 ) {
      printf("That's not a number\n");
      int ch;
      // this throws away the garbage line of input
      while ( (ch=getchar()) != EOF && ch != '\n' ) { }
    } else {
      printf("End of file, or file error\n");
    }
    mrcdlgc likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Thanks a lot , but now i have another problem about this code. Now , if i enter "1a" as a question number , the program says both "Question 1" and "That's not a number" . In this situation , it should say "that's not a number". I can't do that. By the way , i can't understand
    Code:
    while ( (ch=getchar()) != EOF && ch != '\n' ) { }
    this part of code. Could you explain it ?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > this part of code. Could you explain it ?
    I did - with this comment.
    // this throws away the garbage line of input

    I suggest reading the manual page for getchar() to begin with.

    > Now , if i enter "1a" as a question number , the program says both "Question 1" and "That's not a number"
    If you want to validate a whole line, the best thing to do is read a line using fgets, then parse it with sscanf.

    Say
    Code:
    char buff[BUFSIZ];   // look it up
    while ( fgets( buff, BUFSIZ, stdin ) != NULL ) {  // look it up
       int a;
       char c;
       if ( sscanf( buff, "%d%c", &a, &c ) == 2 && c == '\n' ) {
          // an integer, a newline - nothing more, nothing less
       } else {
          // error
       }
    }
    mrcdlgc likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Thank you very much
    Last edited by mrcdlgc; 02-11-2013 at 03:17 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch and case problem.
    By jaja009 in forum C Programming
    Replies: 6
    Last Post: 03-29-2010, 09:15 AM
  2. switch/case problem
    By maxilog1009 in forum C Programming
    Replies: 3
    Last Post: 09-07-2009, 09:35 PM
  3. Switch..case with a string/char* as expression
    By C+/- in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2007, 11:02 AM
  4. How do I make a char switch case?
    By Budgiekarl in forum C Programming
    Replies: 8
    Last Post: 06-21-2003, 09:35 PM
  5. Switch/case problem
    By Ivan! in forum C++ Programming
    Replies: 4
    Last Post: 01-31-2003, 11:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21