I can't figure out a way to solve it~

class gyt
{
public:
gyt()
{
int x, y;
char sinput[5];
char ans[5];
char seat[8][37]=
{
"------------------------------------",
" S C R E E N ",
"------------------------------------",
"A|_1_||_2_||_3_||_4_||_5_||_6_||_7_|",
"B|_1_||_2_||_3_||_4_||_5_||_6_||_7_|",
"C|_1_||_2_||_3_||_4_||_5_||_6_||_7_|",
"D|_1_||_2_||_3_||_4_||_5_||_6_||_7_|",
"E|_1_||_2_||_3_||_4_||_5_||_6_||_7_|"
};
cout << seat[0] << endl << seat[1] << endl << seat[2] << endl << seat[3] << endl;
cout << seat[4] << endl << seat[5] << endl << seat[6] << endl << seat[7] << endl;
do
{
cout << "Please enter your seat choice: " << endl;
cin >> sinput;
system("cls");
switch(sinput[0])
{
case 'A':
{
x = 3;
break;
}

case 'B':
{
x = 4;
break;
}

case 'C':
{
x = 5;
break;
}

case 'D':
{
x = 6;
break;
}

case 'E':
{
x = 7;
break;
}

case 'a':
{
x = 3;
break;
}

case 'b':
{
x = 4;
break;
}

case 'c':
{
x = 5;
break;
}

case 'd':
{
x = 6;
break;
}

case 'e':
{
x = 7;
break;
}
default:
{
cout << "Please enter A - E for the first character only!" << endl;
cin >> sinput;
break;
}
}

switch(sinput[1])
{
case '1':
{
y = 3;
break;
}

case '2':
{
y = 8;
break;
}

case '3':
{
y = 13;
break;
}

case '4':
{
y = 18;
break;
}

case '5':
{
y = 23;
break;
}

case '6':
{
y = 28;
break;
}

case '7':
{
y = 33;
break;
}

default:
{
cout << "Please enter 1 - 7 for the second character only!" << endl;
cin >> sinput;
break;
}
}
seat[x][y]='*';
cout << seat[0] << endl << seat[1] << endl << seat[2] << endl << seat[3] << endl;
cout << seat[4] << endl << seat[5] << endl << seat[6] << endl << seat[7] << endl << endl << endl;
cout<< "Do you want to choose another seat?(Y/N): " << endl;
cin >> ans;

if(ans[0] != 'Y' && ans[0] != 'y' && ans[0] != 'N' && ans[0] != 'n')
{

cout << "Please enter Y or N only! " << endl;
cin >> ans;
}
}while (ans[0] == 'Y' || ans[0] == 'y');

if(ans[0] == 'N' || ans[0] == 'n')
{
cout << endl;
cout<< "Thank You for booking with us." <<endl;
Sleep(2000);
system("cls");
mm();
}
}
};

------------------------------------------------------------------------------
When I've been asked to cin sinput[5], i type aa for my seat choice.
But it str8 bring me to "Please Enter 1-7 only!"
Is there any other way to solve it?
There's no errors ...

Last edited on
your sintax in the case 'number' :its wrong
case '7':
{
y = 33;
break;
}

Fix: renplace with
case 'A':

x = 3;
break;


case 'B':

x = 4;
break;


case 'C':

x = 5;
break;


case 'D':

x = 6;
break;


case 'E':

x = 7;
break;


case 'a':

x = 3;
break;


case 'b':

x = 4;
break;


case 'c':

x = 5;
break;


case 'd':

x = 6;
break;


case 'e':

x = 7;
break;

default:

cout << "Please enter A - E for the first character only!" << endl;
cin >> sinput;
break;



switch(sinput[1])

case '1':

y = 3;
break;


case '2':

y = 8;
break;


case '3':

y = 13;
break;


case '4':

y = 18;
break;


case '5':

y = 23;
break;


case '6':

y = 28;
break;


case '7':

y = 33;
break;


default:

cout << "Please enter 1 - 7 for the second character only!" << endl;
cin >> sinput;
break;

hmmm... i still cant solve the problem,...
Firstly, please use the code tags when posting code. Find them on the right when editing your post.

To answer your initial question: if I input "a3", it works for me. Are you not getting the same behaviour?

Now, in my opinion there are a number of problems with your code. Firstly, such functionality shouldn't really be in a constructor. I don't know what the class 'gyt' is supposed to represent, but I'm sure that initialising an instance of it doesn't involve choosing a seat and outputting the result to the console. A basic console app like this doesn't need to use classes (basic methods and a main will do).

You need to think about the kinds of input that your program will receive. What happens if someone enters "blahblahblah"? Firstly, there's not enough memory in your sinput array to hold that many characters. Secondly, the way you've got your logic, the following line will execute even with invalid input:

seat[x][y]='*';

This will likely crash your program because x and/or y will not be reasonable values.

Another point: your case statements are unnecessary. Think about a way in which you, given a number from 1-7, calculate the corresponding values 3,8,13,18,23 and so on. Also, consider using a loop to output the seat display.

There are a number of ways we can further improve this, but only if you're interested. Let us know what you think about what I've written.
I will use the code tags for the next posting.

If i input "a3", it works too. Its just having problems when i input "aa, 77" .

This is only a small part of my program, plus the instruction told me to use classese.

I do try loop before i use switch cases, but i cant solve it... Maybe im still not familiar with the loops functions.

So, i wonder if you can give me some tips or ways to solve it, i will very appreciate...
Sorry it took me so long to get back to you.

When you're looking at the first character of the input, you can find out if it's alphabetic by using the isalpha function. If it is, you can convert it to upper case, subtract the character '@' from it and convert it to an integer. Thus 'a' would be converted to 1, 'B' would be converted to 2 etc. After that, you can simply check to see if the integer is in the range 1-7, and you won't need the massive switch statement.

Perhaps that wasn't clear enough. Here's some pseudo-code:

1. Check if the sinput[0] character is alphabetic.
2. If YES:
2a. Convert the character to upper case using the toupper function.
2b. Assign the value of (converted character - '@') to a predefined integer variable (let's call it row).
3. Otherwise:
3a. Assign the value of (converted character - '0') to row
4. Check if row is in the range 1-7. If it is, you have valid input for the first character. Otherwise you have an error.

I hope that's clear. See how you go translating it into code. A similar approach should be used for the second character (i.e. the seat number on a particular row).

Let us know how you go.

P.S. Check out the ASCII character chart to see how this conversion from characters to integers works: http://en.wikipedia.org/wiki/Ascii#ASCII_printable_characters
P.P.S. The functions I mentioned (like toupper) are C functions. Google them if required.



Topic archived. No new replies allowed.