For some reason, my program will not let the user enter his/her row number. Here's the code:

#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
int a=0,A=0,b=1,B=1,c=2,C=2,d=3,D=3,e=4,E=4,f=5,F=5;
void ticketchoices();
void displaychartfirst(char firstclass[2][6],int ticketnumselection);
void displaychartbus(char busiclass[5][6],int ticketnumselection);
void displaycharteco(char ecoclass[6][6],int ticketnumselection);



int main(int argc, char *argv[])
{
char X;
char O;
char firstclass[2][6]={{'O','O','X','O','X','X'},  //first class array.
                       {'O','X','O','X','O','X'}};
char busiclass[5][6]= {{'O','O','X','X','O','X'},  //business class array.
                       {'X','O','X','O','X','X'},
                       {'O','X','O','X','O','O'},
                       {'O','X','O','O','O','X'},
                       {'X','O','O','O','X','X'}};
char ecoclass[6][6]=  {{'O','X','O','X','X','O'},  //economy class array.
                       {'X','O','X','X','O','X'},
                       {'O','X','O','X','X','X'},
                       {'O','O','X','O','X','O'},
                       {'O','O','X','X','O','X'},
                       {'O','O','O','O','X','O'}};
int ticketnumselection;
cout<<"Welcome! Please, select a ticket type:"<<endl;
ticketchoices();
cout<<endl;
cout<<"Enter choice: ";
cin>>ticketnumselection;
cout<<endl;
switch(ticketnumselection)
{case 1: cout<<"You've chosen first class."<<endl<<endl;
         break;
 case 2: cout<<"You've chosen business class."<<endl<<endl;
         break;
 case 3: cout<<"You've chosen economy class."<<endl<<endl;
         break;}
displaychartfirst(firstclass,ticketnumselection);
displaychartbus(busiclass,ticketnumselection);
displaycharteco(ecoclass,ticketnumselection);
int sr1,sc1,sr2,sc2,sr3,sc3;
if(ticketnumselection==1)
{
cout<<"Please, enter your seat position, below (Example: A2):"<<endl;
cout<<"Enter seat letter: ";
cin>>sc1;
}
if(ticketnumselection==1)
{
cout<<"Enter row number: ";
cin>>sr1;
}
 
if(ticketnumselection==2)
{
cout<<"Please, enter your seat position, below (Example: C3):"<<endl;
cout<<"Enter seat letter: ";
cin>>sc2;
cout<<"Enter row number: ";
cin>>sr2;
}

if(ticketnumselection==3)
{
cout<<"Please, enter your seat position, below (Example: D11):"<<endl;
cout<<"Enter seat letter: ";
cin>>sc3;
cout<<"Enter row number: ";
cin>>sr3;
}





system("PAUSE");
return EXIT_SUCCESS;}

void ticketchoices()
{cout<<"Press 1 to select first class."<<endl;
cout<<"Press 2 to select business class."<<endl;
cout<<"Press 3 to select economy class."<<endl;
return;}

void displaychartfirst(char firstclass[2][6], int ticketnumselection)
{
int r=0,c=0;
if(ticketnumselection==1)
 {cout<<"       A  B  C  D  E  F"<<endl<<endl;
  for(r=0;r<2;++r)
   {  if(r>=1)
      {cout<<endl;}
   cout<<"Row "<<r+1<<"  ";
    for(c=0;c<6;++c)
     {cout<<firstclass[r][c]<<"  ";}}}
     cout<<endl;
     return;}

void displaychartbus(char busiclass[5][6],int ticketnumselection)
{
int r=0,c=0;
if(ticketnumselection==2)
 {cout<<"       A  B  C  D  E  F"<<endl<<endl;
  for(r=0;r<5;++r)
   { if(r>=1)
     {cout<<endl;}
   cout<<"Row "<<2+r+1<<"  ";
    for(c=0;c<6;++c)
     {cout<<busiclass[r][c]<<"  ";}}}
     cout<<endl;
     return;}

void displaycharteco(char ecoclass[6][6],int ticketnumselection)
{
int r=0,c=0;
if(ticketnumselection==3)
 {cout<<"       A  B  C  D  E  F"<<endl<<endl;
  for(r=0;r<6;++r)
   {  if(r>=1)
      {cout<<endl;}
   cout<<"Row "<<7+r+1<<"  ";
    for(c=0;c<6;++c)
     {cout<<ecoclass[r][c]<<"  ";}}}
     cout<<endl;
     return;}

Recommended Answers

All 14 Replies

Make variables sc1, sc2, and sc3 of type char, as you want the user to enter a signle character in them.

Make variables sc1, sc2, and sc3 of type char, as you want the user to enter a signle character in them.

I want the user to enter a letter A through F, yes; however, I want A/a=0, B/b=1, etc. In the function main, I declared and initialized upper and lowercase letters, A-F, 0-5, as ints. As far as the rows go, just a regular old int is fine.

I want the user to enter a letter A through F, yes; however, I want A/a=0, B/b=1, etc. In the function main, I declared and initialized upper and lowercase letters, A-F, 0-5, as ints. As far as the rows go, just a regular old int is fine.

do the type checking first after you receive the user input . When you know the type, the you check the value. if say the type is a char, ... you write ifs to assign an int of 0-5 to your checking variable then switch for action.

A little logic here. type checking...

In the function main, I declared and initialized upper and lowercase letters, A-F, 0-5, as ints.

No, you didn't declared and initialised any letters, because you can't!
You just declared and initialised variables with letter names, nothing more.
I tested the program setting the variables to char and it works just fine.

No, you didn't declared and initialised any letters, because you can't!
You just declared and initialised variables with letter names, nothing more.
I tested the program setting the variables to char and it works just fine.

I did what you told me:

int sr1,sr2,sr3;
char sc1,sc2,sc3;
char a=0,A=0,b=1,B=1,c=2,C=2,d=3,D=3,e=4,E=4,f=5,F=5;

This minor change in code made my program work perfectly. Thank you.

With that: Why did my program not run properly with the original code I gave in my OP?

int x=1,X=1;
int sc1;
cout<<"Enter column letter: ";
cin>>sc1;
cout<<"The number associated with the letter on your seat is: "<<sc1<<endl;
//sc1 gives me a garbage value. Why, exactly, does this method not help me
//accomplish my task?

It should be made clear that this:

char a=0,A=0,b=1,B=1,c=2,C=2,d=3,D=3,e=4,E=4,f=5,F=5;

does NOT associate letters with numerical values. When the user hits 'f' for example, you WON'T get the number five instead. These are just simple char variables. This line is useless in your programm.

It should be made clear that this:

char a=0,A=0,b=1,B=1,c=2,C=2,d=3,D=3,e=4,E=4,f=5,F=5;

does NOT associate letters with numerical values. When the user hits 'f' for example, you WON'T get the number five instead. These are just simple char variables. This line is useless in your programm.

Would a switch structure fix this problem?

For instance:

char letter;
cout<<"Enter a letter: "<<endl;
cin>>letter;
switch(letter)
{
case 'a': letter=0;
             break;
case 'A': letter=0
             break;
}

Letter is a char variable. Instead of holding a letter of the alphabet that the user entered in, it now holds a single digit integer (which, I believe, a char CAN do). So, perhaps I could send my variable "letter" into the necessary functions to make my program work properly. Is this a viable method?

No, because the line indicates a fundamental misunderstanding of how variables, characters and integers are (un-)related. If you look at the ASCII Table, you will see that the characters which represent the digits are not the same as the integers themselves.

What may not seem as clear is that a variable with an identifier that is a given letter does not have anything to do with the character as a value. If you have a variable A, the variable does not necessarily hold the value 'A', and it cannot be matched to 'A' unless you actually assign that value to it.

No, because the line indicates a fundamental misunderstanding of how variables, characters and integers are (un-)related. If you look at the ASCII Table, you will see that the characters which represent the digits are not the same as the integers themselves.

What may not seem as clear is that a variable with an identifier that is a given letter does not have anything to do with the character as a value. If you have a variable A, the variable does not necessarily hold the value 'A', and it cannot be matched to 'A' unless you actually assign that value to it.

so my switch statement with

char letter;

wouldn't work?

Letter is a char variable. Instead of holding a letter of the alphabet that the user entered in, it now holds a single digit integer (which, I believe, a char CAN do).

Yes, after a fashion, it can. However, it is in the form of a binary number, rather than a character representing the specific digit. As I said in my last post, these are not the same thing. Also, printing such a single-byte number is likely to present a problem, as the standard printing functions assume that the character is encoded in ASCII or something similar.

So, perhaps I could send my variable "letter" into the necessary functions to make my program work properly. Is this a viable method?

Not if they are expecting a character value, no.

so my switch statement with

char letter;

wouldn't work?

Not the way you have it, no, but not because it is a char .

I think I understand what you're getting at now. I'll have to update my code.

Well, it finally works now. Now I have a second question:
-Is there a more efficient way to code out this program? If not, no worries.
-I need to prompt the user to enter in new plane seat coordinates, if he/she picks an already booked seat. What would be the best way to go about doing this?
Here is my updated code:

#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
void ticketchoices();
void displaychartfirst(char firstclass[2][6],int ticketnumselection);
void displaychartbus(char busiclass[5][6],int ticketnumselection);
void displaycharteco(char ecoclass[6][6],int ticketnumselection);
void decision1(char& letter_col, int& num_row, char firstclass[2][6],int ticketnumselection);
void decision2(char& letter_col, int& num_row, char busiclass[5][6],int ticketnumselection);
void decision3(char& letter_col, int& num_row, char ecoclass[6][6],int ticketnumselection);



int main(int argc, char *argv[])
{
char X;
char O;
char firstclass[2][6]={{'O','O','X','O','X','X'},
                       {'O','X','O','X','O','X'}};
char busiclass[5][6]= {{'O','O','X','X','O','X'},
                       {'X','O','X','O','X','X'},
                       {'O','X','O','X','O','O'},
                       {'O','X','O','O','O','X'},
                       {'X','O','O','O','X','X'}};
char ecoclass[6][6]=  {{'O','X','O','X','X','O'},
                       {'X','O','X','X','O','X'},
                       {'O','X','O','X','X','X'},
                       {'O','O','X','O','X','O'},
                       {'O','O','X','X','O','X'},
                       {'O','O','O','O','X','O'}};
int ticketnumselection;
cout<<"Welcome! Please, select a ticket type:"<<endl;
ticketchoices();
cout<<endl;
cout<<"Enter choice: ";
cin>>ticketnumselection;
cout<<endl;
switch(ticketnumselection)
{case 1: cout<<"You've chosen first class."<<endl<<endl;
         break;
 case 2: cout<<"You've chosen business class."<<endl<<endl;
         break;
 case 3: cout<<"You've chosen economy class."<<endl<<endl;
         break;}
displaychartfirst(firstclass,ticketnumselection);
displaychartbus(busiclass,ticketnumselection);
displaycharteco(ecoclass,ticketnumselection);


int num_row;
char letter_col;

cout<<"X: Occupied."<<endl; 
cout<<"O: Available."<<endl<<endl;
cout<<"Enter seat letter: ";
cin>>letter_col;
cout<<"Enter row number: ";
cin>>num_row;
cout<<endl;
if(ticketnumselection==1)
{decision1(letter_col,num_row,firstclass,ticketnumselection);}
if(ticketnumselection==2)
{decision2(letter_col,num_row,busiclass,ticketnumselection);}
if(ticketnumselection==3)
{decision3(letter_col,num_row,ecoclass,ticketnumselection);}


system("PAUSE");
return EXIT_SUCCESS;}


void ticketchoices()
{cout<<"Press 1 to select first class."<<endl;
cout<<"Press 2 to select business class."<<endl;
cout<<"Press 3 to select economy class."<<endl;
return;}



void displaychartfirst(char firstclass[2][6], int ticketnumselection)
{
int r=0,c=0;
if(ticketnumselection==1)
 {cout<<"       A  B  C  D  E  F"<<endl<<endl;
  for(r=0;r<2;++r)
   {  if(r>=1)
      {cout<<endl;}
   cout<<"Row "<<r+1<<"  ";
    for(c=0;c<6;++c)
     {cout<<firstclass[r][c]<<"  ";}}}
     cout<<endl;
     return;}

void displaychartbus(char busiclass[5][6],int ticketnumselection)
{
int r=0,c=0;
if(ticketnumselection==2)
 {cout<<"       A  B  C  D  E  F"<<endl<<endl;
  for(r=0;r<5;++r)
   { if(r>=1)
     {cout<<endl;}
   cout<<"Row "<<2+r+1<<"  ";
    for(c=0;c<6;++c)
     {cout<<busiclass[r][c]<<"  ";}}}
     cout<<endl;
     return;}

void displaycharteco(char ecoclass[6][6],int ticketnumselection)
{
int r=0,c=0;
if(ticketnumselection==3)
 {cout<<"       A  B  C  D  E  F"<<endl<<endl;
  for(r=0;r<6;++r)
   {  if(r>=1)
      {cout<<endl;}
   cout<<"Row "<<7+r+1<<"  ";
    for(c=0;c<6;++c)
     {cout<<ecoclass[r][c]<<"  ";}}}
     cout<<endl;
     return;}

void decision1(char& letter_col,int& num_row,char firstclass[2][6],int ticketnumselection)
{if(ticketnumselection==1)
switch(letter_col)
{case 'a' : letter_col=0;
            break;
 case 'A' : letter_col=0;
            break;
 case 'b' : letter_col=1;
            break;
 case 'B' : letter_col=1;
            break;
 case 'c' : letter_col=2;
            break;
 case 'C' : letter_col=2;
            break;
 case 'd' : letter_col=3;
            break;
 case 'D' : letter_col=3;
            break;
 case 'e' : letter_col=4;
            break;
 case 'E' : letter_col=4;
            break;
 case 'f' : letter_col=5;
            break;
 case 'F' : letter_col=5;
            break;}
num_row=num_row-1;
char x='X';                         
firstclass[static_cast<int>(num_row)][static_cast<int>(letter_col)]=x;
displaychartfirst(firstclass,ticketnumselection);
cout<<endl;
cout<<"Seat booked successfully. Thank you and enjoy your flight!"<<endl<<endl;}

void decision2(char& letter_col,int& num_row,char busiclass[5][6],int ticketnumselection)
{if(ticketnumselection==2)
switch(letter_col)
{case 'a' : letter_col=0;
            break;
 case 'A' : letter_col=0;
            break;
 case 'b' : letter_col=1;
            break;
 case 'B' : letter_col=1;
            break;
 case 'c' : letter_col=2;
            break;
 case 'C' : letter_col=2;
            break;
 case 'd' : letter_col=3;
            break;
 case 'D' : letter_col=3;
            break;
 case 'e' : letter_col=4;
            break;
 case 'E' : letter_col=4;
            break;
 case 'f' : letter_col=5;
            break;
 case 'F' : letter_col=5;
            break;}
num_row=num_row-3;
char x='X';
busiclass[static_cast<int>(num_row)][static_cast<int>(letter_col)]=x;
displaychartbus(busiclass,ticketnumselection);
cout<<endl;
cout<<"Seat booked successfully. Thank you and enjoy your flight!"<<endl<<endl;}

void decision3(char& letter_col,int& num_row,char ecoclass[6][6],int ticketnumselection)
{if(ticketnumselection==3)
switch(letter_col)
{case 'a' : letter_col=0;
            break;
 case 'A' : letter_col=0;
            break;
 case 'b' : letter_col=1;
            break;
 case 'B' : letter_col=1;
            break;
 case 'c' : letter_col=2;
            break;
 case 'C' : letter_col=2;
            break;
 case 'd' : letter_col=3;
            break;
 case 'D' : letter_col=3;
            break;
 case 'e' : letter_col=4;
            break;
 case 'E' : letter_col=4;
            break;
 case 'f' : letter_col=5;
            break;
 case 'F' : letter_col=5;
            break;}
num_row=num_row-8;
char x='X';
ecoclass[static_cast<int>(num_row)][static_cast<int>(letter_col)]=x;
displaycharteco(ecoclass,ticketnumselection);
cout<<endl;
cout<<"Seat booked successfully. Thank you and enjoy your flight!"<<endl<<endl;}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.