C++/A little more help ??
Expert: Zlatko - 4/8/2010
Questionokay ..here is my revised code ..It works great.But is there another way to write it aside from using low/high?? ( obviously this is my Homewrok and weve yet to use this? Also I know what atoi does, but again we havent used it . is it possible to ommit it from the program or will we have input errors. Sorry to be a pain ..Thank you !
#include<iostream>
#include<cctype>
#include<iomanip>
#include<string>
using namespace std;
void initialize(char form[][6]);
void getData(char& ticketType, int& row,
char& column);
void printForm(char form[][6], int row, char column);
char inputChar(const char* prompt, const char* valid)
{
char result;
do
{
cout << prompt << endl;
std::string text;
//std::getline(cin, text);
cin >> text;
result = text[0];
result = static_cast<char>(toupper(result));
if(strchr(valid, result) == NULL)
cout << "Incorrect entry."; // wrong!
else
break; //right!
} while(true);
return result;
}
int inputInt(const char* prompt, int lowValid, int highValid)
{
int result;
do
{
cout << prompt << endl;
std::string text;
//std::getline(cin, text);
cin >> text;
result = atoi(text.c_str());
if(result < lowValid || result > highValid)
cout << "\nIncorrect entry. "; //incorrect !
else
break; //success
} while (true);
return result;
}
int main()
{
char ch, ticketType, column;
int row;
char form[13][6];
initialize(form);
ch = inputChar("This program allows the user to assign seats to an airplane."
"\n\nDo you wish to begin the seating assignments? Y/y for yes or N/n for no.", "YN");
while(ch == 'Y')
{
getData(ticketType, row, column);
printForm(form, row, column);
ch = inputChar("Continue with the seating assignments? Y/y for yes or N/n for no.", "YN");
}
return 0;
}
void initialize( char form[][6])
{
for(int i=0 ;i < 13 ;i++)
for(int j=0 ;j<6 ;j++)
form[i][j]='*';
}
void getData(char& ticketType, int& row, char& column)
{
cout << "\nThe airplane has 13 rows, with six seats in each row. " << endl;
do
{
cout << "Enter ticket type,\n"
<< "F/f for first class, \n"
<< "B/b for business class,\n"
<< "E/e for economy class:" << endl;
cin >> ticketType;
ticketType = static_cast<char>(toupper(ticketType));
if(ticketType != 'F' && ticketType != 'B' && ticketType != 'E')
cout << "Invalid ticket type." << endl;
else
break;
} while (true);
int rowLow;
int rowHigh;
switch(ticketType)
{
case 'F':
cout << "Row 1 and 2 are first class,\n" ;
rowLow = 1;
rowHigh = 2;
break;
case 'B':
cout << "Row 3 throuh 7 are business class,\n";
rowLow = 3;
rowHigh = 7;
break;
case 'E':
cout << "Row 8 through 13 are economy class." << endl;
rowLow = 8;
rowHigh = 13;
break;
}// end switch
row = inputInt("Enter the row number you want to sit: ", rowLow, rowHigh);
column = inputChar("Enter the seat number (from A to F). ", "ABCDEF");
}
void printForm(char form[][6], int row, char column)
{
int i, j;
while(form[row-1][static_cast<int>(column-'A')]=='X')
{
cout << "This seat already assigned. Choose another seat: " << endl;
cin >> column;
column = static_cast<char>(toupper(column));
}
form[ row-1 ] [static_cast<int>(column)-'A']= 'X';
do
{
cout << "* indicates that the seat is available; " << endl;
cout << "X indicates that the seat is occupied. " << endl;
cout << setw(12) << "A" << setw(6) << "B" << setw(6) << "C"
<< setw(6) << "D" << setw(6) << "E" << setw(6) << "F" << endl;
for(i = 0; i < 13; i++)
{
cout << left << setw(3) << "Row " << setw(2)<< i+1 ;
for(j = 0; j < 6; j++)
{
cout << right << setw(6) << form [i][j];
}
cout << endl;
}
if(column != 'A' && column != 'B' && column != 'C' &&
column != 'D' && column != 'E' && column != 'F')
cout << "Invalid Row !" << endl;
else
break;
} while(true);
}
AnswerHello Jason.
The atoi function converts a string of digit characters to an integer. So the string "123" becomes the integer 123. It is a simple and well known function and you shouldn't be worried about using it even if you haven't covered it in class. I'll bet it is somewhere in your textbook.
If you want to get an integer from the user, then this is a good way for a beginner. Using
int x;
cin >> x
can lead to problems if the user inputs something other than digits. I think you saw that in your original question to me when you wrote about the program having an infinite loop.
I assume the low/high you are asking about is the rowLow and rowHigh. There you are just choosing parameters in the switch block, and then passing the parameters to the inputInt function. Since you are already using functions and parameters in your program, the rowLow and rowHigh technique is not such a stretch.
I suppose you could write it as
switch(ticketType)
{
case 'F':
cout << "Row 1 and 2 are first class,\n" ;
row = inputInt("Enter the row number you want to sit: ", 1, 2);
break;
case 'B':
cout << "Row 3 throuh 7 are business class,\n";
row = inputInt("Enter the row number you want to sit: ", 3, 7);
break;
case 'E':
cout << "Row 8 through 13 are economy class." << endl;
row = inputInt("Enter the row number you want to sit: ", 8, 13);
break;
}// end switch