Thread: Switch statement problem

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    17

    Post Switch statement problem

    I am new to c and I am buliding a program with a buch of nested if and a switch statement based on my an input of a character from a scanf statement. the problem is when you ask the question what type it automatically defaults to illegal because I believe it is taken in the character alomg with a carriage return but I'm not sure. The following is what I have and according to the structure in the book it should work. If that is the problem how do I get around the carriage return.
    Thanks
    Code:
    printf("What is the customer code?\n");
    	scanf("%c\n",&cc);
    		
    		switch (cc) {
    		case 'R' :
    		case 'r' :
    			thresh = .0865;
    			excess = fee_r;
    			rate = r;	
    			break;
    
    		case 'C' :
    		case 'c' :
    			thresh = .0695;
    			excess = fee_c;
    			rate = c;		
    			break;
    
    		case 'I' :
    		case 'i' :
    			thresh = .0584;
    			excess = fee_i;
    			rate = i;	
    		break;
    		default:
    			printf("Illegal customer code %c\n", cc);
    	}

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Your 'I' case probably wants a break after it(?).

    I generally prefer to read a line of input as a string.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Dave_Sinkula View Post
    Your 'I' case probably wants a break after it(?).
    looks like there is one, just a little hidden.

    If you put a whitespace character in there, it'll discard whitespace characters.
    Code:
    scanf(" %c",&cc);

  4. #4
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Nice trick rob, works charmingly... even with strings. I had never though about such a simple solution to the eternal newline problem.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Thanks, but I didn't think of it... somebody else on these boards said it before me, I think.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    17

    Smile Thanks

    I'll tell you sometimes you can understand the hardest part of something whether it be programing like this or anything in life but what seems to be the simpliest you can rack your brains. I appreciate the assistance!! I am new to this board and want to make sure that wasn't a violation of the homework policy. I will never ask anyone to write a program for me because thats my job and I rather understand what I'm doing, but your assistance got me be back to the meat in which the rest is just about done. I am taking a C course and so far so good but sometimes you just reach a point in a program where you a hit a wall and just need a little assistance and I'm hoping this is what a forum in this subject can do for you. Thanks!!!!!!!!!!!!

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Super! Glad it worked out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.get(); not working with my switch statement
    By tenor_jazz13 in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2008, 10:33 PM
  2. having problem with string statement during a loop!
    By Hp7130p in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2005, 09:40 AM
  3. Switch statement
    By big146 in forum C++ Programming
    Replies: 7
    Last Post: 06-25-2004, 07:16 AM
  4. begginner and I need help, switch statement
    By trkpony in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 08:03 AM
  5. Equivalent of less than in a switch statement?
    By Diamonds in forum C++ Programming
    Replies: 5
    Last Post: 10-14-2002, 07:14 PM