hi all,
here with I attach a programe written from using C++ language. I am new to C++. problem is I want to be a good programmer and I think I still do mistakes in witting programes.
what I want is check my code and criticize my mistakes. tell me what I want to improve.
programme was written using a 2d array. I didnt use Classes since this scope was enogh for the requirment.
here is the code
// test2.cpp : Defines the entry point for the console application.
//
#include "stdlib.h"
#include "stdafx.h"
#include <iostream>
using namespace std;
const int rows=13;
const int coloums=6;
const int firstclass_lastrow=2;
const int secondclass_lasstrow=7;
char **updateSeats(char **seats, int row , int coloum);
void printSeatsAvailable(char **seats);
char **createSeats();
void getUserPreference(char **seats);
void gotoMainMenu(char **seats2);
bool checkSeat(char **seats, int row_number , int colum_number);
bool seatAvailable(char **seats, int start , int end);
void welcomeMessage();
bool validateUserpreference(int row, int colum, char classtype, char smokPref);
char **updateSeats(char **seats, int row , int coloum)
{
//this function wil update the seat , updates 2D array wil be passe into the ......function
seats[row-1][coloum-1]='X';
cout <<"\n"; cout <<"\n";
return seats;
}
void printSeatsAvailable(char **seats)
{
//input : two dimentional array of seats
//output : print the current state of the seat availability
for (int i=0; i<rows; i++)
{
cout <<"\n\t";
for (int j=0; j<coloums; j++)
{
cout << seats[i][j]<<" ";
}
}
}
char **createSeats()
{
// this function wil be run at the begining
// all seats are set to available
char **seats=new char*[rows];
for (int i=0; i<rows; i++)
{
seats[i] = new char[10];
for (int j=0; j<coloums; j++)
seats[i][j]='*';
}
return seats;
}
bool validateUserpreference(int row, int colum, char classtype, char smokPref)
{
//inputs : class category, smoking perfernce
// output: identify whether seat is selected from the right class
if(classtype=='F')
{
if((row -1)<firstclass_lastrow && (colum-1) <coloums)
{
return true;
}
else
{
cout <<" Invalid user input";
cout << "\n First class belongs first and second row\n";
return false;
}
}
else if(classtype=='E')
{//validateUserpreference(row, colum, 'E',smoking_pref )
if(smokPref=='S')
{
if(secondclass_lasstrow<(row -1)&& (row -1)<rows && (colum-1) <coloums)
{
return true;
}
else
{
cout <<" Invalid user input";
cout << "\n This row is not belong to economy smoking class\n";
return false;
}
}
else if(smokPref=='N')
{
if(firstclass_lastrow<row && row <secondclass_lasstrow && colum-1 <coloums)
{
return true;
}
else
{
cout <<" Invalid user input";
cout << "\n This row is not belong to economy non smoking class\n";
return false;
}
}
}else
{
return false;
}
}
void getUserPreference(char **seats)
{ //inputs : get users prefence for selecting a seat
char **seats2;
char class_type, smoking_pref;
int colum, row;
bool val;//hold validated value for the seat
cout << "\n\n\t\t\t\t Please select your class\n\n\n";
cout << "\tFor first class enter 'F'\n";
cout << "\tFor economy class enter 'E'\n";
cout << "\tTo exit from the programe enter 'O'\n";
cout << "\tEnter your preference :";
cin >> class_type;
class_type=toupper(class_type); // convert all user inputs to upper case.
if(class_type=='F')
{
//do stuff here
// here user can set check available seats
// checkAvailableSeats(array_check)
cout << "\n\tEnter the preference seat\n";
cout << "\tRow :";
cin >> row;
cout << "\n\tColoumn :";
cin >> colum;
val =validateUserpreference(row, colum, 'F','N' );
if(val==true){
// check_seat_available(class_type, smoking_pref, colum, row);
if(checkSeat(seats, row , colum)){
seats2=updateSeats(seats, row, colum);
printSeatsAvailable(seats2);
getUserPreference(seats2);
}else
{
cout << "\n\n\tAlready occupied this seat! \n";
cout << "\tTry Again !!";
//system("cls");
getUserPreference(seats);
}
}else{
getUserPreference(seats);
}
cin.get();
}
else if (class_type=='E')
{
//check preferece of smoking or nun somoking
cout << "\t\tFor smoking row enter 'S'\n";
cout << "\t\tFor non-smoking row enter 'N'\n";
cout << "\n\t";
cin >> smoking_pref;
smoking_pref=toupper(smoking_pref);
//// here user can set check available seats
// checkAvailableSeats
cout << "\n\tEnter the preference seat\n";
cout << "\t Row :";
cin >> row;
cout << "\t Coloumn :";
cin >> colum;
val =validateUserpreference(row, colum, 'E',smoking_pref );
if(val==true){
// check_seat_available(class_type, smoking_pref, colum, row);
if(checkSeat(seats, row , colum)){
seats2=updateSeats(seats, row, colum);
printSeatsAvailable(seats2);
getUserPreference(seats2);
}else
{
cout << "\n\n\tAlready occupied this seat! \n";
cout << "\tTry Again !!";
//system("cls");
getUserPreference(seats);
}
}else{
getUserPreference(seats);
}
}
else if(class_type=='O')
{
welcomeMessage();
//if user enter invalid letter , give chance to re-enter data.
}else{
cout <<"\n\t Invalid character!! ... try again!";
getUserPreference(seats);
}
}
void gotoMainMenu(char **seats)
{
char input;
cout << "\n\n\t****************************************************\n";
cout << "\t#################welcome to main menu###############\n\n \n";
cout << "\t\tenter 1 for buy a seat\n";
cout << "\t\t";
cout << "\n\n\n\t##################################################### \n";
cout << "\t*****************************************************\n";
cout << "\tEnter your preference :";
cin >> input;
if(input=='1')
{
system("cls");
getUserPreference(seats);
}
else
{ system("cls");
gotoMainMenu(seats);
}
}
bool checkSeat(char **seats, int row_number , int colum_number)
{
//this function wil check a particular seat is availablle or not.
if (seats[row_number-1][colum_number-1]=='*')
{
return true;
}
else
{
return false;
}
}
bool seatAvailable(char **seats, int start , int end)
{
// this function will get seat category and check whether free seats are available.
for(int i=start; i<end; i++)
{
for (int j=0; j<7; j++){
if (seats[i][j]=='*')
{
return true;
}
else
{
return false;
}
}
}
}
void welcomeMessage()
{
char ans;//this variable holds user answere
do{
cout <<"\n\n";
cout <<"\t ########### Welcome To Jet Airways ##############"<<"\n" ;
cout <<" *********** ********** "<< "\n\n\n";
cout <<" ******* Airplane Seating Assingment ****** "<< "\n";
cout <<" * \t * "<< "\n";
cout <<" * \t * "<< "\n";
cout <<" * \t * "<< "\n";
cout <<"\n\n";
cout <<"\n\n";
cout<<" ~*~*~*~*~*~*~*~*~*~*~*~ About Us ~*~*~*~*~*~*~*~*~*~ "<<endl<<endl;
cout<<" WORK WITH PASSION "<<endl;
cout<<" < E-mail : Jetliner Airways@ rocketmail.com > "<<endl;
cout<<" < Website: www.jetliner.com > "<<endl;
cout <<"\n\n";
cout <<"\n\n";
cout <<"\t\tDo you want to continue? (y/n) :";
cin >> ans;
ans=toupper(ans);//changing input into uppercase
}while(ans!='Y' && ans!='N');
if(ans =='Y')
{
system("cls");
char **seats=createSeats();// initially all seats are free
gotoMainMenu(seats);
}else
{
exit(1);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
welcomeMessage();
cin.get();
return 0;
}
thx inadvance,
menuka