Thread: Having A problem...probably right under my nose

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

    Having A problem...probably right under my nose

    So I dont know why this will not work...everything looks fine.....Ive been writing code for 4hrs this morning so its probably the smallest thing Im missing and ho[ing someone can spot it out for me....thanks in advance....my eyes are bugging!

    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");
    
    
    	switch(n) {
    	case '1':
    		printf("Car 1 is a Ford\n");
    		break;
    
    
    		case'2':
    			printf("Car 2 is a Chev\n");
    			break;
    
    			
    			case'3':
    				printf("Car 3 is a Foreign\n");
    				break;
    
    
    				default:
    					break;
    
    	}
    
    					return 0; //Return to the IDE
    	}
    What Im trying to do is get is to ask me to input 1, 2 or 3 and it will display Ford, Chev, or Foreign. Its ........ing me off that I cannot find the problem. I get error saying 'n' in not initialized, when it is.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    You don't read in anything.
    try
    Code:
    scanf("%d", &n);

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That's probably not an error, but a warning. It's telling you that you didn't set a value for n before you used it. Looking at your code, you have no code for reading the input in from the user.

    Since you are using C code, you should probably post these questions in the C forum, but the simple answer would be to read in a value for n before the switch.

    If you use Quantum1024's suggestion it won't quite work because that code reads in an integer, so n will have 1, 2 or 3. The case statements in the switch are looking at characters: '1', '2', and '3', so they will never match ('1' does not equal 1). To fix it, either change scanf to read in a char, or change your case statements to use integers instead of characters by removing the single quotes.
    Last edited by Daved; 11-21-2005 at 01:07 PM.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    4
    Thanks guys. Sorry for posting here, Ill post in the C forum.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    ^^^Daved Nice catch.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM