This is driving me nuts...

This is a discussion on This is driving me nuts... within the C Programming forums, part of the General Programming Boards category; I want my program to ask me to input #1, 2, or 3 and it will display 1 = Ford, ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    4

    This is driving me nuts...

    I want my program to ask me to input #1, 2, or 3 and it will display 1 = Ford, 2=chev, 3=foreign....It executes with no errors of warnings but it stops before it gets to the switch command.

    Code:
    #include<stdio.h>
    
    main()
    {
    	int n;
    	printf("Please enter a single digit for a car.\n");
    	printf("(within the range of 1 to 3):\n");
    	n=getc(stdin);
    
    
    	switch(n) {
    	case 1:
    		printf("Car %d is a Ford.\n",n);
    		scanf("%d\n",n);
    		break;
    	
    
    		case 2:
    			printf("Car %d is a Chev.\n",n);
    			scanf("%d\n",n);
    			break;
    		
    			
    			case 3:
    				printf("Car %d is a Foreign.\n",n);
    				scanf("%d\n",n);
    				break;
    
    
    				default:
    					break;
    
    
    	}
    
    					return 0; //Return to the IDE
    	}
    It displays "Please enter a single digit for a car".....you press anything it stops the program. I know its something stupid and small. Any help would be appreciated for this newbie.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Your comparing the charactor to numbers. AS was mentioned in the other thread if you want to compare to charactors then use '1', '2' and '3' or else use an int
    also since you are asking for a charactor and then printing the decimal equivilant this will be an ASCII code.

  3. #3
    Registered User fischerandom's Avatar
    Join Date
    Aug 2005
    Location
    Stockholm
    Posts
    71
    Try this:
    Code:
    int		main( ) {
    	int		n;
    	
    	printf( "Please enter a single digit for a car.\n" );
    	printf( "(within the range of 1 to 3):\n" );
    	n = fgetc( stdin );
    	
    	switch ( ( char ) n ) {
    	case '1':
    		printf( "Car %d is a Ford.\n", n );
    		scanf( "%d\n", n );
    		
    		break;
    	
    	case '2':
    		printf( "Car %d is a Chev.\n", n );
    		scanf( "%d\n", n );
    		
    		break;
    	
    	case '3':
    		printf( "Car %d is a Foreign.\n", n );
    		scanf( "%d\n", n );
    		
    		break;
    	}
    	
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    4
    ohhhh, Im slow, Thanks man....Im greatful. I mis read the last post. Being new to this and trying to teach yourself sucks.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The typecast is not needed. All case statements are integeral, and as such, will be promoted to integers.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    No need for scanf inside switch { }
    try coding:

    Code:
    #include<stdio.h>
    
    main()
    {
    	int n;
    	printf("Please enter a single digit for a car.\n");
    	printf("(within the range of 1 to 3):\n");
    	scanf("%d",&n);
    
    
    	switch(n) {
    	case 1:
    		printf("Car %d is a Ford.\n",n);
    		break;
    	
    
    		case 2:
    			printf("Car %d is a Chev.\n",n);
    			break;
    		
    			
    			case 3:
    				printf("Car %d is a Foreign.\n",n);
    				break;
    
    
    				default:
    					break;
    
    
    	}
    
    					return 0; //Return to the IDE
    	}
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. This is driving me nuts.
    By Matt13 in forum C Programming
    Replies: 3
    Last Post: 03-25-2004, 09:55 AM
  3. i can't fix it! its driving me nuts
    By faxtoaster in forum C Programming
    Replies: 2
    Last Post: 07-10-2003, 05:46 PM
  4. This compiler is driving me nuts
    By martin_00 in forum C Programming
    Replies: 32
    Last Post: 12-31-2002, 02:25 PM
  5. driving me nuts
    By boontune in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2002, 04:35 AM

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