switch problem

This is a discussion on switch problem within the C Programming forums, part of the General Programming Boards category; It's why c must be an int. A char can hold character values, but because EOF is a state of ...

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    It's why c must be an int. A char can hold character values, but because EOF is a state of the stream rather than an actual character in the file, the variable you hold EOF in must be able to hold more values than a char. int is usually what's used.

    Anyway, for replacing gets(), just use something like this.
    Code:
    char buffer[BUFSIZ], *p;
    if(fgets(buffer, sizeof(buffer), stdin)) {
        if((p = strchr(buffer, '\n'))) *p = 0;
        printf("Hello, %s!\n", buffer);
    }
    else perror("Error reading line");
    BUFSIZ is defined in <stdio.h>. It's usually at least 512, and I've seen it defined as 8192. fgets() doesn't remove the newline from the line like gets() does, so my code removes it manually with strchr() (from <string.h>). Lastly, the return value of fgets() is checked to make sure it worked.

    A common idiom, to get every line possible, is to use
    Code:
    while(fgets(buffer, sizeof(buffer), stdin)) {
        /* process buffer */
    }
    The loop exits when EOF or some other error is reached.

    Also, one other note: when using sizeof() on actual variables instead of types like int, you can leave off the parentheses.
    Code:
    fgets(buffer, sizeof buffer, stdin);
    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.

  2. #17
    Registered User
    Join Date
    Jun 2007
    Posts
    36
    Quote Originally Posted by mike_g View Post
    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: &#37;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

    thanks, i havent even really got into arrays yet, so i cant use that

    but i have modified my code to WORK properly now!

    thanks for everbodies inputs, its much appreciated!!

    here is my WORKING code:

    Code:
    #include <stdio.h>
    
    int ResColour(int input);
    int after(int divisor);
    
    int main(void)
    {
    
    	int input;
    	int rc;
    	int i;
    	int divisor;
    	int digit;
    	int band=0;
    
    
    
    	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; // setting i to input entered
    			divisor = 1000000000; // setting divisor to 10000
    			while(divisor!=0) // keeps looping while true
    				{
    					if(input>=divisor) // if input entered is greater than 10000
    						{
    							
    								digit = (i/divisor);  // setting digit to input entered divided by 10000
    								i = i % divisor; // dividing i by divisor to check for remainder and assigning remainder to i
    								++band;
    								ResColour(digit); // passing digit to ResColour	
    						
    								if(band>=2)
    									{
    										after(divisor);
    										return(0);
    									}
    							
    						}
    
    						divisor = divisor / 10;
    				}
    		
    		
    	}
    	
    	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( "  Too big");
    
    		return(input);
    	}
    	
    }
    int after(int divisor)
    {
    	switch (divisor)
    	{
    		case 1: printf( "  Black\n");
    				break;
    		case 10: printf( "  Brown\n");
    				break;
    		case 100: printf( "  Red\n");
    				break;
    		case 1000: printf( "  Orange\n");
    				break;
    		case 10000: printf( "  Yellow\n");
    				break;
    		case 100000: printf( "  Green\n");
    				break;
    		case 1000000: printf( "  Blue\n");
    				break;
    		case 10000000: printf( "  Violet\n");
    				break;
    		case 100000000: printf( "  Grey\n");
    				break;
    		case 1000000000: printf( "  White\n");
    				break;
    		default: printf( "  Number too big\n");
    
    		return(divisor);
    	}
    	
    }
    Last edited by sh4k3; 06-11-2007 at 11:25 PM.

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    One thing caught my eye.
    Code:
    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( "  Too big");
    
    		return(input);
    	}
    	
    }
    If the default case was not executed, the function returns an undefined value.

    Also, I don't like large switch statements like that which do basically the same thing. You could use something like this, or you could leave it as it is if this looks too complicated:
    Code:
    const char *colour[] = {
        "Black", "Brown", "Red", /* ... */
    };
    
    if(input >= 0 && input <= 9) {
        printf("  &#37;s", colour[input]);
    }
    else printf("  Too big");
    Oh yes, and this is entirely optional, but this
    Code:
    divisor = divisor / 10;
    could be written as
    Code:
    divisor /= 10;
    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.

  4. #19
    Registered User
    Join Date
    Jun 2007
    Posts
    36
    Quote Originally Posted by dwks View Post
    One thing caught my eye.
    Code:
    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( "  Too big");
    
    		return(input);
    	}
    	
    }
    If the default case was not executed, the function returns an undefined value.

    Also, I don't like large switch statements like that which do basically the same thing. You could use something like this, or you could leave it as it is if this looks too complicated:
    Code:
    const char *colour[] = {
        "Black", "Brown", "Red", /* ... */
    };
    
    if(input >= 0 && input <= 9) {
        printf("  %s", colour[input]);
    }
    else printf("  Too big");
    Oh yes, and this is entirely optional, but this
    Code:
    divisor = divisor / 10;
    could be written as
    Code:
    divisor /= 10;

    hmm... thanks for the input
    ill definitely try to figure out what is happening there, but i dont think im gonna use that just yet due to the fact that we just introduced arrays today, but thx!

Page 2 of 2 FirstFirst 12
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, 03:05 PM
  2. problem on switch
    By toxicherry in forum C Programming
    Replies: 11
    Last Post: 12-31-2007, 04: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, 03:49 PM

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