Thread: Switch Problem

  1. #1
    Registered User
    Join Date
    Apr 2009
    Location
    Malaysia
    Posts
    2

    Switch Problem

    Code:
    #include <stdio.h>
    
    char option;
    
    void PURCHASE(), VIEW(), QUIT();
    void BUSINESS(), ECONOMY();
    
    void main(){
    	printf("AIRLINE RESERVATION SYSTEM \n");
    	printf("P - to Purchase Ticket \n");
    	printf("V - to View Arrangement \n");
    	printf("Q - to Quit the system \n");
    	printf("Enter the option : ");
    	scanf_s("%c", &option);
    	getchar();
    
    	switch(option){
    		case '1':
    		case 'P':
    		case 'p':
    			printf("Purchase\n");
    			//PURCHASE();
    			break;
    
    		case '2':
    		case 'V':
    		case 'v':
    			printf("View\n");
    			//VIEW();
    			break;
    
    		case '3':
    		case 'Q':
    		case 'q':
    			printf("Quit\n");
    			//QUIT();
    			break;
    
    		default:
    			printf("Invalid Option \n");
    			break;
    	}
    }
    Please help me see what is the problem, no matter that character or number I entered, the program also display Invalid Option.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Most likely because the next scanf reads a newline, and thus ends up in the "invalid option", as you do not have any entry for '\n'. You could verify this by adding a '\n' case in your switch, and print a message "got a newline" or some such.

    To fix that, add a space in the format string, " %c" to read out any "whitespace" characters (newline belongs to this category).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by ahhak1989 View Post
    Code:
    #include <stdio.h>
    
    char option;
    
    void PURCHASE(), VIEW(), QUIT();
    void BUSINESS(), ECONOMY();
    
    void main(){
    	printf("AIRLINE RESERVATION SYSTEM \n");
    	printf("P - to Purchase Ticket \n");
    	printf("V - to View Arrangement \n");
    	printf("Q - to Quit the system \n");
    	printf("Enter the option : ");
    	scanf_s("%c", &option);
    	getchar();
    
    	switch(option){
    		case '1':
    		case 'P':
    		case 'p':
    			printf("Purchase\n");
    			//PURCHASE();
    			break;
    
    		case '2':
    		case 'V':
    		case 'v':
    			printf("View\n");
    			//VIEW();
    			break;
    
    		case '3':
    		case 'Q':
    		case 'q':
    			printf("Quit\n");
    			//QUIT();
    			break;
    
    		default:
    			printf("Invalid Option \n");
    			break;
    	}
    }
    Please help me see what is the problem, no matter that character or number I entered, the program also display Invalid Option.
    1. dont use void main,it returns int.
    2.just change scanf_s(i dont know what it is) to simply scanf.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BEN10 View Post
    1. dont use void main,it returns int.
    2.just change scanf_s(i dont know what it is) to simply scanf.
    scanf_s is the "safe" version of scanf() promoted by MS - I'm not entirely sure how or why it's safer tho'.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    s_scanf reads from an array right not normal int ?

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    i hope this helps
    i used getchar in it for scanning the char
    Code:
    #include <stdio.h>
    #include <ctype.h>
    int main(void){
        char c;
        for(;;){
            puts("AIRLINE RESERVATION SYSTEM \n"
    	          "P - to Purchase Ticket \n"
                  "V - to View Arrangement \n"
    	          "Q - to Quit the system \n"
    	          "Enter the option : ");
            c=getchar();
            c=toupper(c);
            switch(c){
                case 'P':
                     puts("Purchase\n");
                     break;
                case 'V':
                     puts("View\n");
                     getchar();
                     break;
                case 'Q':
                     printf("Quit\n");
                     getchar();
                     break;
    	    default:
    			puts("Invalid Option \n"
    			         "Menu again\n");
    			continue;}
    			break;}
        return 0;
    }

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by lolguy View Post
    s_scanf reads from an array right not normal int ?
    It reads from a string, yes.
    scanf_s reads from the input (similar to scanf).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    Quote Originally Posted by Elysia View Post
    It reads from a string, yes.
    scanf_s reads from the input (similar to scanf).
    why is it safer than scanf ?

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I believe it checks the syntax of the format string. Read more about it here.

  10. #10
    Registered User
    Join Date
    Apr 2009
    Location
    Malaysia
    Posts
    2
    Thx for help, now I learn more about C Programming, thx for giving solution and suggestion ^^

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    Hello

    Just replace scanf_s ---> scanf.
    it will work fine.

    Regards,
    Swetha

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, but is there a need for it? The program will work the same regardless, so there's no point.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    From what I heard, scanf_s (for example) takes an additional buffer size parameter after every "%s". Microsoft claims that it is safer because the compiler will complain if you don't specify the buffer size.

    I don't see why couldn't they just warn whenever a "%s" is used without a field width ("%10s")...

    I'm gonna say it's just another Microsoft attempt at vendor lock-in.

    So I suggest changing it back to scanf for standard compliance (unless there is something I missed).
    Last edited by cyberfish; 04-23-2009 at 12:50 AM.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by cyberfish View Post
    I'm gonna say it's just another Microsoft attempt at vendor lock-in.
    But it isn't. It performs additional security checks that scanf doesn't.

    So I suggest changing it back to scanf for standard compliance (unless there is something I missed).
    I suggest not changing it. For standards compliance, all they have to do is add #define scanf_s scanf.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Elysia View Post
    But it isn't. It performs additional security checks that scanf doesn't.



    I suggest not changing it. For standards compliance, all they have to do is add #define scanf_s scanf.
    And why standards cometee should follow Microsoft and not the opposite?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement problem
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 03-08-2008, 04:05 PM
  2. problem on switch
    By toxicherry in forum C Programming
    Replies: 11
    Last Post: 12-31-2007, 05:17 AM
  3. Switch Problem
    By Tynnhammar in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2004, 11:57 AM
  4. Replies: 1
    Last Post: 08-31-2004, 04:07 AM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM