double value switch problem

This is a discussion on double value switch problem within the C++ Programming forums, part of the General Programming Boards category; hey guys, here's my problem: I'm getting an imput in form of a char variable and an int variable. The ...

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572

    double value switch problem

    hey guys, here's my problem: I'm getting an imput in form of a char variable and an int variable. The inputs will be similar to A1 A4 A7 B2 B4 B6...and so on.

    I'm putting the letter into a char var and the number into int, than I'm trying to match these two some global variables. To do this I want to use switch, but when I do switch(letter+number) and then the the cases, after a while I'm getting an error message saying that "this value has been allready used. Please help in finding a way to do this...

    here is the code

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std; 
    
    	//declaring global variables to hold the positions on the board
    char p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12; 
    char p13, p14, p15, p16, p17, p18, p19, p20, p21, p22, p23, p24;
    
    
    
    	//function displaying the board
    void displayBoard()
    {
    
    	cout << "\n\n";
    	cout << "  1  2  3  4  5  6  7\n\n"
    		 << "A "<<p1<<"........"<<p2<<"........"<<p3<<" A\n"
    		 << "  .        .        .\n"
    		 << "B .  "<<p4<<"....."<<p5<<"....."<<p6<<"  . B\n"
    		 << "  .  .     .     .  .\n"
    		 << "C .  .  "<<p7<<".."<<p8<<".."<<p9<<"  .  . C\n"
    		 << "  .  .  .     .  .  .\n"
    		 << "D "<<p10<<".."<<p11<<".."<<p12<<"     "<<p13<<".."<<p14<<".."<<p15<<" D\n"
    		 << "  .  .  .     .  .  .\n"
    		 << "E .  .  "<<p16<<".."<<p17<<".."<<p18<<"  .  . E\n"
    		 << "  .  .     .     .  .\n"
    		 << "F .  "<<p19<<"....."<<p20<<"....."<<p21<<"  . F\n"
    		 << "  .        .        .\n"
    		 << "G "<<p22<<"........"<<p23<<"........"<<p24<<" G\n\n"
    		 << "  1  2  3  4  5  6  7\n" 
    		 << endl;
    
    }
    
    void matchPosition(char letter, int number)
    {
    
    	switch(letter, number){
    		case 'A'+1: case 'a'+1: p1='x'; break;
    		case 'A'+4: case 'a'+4: p2='x'; break;
    		case 'A'+7: case 'a'+7: p3='x'; break;
    		case 'B'+2: case 'b'+2: p4='x'; break;
    		case 'B'+4: case 'b'+4: p5='x'; break;
    		case 'B'+6: case 'b'+6: p6='x'; break;
    		case 'C'+3: case 'c'+3: p7='x'; break;
    		case 'C'+4: case 'c'+4: p8='x'; break;
    		case 'C'+5: case 'c'+5: p9='x'; break;
    		case 'D'+1: case 'd'+1: p10='x'; break;
    		case 'D'+2: case 'd'+2: p11='x'; break;
    		case 'D'+3: case 'd'+3: p12='x'; break;
    		case 'D'+5: case 'd'+5: p13='x'; break;
    		case 'D'+6: case 'd'+6: p14='x'; break;
    		case 'D'+7: case 'd'+7: p15='x'; break;
    		case 'E'+3: case 'e'+3: p16='x'; break;	
    		case 'E'+4: case 'e'+4: p17='x'; break;
    		case 'E'+5: case 'e'+5: p18='x'; break;
    		case 'F'+2: case 'f'+2: p19='x'; break;
    		case 'F'+4: case 'f'+4: p20='x'; break;
    		case 'F'+6: case 'f'+6: p21='x'; break;
    		case 'G'+1: case 'g'+1: p22='x'; break;
    		case 'G'+4: case 'g'+4: p23='x'; break;
    		case 'G'+7: case 'g'+7: p24='x'; break;*/
    
    	}
    
    	//cout << letter << number;
    
    
    }
    
    	//function displaying prompt and storring player's move
    void displayPromptAndGetMove(){
    
    	int column;
    	char row;
    	
    
    	cout << "\nEnter a move position: ";
    	cin >> row >> column;
    
    	matchPosition(row, column);
    	
    			
    }
    
    int main(){
    	
    	//setting all the global variables equal a period (.)
    	p1 = p2 = p3 = p4 = p5 = p6 = p7 = p8 = p9 = p10 = p11 = p12 = '.';
    	p13 = p14 = p15 = p16 = p17 = p18 = p19 = p20 = p21 = p22 = p23 = p24 = '.';
    
    	bool notDone = true;
    
    	displayBoard();
    
    	while(notDone){
    
    	displayPromptAndGetMove();
    	displayBoard();
    
    
    
    
    
    	}
    	return 0;
    }
    thanks,

    axon

    P.S. by the way, I can't use arrays for this one :-(
    Last edited by axon; 02-13-2003 at 10:36 PM.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    can you use strings?

    if not, I don't think you can do a switch like that, I could be wrong, but that might be what is causing the error.

    you could do either of these if you can't use strings, they are kindof long though.

    Code:
    switch(letter) {
        case 'A':
        case 'a':
            switch(number) {   
                case 1:
                    p1 = 'x';
                    break;
                //...
            }
            break;
        //...
    }
    Or

    Code:
    if(letter == 'a' || letter == 'A') {
        if(number == 1) {
            p1 = 'x';
        }
        //...
    } else if(/*...*/) {
        //...
    }

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    I just found out that I COULD! use strings just for that part, so I guess it makes it easier now! I'll post the code later for you guys tp review it. But anyhow I wonder if it is doable without the nested switch statements. Yes I know that the if-else option exists, and is simple, but very messy and much toooooo long,

    thanks for reply,

    axon

  4. #4
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    this should probably be done with ifs.

    you aren't using the switch statement correctly, so you should check your text book or reference manual about the proper usage of it.

    And the reason you are getting an error about two cases having the same value is because you are forgetting that a char is really just a number and therefore if you say 'a'+2 or 'd'-1 or 'c' they are really just all the exact same number

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    I figured it out - thanks for the input.

    Basically what I did, is that as soon as I got the input, I converted it to CAPS and than I used if else statements.

    axon

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. expected primary expression
    By mju4t in forum C Programming
    Replies: 2
    Last Post: 03-27-2007, 06:59 PM
  4. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM
  5. while, sentinel, if, switch
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2001, 10:50 PM

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