Thread: switch problem

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    36

    switch problem

    my code seems to not return any of the case statements other than default, when i enter in a number larger than zero... and im not sure why...

    help please?

    Code:
    #include <stdio.h>
    
    int ResColour(int colour);
    
    int main(void)
    {
    
    	int input;
    	int rc;
    	int colour;
    
    	printf("Please enter a resistor value in ohms:\n");
    
    	rc = scanf("%d",&input);
    
    	if(rc!=1)
    		printf("Please enter a valid resistor value!\n");
    	else if(input==0)
    		printf("Please enter a valid resistor value!\n");
    	else if(input < 0)
    		printf("Please enter a valud resistor value!\n");
    	else if(input > 0)
    	{
    		
    			printf("The colour code for resistor value %d is:\n",input);
    			ResColour(colour);
    		
    	}
    
    	printf("\n\n");
    
    	return(0);
    }
    
    int ResColour(int colour)
    {
    	
    
    	switch(colour)
    	{
    		case 0: printf( " black");
    				break;
    		case 1: printf( " brown");
    				break;
    		case 2: printf( " red");
    				break;
    		case 3: printf( " orange");
    				break;
    		case 4: printf( " yellow");
    				break;
    		case 5: printf( " green");
    				break;
    		case 6: printf( " blue");
    				break;
    		case 7: printf( " violet");
    				break;
    		case 8: printf( " grey");
    				break;
    		case 9: printf( " white");
    				break;
    		default: printf( " unknown digit detected");
    
    		return(colour);
    	}
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    if(rc!=1)
    		printf("Please enter a valid resistor value!\n");
    	else if(input==0)
    		printf("Please enter a valid resistor value!\n");
    	else if(input < 0)
    		printf("Please enter a valud resistor value!\n");
    Is over complex and redundant,

    Code:
    if(rc != 1)
        printf("Please enter a valid resistor value!\n");
    else{
        printf("The colour code for resistor value &#37;d is:\n",input);
        ResColour(colour);
    }
    Is all that is required.

    And as for your problem,
    Code:
    int colour;
    
    ResColour(colour);
    You haven't given colour a value nor initialized it, sure you don't mean ResColour(Input); instead?

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    36
    Quote Originally Posted by zacs7 View Post
    And as for your problem,
    Code:
    int colour;
    
    ResColour(colour);
    You haven't given colour a value nor initialized it, sure you don't mean ResColour(Input); instead?

    damn... you're right. it should've been ResColour(input)!

    thank you so much for your help

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    36
    ok, my code has been modified:

    it works great when only one integer is entered, but for numbers w/ 2 digits, for example, 56, it is printing out " unknown digit detected".

    I understand that im going to need a loop nested inside of my if else, but im having problems figuring out how i can set the loop to read in as many integers as entered.

    example... if entered in 560
    i want the screen to print: green blue black

    help please?

    Code:
    #include <stdio.h>
    
    int ResColour(int input);
    
    int main(void)
    {
    
    	int input;
    	int rc;
    
    	printf("Please enter a resistor value in ohms:\n");
    
    	rc = scanf("&#37;d",&input);
    
    	if(rc != 1)
    		printf("Please enter a valid resistor value!\n");
    	else
    	{
    		printf("The colour code for resistor value %d is:\n",input);
    		ResColour(input);		
    		
    	}
    	printf("\n\n");
    
    	return(0);
    }
    
    int ResColour(int input)
    {
    	
    
    	switch(input)
    	{
    		case 0: printf( " black");
    				break;
    		case 1: printf( " brown");
    				break;
    		case 2: printf( " red");
    				break;
    		case 3: printf( " orange");
    				break;
    		case 4: printf( " yellow");
    				break;
    		case 5: printf( " green");
    				break;
    		case 6: printf( " blue");
    				break;
    		case 7: printf( " violet");
    				break;
    		case 8: printf( " grey");
    				break;
    		case 9: printf( " white");
    				break;
    		default: printf( " unknown digit detected");
    
    		return(input);
    	}
    }
    Last edited by sh4k3; 06-10-2007 at 09:42 PM.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Idea:

    Don't read the number as an int. Read the number as a string, and pass it char by char into a function to determine the full number's color scheme...

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    36
    Quote Originally Posted by MacGyver View Post
    Idea:

    Don't read the number as an int. Read the number as a string, and pass it char by char into a function to determine the full number's color scheme...


    sorry, im a bit confused by what you said...

    do u mean to declare my variables as data type string, instead of int?

    and for my function prototype, instead of declaing it:

    int ResColour(int colour)

    do this instead:

    char ResColour(char colour) ?

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Yes, basically.

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    36
    string input;
    string rc;


    are u sure thats right?
    the text doesnt even change to blue when i do that...

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    903
    He actually really meant a character array, when he said a string... Otherwise that would be std::string, but that's C++, not C.

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    A string is merely a null-terminated character array...

  11. #11
    Registered User
    Join Date
    Jun 2007
    Posts
    36
    ok... i modified my program to read in all integers and then display the colours.

    The problem is now reading the third integer. With resistor values, the first two integers read result in a colour, and the third integer is known as a multiplier. For example
    220 = red red brown
    since the first two integers are red
    and the last integer is reading how many zeros there are, in this case 1, therefore printing the colour code red red brown.

    Can someone please suggest an idea to prevent my program from reading the third integer as a colour code?

    Code:
    #include <stdio.h>
    
    int ResColour(int input);
    int band(int b);
    
    int main(void)
    {
    
    	int input;
    	int rc;
    	int i;
    	int divisor;
    	int digit;
    	int band = 1;
    
    
    	printf("Please enter a resistor value in ohms:\n");
    
    	rc = scanf("%d",&input);
    	printf("The colour code for resistor value %d is:\n",input);
    
    	if(rc != 1)
    		printf("Please enter a valid resistor value!\n");
    	else
    	{
    			i = input;
    			divisor = 10000;
    			while(divisor!=0)
    				{
    					if(input>=divisor)
    						{
    								
    								digit = (i/divisor);
    								i = i % divisor;
    								ResColour(digit);																
    									
    						}
    					
    					divisor = divisor / 10;
    					
    										
    				}
    		
    		
    			
    			//ResColour(input);
    		
    		
    	}
    	
    	printf("\n\n");
    
    	return(0);
    }
    
    int ResColour(int input)
    {
    
    	switch(input)
    	{
    		case 0: printf( " black");
    				break;
    		case 1: printf( " brown");
    				break;
    		case 2: printf( " red");
    				break;
    		case 3: printf( " orange");
    				break;
    		case 4: printf( " yellow");
    				break;
    		case 5: printf( " green");
    				break;
    		case 6: printf( " blue");
    				break;
    		case 7: printf( " violet");
    				break;
    		case 8: printf( " grey");
    				break;
    		case 9: printf( " white");
    				break;
    		default: printf( " unknown digit detected");
    
    		return(input);
    	}
    	
    }

  12. #12
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    As far as I can see in your prog the user only enters an integer. Heres an example I made where the user can enter a string, the first three characters are then converted to a number (0-9).
    Code:
    #include <stdio.h>
    
    int main()
    {
        char temp[100]; //Temporary string for holding input
        int col_1, col_2, resistor;
        
        gets(temp);
        
        
        col_1=temp[0]-48;    //Digit for colour 1 equals the first character entered    
        col_2=temp[1]-48;    //Colour 2 equals the second character  
        resistor=temp[2]-48; //minus 48 off ASCII value to get numeric value
    
        printf("Colour 1: %i\nColour 2: %i\nResistor: %i", col_1, col_2, resistor);
        getchar();
    }
    By getting input as a string the program is generally less likely to screw up on you and do bizzare things. That said if you had to extract the digits from an integer it could be done by a combination of dividing and modding. Example:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int input, col_1, col_2, resistor;
        scanf("%i", &input);
        col_1=input/100;
        col_2=(input/10)%10;
        resistor=input%10;
        printf("Colour 1: %i\n Colour 2: %i\n Resistor: %i", col_1, col_2, resistor);
        getchar(); 
        getchar();
    }
    Hope this helps

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    gets() is a very bad idea. http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    Rather than just calling getchar() twice it would be preferable to use
    Code:
    int c;
    while((c = getchar()) != '\n' && c != EOF);
    twice. Well, int c once, of course.

    '0' is much more readable than 48.
    Code:
    int char2number(char c) {
        return c - '0';
    }
    [edit] Not to mention // comments, though they are pretty much acceptable. [/edit]
    Last edited by dwks; 06-11-2007 at 04:05 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yeah I noticed from posts here that gets() seems to be a bad way to get a string. I have just been using it because I don't really know any better yet, but I will read that section in the FAQ now.

    This looks cool:
    Code:
    int c;
    while((c = getchar()) != '\n' && c != EOF);
    I guess EOF must be for escape.

    Cheers.

  15. #15
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    EOF is a way to tell if the character you read signifies something is wrong on the stream. Basically, it would mean that you should not assume you will get anymore valid characters.

    It means End Of File.

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