Ok, so I'm trying to get the user to enter a coordinate like E4. So I made a switch statement making all of the letter values from A-H (lowercase too) numbers. For example A=1, B=2, and so on. The way I'm separating the first part of the string (the letter, i.e. E) from the second part (i.e. 4) is by using string.at(0) and string.at(1). I store these into separate characters and for debugging purposes I display them and they are the values that I want. However, when I use the switch statement, even though the value should work, it uses the default case. I'll paste the code for you.
for (int n = 1; n < 8; n++)
{
retry:
cout << "Coordinates of Ship " << n << "(i.e. E4) from A-H and 1-7: ";
cin >> entry;
char x = entry.at(0);
char y = entry.at(1);
cout << x <<"\n";
cout << y;
switch (x)
{
case ('A'):
x = 1;
break;
case ('a'):
x = 1;
break;
case ('B'):
x = 2;
break;
case ('b'):
x = 2;
break;
case ('C'):
x = 3;
break;
case ('c'):
x = 3;
break;
case ('D'):
x = 4;
break;
case ('d'):
x = 4;
break;
case ('E'):
x = 5;
break;
case ('e'):
x = 5;
break;
case ('F'):
x = 6;
break;
case ('f'):
x = 6;
break;
case ('G'):
x = 7;
break;
case ('g'):
x = 7;
break;
case ('H'):
x = 8;
break;
case ('h'):
x = 8;
break;
default:
cout << "Incorrect position. Retype using format given.\n";
goto retry;
break;
}
I can give you more details if you want, so help me if you can. Thanks.
int row, col;
for (int n = 1; n < 8; n++) // n goes 1..7
{
cout << "Coordinates of Ship " << n << "(i.e. E4) from A-H and 1-7: ";
while (true)
{
cin.clear();
char x, y;
cin >> x >> y;
x = toupper( x );
// (see note)
if ((cin)
and (x < 'A') and (x > 'H')
and (y < '1') and (y > '7'))
break;
else
cout << "What? Try again: ";
}
row = y -'1'; // row := 0..6
col = x -'A'; // col := 0..7
}
BTW, if this is for a battleship game, a typical gameboard is 10 x 10, not 8 x 7.
(see below) note
You could also give the user the option to quit. Tell him that typing 'quit' at any time will let him escape. At the comment above, you could add
if (c == 'Q') return;
To exit back to the main function. (All this stuff is in a subroutine, right?)
Ok... I haven't tried it but I'm a little confused. if x<'A' ? How does that work? The compiler doesn't know the order of the alphabet, right? And sorry for my not knowing some basic stuff (this is the beginners forum after all) but when you use a while loop dont you have something like while (x==true)? how do you do while(true)? And also could you explain cin.clear() and what if (cin) means? sorry for all the random questions...
And don't worry about asking questions like this - I've seen far to many hours wasted at work when junior (and not so junior) developers were too afraid to ask questions in case they looked foolish. That's the point of these forums, to let people ask questions and get answers that help them understand and learn (rather than blindly copy).
First of all single letters are stored in a char variable type which is one byte big. A single byte can hold any integer between -128 to 127 . To allow the use of characters, numbers and punctuation etc, there is an ASCII standard chart that defines which number corresponds to which letter for 0 - 127. For example 'A' is 65 and 'a' is 97 (see http://www.asciitable.com/ for the full chart). Compilers have this conversion table built in so in source code, the letter is interchangeable with its number. So back to your question
x < 'A' is the same as x < 65 so in a way the compiler does know the order of the alphabet.
You are perfecly correct about while loops, while(true) will never come out of the loop. This is a never ending or infinite loop and is quite a common technique in the main control loop of a program. The only time you exit the loop is to exit the program.