Switch Case

This is a discussion on Switch Case within the C++ Programming forums, part of the General Programming Boards category; I am using a Switch Case in my program, but I can't make it working! The program I am making ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    50

    Switch Case

    I am using a Switch Case in my program, but I can't make it working! The program I am making is just for practise and it will be a calculator when finished. The problem with my Switch Case is that the Switch variable(input) is not an integer. It is a char.
    It's value depends on what the user inputs.(A, B, C or D)
    I have used a ELSE IF statement earlier in the code but I am not sure if it has any effect on the working of the Switch Case.

    It is difficult to explain this, so have a look at the code:

    switch (input, 2, '\n')

    {
    case (!strcmpi("A", input))
    do z=x+y //this is where the compiler gives a bug report
    break

    case: (!strcmpi("B", input))
    do z=x-y
    break

    case: (!strcmpi("C", input))
    do z=x*y
    break

    case: (!strcmpi("D", input))
    do z=x:y
    break
    }

    ___________________

    I will not display the entire program here, but I know I am doing something terribly wrong.. I am sure I have included the right header files, and all the used variables exist. Ever time when I try to compile the compiler says that there is a Parse error before DO.

    Please someone tell me what I am doing wrong, the tutorial is not clear enough on Switch Case
    I dont know half as many of you half as well as I should like, and I like less than half of you half as well as you deserve...

  2. #2
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    Here is a working example of what you were trying to do i think. If you have any questions feel free to ask...
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	char input;
    	cout << "Enter A,B,C, or D:";
    	cin >> input;
    	int x=2,y=3,z=0;
    
    	switch (input)
    	{
    		 case 'A':
    			  z=x+y; //you need to use colons...
    		 break;
    
    		 case 'B':
    			  z=x-y;
    		 break;
      
    		 case 'C':
    			  z=x*y;
    		 break;
    
    		 case 'D':
    			  z=x/y;
    		break;
    	}
    	cout << "Z=" << z;
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The problem with my Switch Case is that the Switch variable(input) is not an integer. It is a char.
    A char is an integer.

    switch (input, 2, '\n')
    lol. What is that? Do you believe you can make up your own format for a switch statment, and somehow the computer will know what you mean?

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    A char is not an integer, it is used to store characters, but you can cast it so it will be treated as an integer, and an integer can be casted as a char:

    Code:
    char a='A';
    int x= (int) a; //or alternatively int(a); althought its a bit weird 
    cout<<(char)x;
    oh, and please use code tags (# button)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Or, more correctly:

    Code:
    char a='A';
    int x = static_cast<int>(a);
    cout << static_cast<char>(x);
    C-style casts should be avoided, and the appropriate one of the 4 new cast types used in their place, for good C++ style. It also makes it more clear what kind of cast you mean, and the compiler can inform you if that style of cast is allowed.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by JaWiB
    A char is not an integer, it is used to store characters, but you can cast it so it will be treated as an integer, and an integer can be casted as a char:
    Regardless of whether you consider it an integer or not, it is still an integral type (stored exactly the same way as an integer), so as far as the compiler is concerned, it is an integer.

    Its also good to have a 'default' statement at the end of the switch statement in the event that unexpected input is encountered.

    Code:
    switch(cond)
    {
       case 1:
          // ...
          break;
       ...
       case n:
          // ...
          break;
       default:
          // If the input didn't match any of the above...
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    50

    Reply

    Well, each time I treated char(input) the same as an integer, the compiler would tell me that input could not be treated as an integer.. now what about that??
    I dont know half as many of you half as well as I should like, and I like less than half of you half as well as you deserve...

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    50
    My tiny program is finished now, but it doesn't work quite right..
    I want my program to run all over from the beginning when it ends..
    I dont know half as many of you half as well as I should like, and I like less than half of you half as well as you deserve...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  4. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 09:42 AM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 01:07 AM

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