i'm trying to set up a simple flight booking system program, but my second bit of cin code is not calling for input when i run the program. Unlike the initial cin that requires your name initially. the program just runs and returns 0. I'm a beginner at c++ and i know this is a simple fix so please be understanding . Thank you any guidance will be greatly appreciated.
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
int name;
int Seatnumber;
int optionnumber = 1-5 ;
std::string out_string;
std::stringstream ss;
ss << optionnumber;
out_string = ss.str();
cout << "Welcome to the COS1511 Flight Booking System" "\n" << endl;
cout << "Enter full name : " << endl;
cin >> name ; "\n";
cout << "\n" "The Available travel times for flights are:" << endl;
cout << " Depart Arrive" << endl;
cout << "1. 7.00 9.30" << endl;
cout << "2. 9.00 11.30" << endl;
cout << "3. 11.00 13.30" << endl;
cout << "4. 13.00 15.30" << endl;
cout << "5. 15.00 17.30" << endl;
cout << "Choose the time by entering the option number from the displayed list : " << endl;
cin >> optionnumber ;
if (optionnumber == 1-5){
cout << "\n" "The available seats for are as follows " << endl;
}
else
cout << "Incorrect option! Please Choose from 1-5 " << endl;
cout << "First Class(1920)" << endl;
cout << "|A1||A2||A3|----|A4||A5||A6|" << endl;
cout << "|B1||B2||B3|----|B4||B5||B6|" << endl;
cout << "|C1||C2||C3|----|C4||C5||C6|" << endl;
cout << "|D1||D2||D3|----|D4||D5||D6|" << endl;
cout << "| Economy Class(1600)" << endl;
cout << "|E1||E2||E3|----|E4||E5||E6|" << endl;
cout << "|F1||F2||F3|----|F4||F5||F6|" << endl;
cout << "|G1||G2||G3|----|G4||G5||G6|" << endl;
cout << "|H1||H2||H3|----|H4||H5||H6|" << endl;
cout << "|I1||I2|" << endl;
cout << "Please Key in a seat number to choose a seat(eg: A2)" << endl;
cin >> Seatnumber;
}
prompt the user to enter their name.
Then display a menu showing the available times for the flight.
the user can choose a preferred departure time(option 1-5)
the option selected should be validated for 1-5
if the user entered the correct time a seating arrangement for that particular flight time should be displayed to the next user for the user to choose a seat.
Warning
int optionnumber = 1-5 ;
does
int optionnumber = -4 ;
and
if (optionnumber == 1-5){
does
if (optionnumber == -4){
but you wanted if ((optionnumber >= 1) && (optionnumber <= 5))
if the user entered the correct time a seating arrangement for that particular flight time should be displayed to the next user for the user to choose a seat.
No, whatever the result of the test above you continue and write "First Class(1920)" etc so even when the choice is invalid
in
cin >> name ; "\n";
what did you expect about the "\n" ?
I encourage you to check the read success, currently if the user does not enter an integer you do not know that
But are you sure the name must be an integer ? Probably it must be s string
out_string is unused, it can be removed
Visibly Seatnumber is not an int but a string (A1 ...)
you probably want to loop until a valid time is enter, also fixing the other problems a solution can be :
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
string Seatnumber;
int optionnumber;
cout << "Welcome to the COS1511 Flight Booking System" "\n" << endl;
cout << "Enter full name : " << endl;
if (!(cin >> name))
// EOF (input from a file)
return -1;
cout << "\n" "The Available travel times for flights are:" << endl;
cout << " Depart Arrive" << endl;
cout << "1. 7.00 9.30" << endl;
cout << "2. 9.00 11.30" << endl;
cout << "3. 11.00 13.30" << endl;
cout << "4. 13.00 15.30" << endl;
cout << "5. 15.00 17.30" << endl;
cout << "Choose the time by entering the option number from the displayed list : " << endl;
for (;;) {
if (!(cin >> optionnumber)) {
// not an int
cin.clear(); // clear error
string s;
// flush invalid input
if (!(cin >> s))
// EOF (input from a file)
return -1;
}
else if ((optionnumber >= 1) && (optionnumber <= 5))
// valid choice
break;
cout << "Incorrect option! Please Choose from 1-5 " << endl;
}
cout << "\n" "The available seats for are as follows " << endl;
cout << "First Class(1920)" << endl;
cout << "|A1||A2||A3|----|A4||A5||A6|" << endl;
cout << "|B1||B2||B3|----|B4||B5||B6|" << endl;
cout << "|C1||C2||C3|----|C4||C5||C6|" << endl;
cout << "|D1||D2||D3|----|D4||D5||D6|" << endl;
cout << "| Economy Class(1600)" << endl;
cout << "|E1||E2||E3|----|E4||E5||E6|" << endl;
cout << "|F1||F2||F3|----|F4||F5||F6|" << endl;
cout << "|G1||G2||G3|----|G4||G5||G6|" << endl;
cout << "|H1||H2||H3|----|H4||H5||H6|" << endl;
cout << "|I1||I2|" << endl;
cout << "Please Key in a seat number to choose a seat(eg: A2)" << endl;
cin >> Seatnumber;
return 0;
}
Compilation and execution :
pi#raspberrypi:/tmp $ g++ -pedantic -Wextra -Wall cc.cc
pi#raspberrypi:/tmp $ ./a.out
Welcome to the COS1511 Flight Booking System
Enter full name :
bruno
The Available travel times for flights are:
Depart Arrive
1. 7.00 9.30
2. 9.00 11.30
3. 11.00 13.30
4. 13.00 15.30
5. 15.00 17.30
Choose the time by entering the option number from the displayed list :
aze
Incorrect option! Please Choose from 1-5
7
Incorrect option! Please Choose from 1-5
2
The available seats for are as follows
First Class(1920)
|A1||A2||A3|----|A4||A5||A6|
|B1||B2||B3|----|B4||B5||B6|
|C1||C2||C3|----|C4||C5||C6|
|D1||D2||D3|----|D4||D5||D6|
| Economy Class(1600)
|E1||E2||E3|----|E4||E5||E6|
|F1||F2||F3|----|F4||F5||F6|
|G1||G2||G3|----|G4||G5||G6|
|H1||H2||H3|----|H4||H5||H6|
|I1||I2|
Please Key in a seat number to choose a seat(eg: A2)
qsd
pi#raspberrypi:/tmp $
Note entering the name with cin >> name does not allow it to contain several names separated by a space, to allow composed name getline can be used
Related
I have written this code using array of structures in C++ and the output was correct, but when I included the pointers, the output is not correct and missing a lot of, what is the reason of this?
This is the code using pointers:
#include <iostream>
using namespace std;
struct Course {
string CourseCode;
string CourseName;
string LecturerName;
};
struct Student {
string StudentID;
string StudentName;
string Nickname;
Course info;
};
void menu(void) {
cout << "=================================" << endl;
cout << " MENU " << endl;
cout << "=================================" << endl;
cout << " 1- Add new students" << endl;
cout << " 2- Display student list" << endl;
cout << " 3- Add new course" << endl;
cout << " 4- Display Course Offered" << endl;
cout << " 5- Exit Program" << endl;
cout << "=================================" << endl << endl;
}
int main()
{
Student *ptr[2] ;
Student details[2];
ptr[2] = &details[2];
int m, option, courNo;
menu();
cout << "Choose an option from the menu: ";
cin >> option;
while (option != 5)
{
if (option == 1) {
// cout << "\nHow many students do you want to enter? ";
// cin >> stuNo;
for (m = 0; m < 2; m++) {
cout << "\nEnter student " << m + 1 << " details: " << endl;
cout << "ID: ";
cin >> ptr[m]->StudentID;
cout << "Name: ";
cin >> ptr[m]->StudentName;
cout << "Nickname: ";
cin >> ptr[m]->Nickname;
}
}
else if (option == 2) {
cout << "\nThe students details you entered are:" << endl;
for (m = 0; m < 2; m++) {
cout << "\nStudent " << m + 1 << ": " << endl;
cout << "ID: " << ptr[m]->StudentID << endl;
cout << "Name: " << ptr[m]->StudentName << endl;
cout << "Nickname: " << ptr[m]->Nickname << endl;
}
}
else if (option == 3) {
cout << "\nHow many courses do you want to enter? ";
cin >> courNo;
for (m = 0; m < courNo; m++) {
cout << "\nEnter course " << m + 1 << " details: " << endl;
cout << "Course Code: ";
cin >> ptr[m]->info.CourseCode;
cout << "Course Name: ";
cin >> ptr[m]->info.CourseName;
cout << "Lecturer Name: ";
cin >> ptr[m]->info.LecturerName;
}
}
else if (option == 4) {
cout << "\nThe courses details you entered are:" << endl;
for (m = 0; m < 2; m++) {
cout << "\nCourse " << m + 1 << ": " << endl;
cout << "Course Code: " << ptr[m]->info.CourseCode << endl;
cout << "Course Name: " << ptr[m]->info.CourseName << endl;
cout << "Lecturer Name: " << ptr[m]->info.LecturerName << endl;
}
}
else
cout << "Invalid number! Please re-enter a choice from the menu." << endl;
cout << "=====================================================" << endl;
cout << "\nChoose an option from the menu: ";
cin >> option;
}
if (option == 5)
cout << "End of program!" << endl;
}
This is the output of the program, after I enter the code it was terminated:
=================================
MENU
=================================
1- Add new students
2- Display student list
3- Add new course
4- Display Course Offered
5- Exit Program
=================================
Choose an option from the menu: 3
How many courses do you want to enter? 2
Enter course 1 details:
Course Code: 123
This is the output without using pointers, it was like this which is the output that I'm looking for:
=================================
MENU
=================================
1- Add new students
2- Display student list
3- Add new course
4- Display Course Offered
5- Exit Program
=================================
Choose an option from the menu: 3
How many courses do you want to enter? 2
Enter course 1 details:
Course Code: 0000
Course Name: course1
Lecturer Name: lecturer1
Enter course 2 details:
Course Code: 1111
Course Name: course2
Lecturer Name: lecturer2
=====================================================
Choose an option from the menu: 5
End of program!
I'm fairly new to c++, I have been given an assignment to do a fairly basic program that users can use to buy tickets but I am having some issues with the calculation.
This is my code so far.
#include <iostream>
using namespace std;
int main()
{
double type_ticket, num_tickets, price1, price2, price3, total_price, decision;
cout << "Welcome to the ticket kiosk.";
cout << "\n";
cout << "\n";
cout << "1. VVIP - RM 200";
cout << "\n";
cout << "2. VIP - RM 150";
cout << "\n";
cout << "3. Normal - RM 100" << endl;
cout << "\n";
do
{
cout << "Please select the category of ticket you would like to purchase: ";
cin >> type_ticket;
cout << "\n";
if (type_ticket == 1)
{
cout << "How many would you like: ";
cin >> num_tickets;
cout << "\n";
price1 = num_tickets * 200;
cout << "The price is: RM " << price1 << endl;
cout << "\n";
cout << "\n";
cout << "1. YES" << endl;
cout << "2. NO" << endl;
cout << "\n";
cout << "Would you like to continue purchasing more tickets: ";
cin >> decision;
cout << "\n";
}
else if (type_ticket == 2)
{
cout << "How many would you like: ";
cin >> num_tickets;
cout << "\n";
price2 = num_tickets * 150;
cout << "The price is: RM " << price2 << endl;
cout << "\n";
cout << "\n";
cout << "1. YES" << endl;
cout << "2. NO" << endl;
cout << "\n";
cout << "Would you like to continue purchasing more tickets: ";
cin >> decision;
cout << "\n";
}
else if (type_ticket == 3)
{
cout << "How many would you like: ";
cin >> num_tickets;
cout << "\n";
price3 = num_tickets * 100;
cout << "The price is: RM " << price3 << endl;
cout << "\n";
cout << "\n";
cout << "1. YES" << endl;
cout << "2. NO" << endl;
cout << "\n";
cout << "Would you like to continue purchasing more tickets: ";
cin >> decision;
cout << "\n";
}
else
{
cout << "You have entered an invalid input, please try again. " << endl;
cout << "\n";
}
}
while (decision == 1);
total_price = price1 + price2 + price3;
cout << "The grand total is: RM " << total_price << endl;
cout << "\n";
cout << "Thank you for using this service today, we hope you enjoy the show." << endl;
cout << "\n";
}
The problem that I am having is when the user buys tickets from vvip and/or vip, the calculation for total_price is not being done right. When a price 3 has been entered however, the calculation works fine.
User buys vvip and/or vip = calculation not done right.
User buys normal and vvip and/or vip = calculation done right.
Any help would be very much appreciated.
FYI, this code is not yet complete, but for now, this is what I have.
You seem not to initialize priceN (where N is one of 1, 2, 3) variables before calculation of:
total_price = price1 + price2 + price3;
in case of only one type of the ticket, so the result is unpredictable because variables contain garbage.
You should start with :
double price1 = 0;
double price2 = 0;
double price3 = 0;
Just needed some help with my program.
I keep getting an error with my if statement.
Could you help me out please.
This is the code I have written.
#include <iostream>
#include <cstdlib>
#include <iostream>
#include <string>
char carChoice;
int random_integer = rand();
int pricePerDay;
int daysOfHire;
int totalCost;
int age;
int telephoneNumber[30];
char name[30];
char address[30];
char response [4];
using namespace std;
int main()
{
cout << "Royal Rentals" << endl;
cout << "---------------------------------------------"<<"\n" <<endl;
cout << "Current Stock: " <<endl;
cout << "A - Lamborghini Aventador" <<endl;
cout << "B - Lamborghini Huracan" <<endl;
cout << "C - Mercedes Benz AMG GT S" <<endl;
cout << "D - Audi R8 V10" <<endl;
cout << "Which car would you like to rent (A, B, C or D)" << "\n" << endl;
cin >> carChoice;
cout << "How many days would you like to hire the car for" << "\n" << endl;
cin >> daysOfHire;
cout << "How old are you?" << "\n" << endl;
cin >> age;
if (age < 21)
{
cout << "Sorry but you have to over the age of 21 to hire our super cars" << "\n" << endl;
cout << "Please close the program to hire a car suitable for your age" << "\n" << endl;
exit (0);
}
cout << "What is your name?" << "\n" << endl;
cin >> name;
cout << "Have you got a full drivers license?" "\n" << endl;
cin >> response;
if (response == 'Y' //not "Y" but only one 'Y')
{
cout << "blah"<< "\n" <<endl;
}
else if (response == 'N')
{
cout << "blah"<< "\n" << endl;
}
cout << "what is your address?" << "\n" << endl;
cin >> address;
cout << "what is your telephone number?"<< "\n" << endl;
cin >> telephoneNumber;
if (carChoice == 'a')
{
totalCost = daysOfHire * 685;
cout << "You have chosen the following car for hire: Lamborghini Aventador" << endl;
cout << "The price per day is 685.00 GBP" << endl;
cout << "The Total Cost is: " << totalCost << endl;
cout << "Invoice Number; " << random_integer << endl;
}
else if (carChoice == 'b')
{
totalCost = daysOfHire * 585;
cout << "You have chosen the following car for hire: Lamborghini Huracan" << endl;
cout << "The price per day is �585.00 GBP" << endl;
cout << "The Total Cost is: " << totalCost << endl;
cout << "Invoice Number; " << random_integer << endl;
}
else if (carChoice == 'c')
{
totalCost = daysOfHire * 485;
cout << "You have chosen the following car for hire: Mercedes Benz AMG GT S" << endl;
cout << "The price per day is �485.00 GBP" << endl;
cout << "The Total Cost is: " << totalCost << endl;
cout << "Invoice Number; " << random_integer << endl;
}
else if (carChoice == 'd')
{
totalCost = daysOfHire * 445;
cout << "You have chosen the following car for hire: Audi R8 V10" << endl;
cout << "The price per day is �445.00 GBP" << endl;
cout << "The Total Cost is: " << totalCost << endl;
cout << "Invoice Number; " << random_integer << endl;
}
else
{
cout << "You have entered an invalid response" << endl;
}
}
The statement is to verify if you have a valid driving license, if you don't then the program should display a message and close. If they do then it should continue so they can enter the rest of their details.
What am I missing from it and could some correct my if statement.
Many thanks,
Irbaaz
if (response == 'Y')
Response is an array, you cannot compare an array with an integer. If you want to check that the whole string consists of one 'Y' symbol, you should check
Strlen(response) == 1 && response[0] == 'Y'
I am having trouble getting my while loop to run. I am a very beginner coder and I have made many attempts with no success to make this work. I need help PLEASE!! Please be very specific and with laymen terms with your help since I am new to this.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string mystr1;
string mystr2;
cout << "Please provide your first and last name" << endl;
getline (cin, mystr1);
cout << endl;
cout << "Please provide your shipping address" << endl;
getline (cin, mystr2);
cout << endl;
cout << "Hello " << mystr1 << " and welcome to Faulk Couture
Handbags Boutique" << endl; // prints Hello and welcome to Faulk
Couture Handbags Boutique
cout << endl;
cout << "We have a variety of specialty and fashionable handbags to
select from. Please see below for the available products and their
descriptions." << endl;
cout << endl;
cout << "Product 1: Crosby Carryall in black priced at $395. This
sophisticated and spacious Crosby Carryall is a work-to-weekend favorite
and is finished with " << endl;
cout << "bound leather edges, a detachable leather strap and petite
brass turnlocks securing its two zippered compartments."<< endl;
cout << endl;
cout << "Product 2: Prairie satchel with chain nude priced at $450.
Crafted in lightweight pebble leather with a bit of sheen, this
gracefully curved shape distills" <<endl;
cout << "the satchel to its purest form. The simple design is finished
with a slender strap and an elegant chain detail that detaches for a
different look." << endl;
cout << endl;
cout << "Product 3: Faulk Swagger 20 brown priced at $325. This
Statement belting with double-turnlock hardware is one of our most
popular designs with a little bit of “swagger.” "<< endl;
cout << "Named for a bold, brass-trimmed Bonnie Cashin design from 1967,
this very modern carryall in refined pebble leather comes finished with
a detachable strap for crossbody wear." << endl;
cout << endl;
cout << "Product 4: Zip top tote in brown priced at $285. This
sophisticated and light weight in signature canvas with hand-finished
leather trim, this aptly named tote is made for one-the-go ease." <<
endl;
cout << "A modern, flared shape and oversized strap anchors add playful
proportions to its spacious, brightly lined design." << endl;
cout << endl;
cout << "Product 5: Wristlet 24 priced at $175. This striking, feminine
design in polished pebble leather has space enough for a tablet and an
elegant chain that converts it from wristlet to top handle." << endl;
cout << "A dog-leash clip on the strap and an embossed hangtag charm
finish it with signature Faulk Couture Style." << endl;
cout << endl;
double cost, total, amount;
int product;
cout << "Please enter the product number for your bag choice" << endl;
cin >> product;
cout <<"The respective price for this bag is: " << endl;
cin>>cost;
cout<<"Please enter the quantity you would like to purchase for this
bag choice" << endl;
cin>>amount;
total = cost*amount;
cout <<"Your total purchase price for " <<amount<< " qty of product
number " <<product<< " is " <<total<<"."<< endl;
int choice=1;
while (choice==1);
{
cout << "To purchase another bag, please enter 1 (anything else to
quit)" << endl;
cin >> choice;
cout << "Please enter the product number for your next bag choice" <<
endl;
cin >> product;
cout << "The respective price for this bag is: " << endl;
cin >> cost;
cout << "Please enter the quantity you would like to purchase for this
bag choice" << endl;
cin >> amount;
total = cost*amount;
cout <<"Your total purchase price for " <<amount<< " qty of product
number " <<product<< " is " <<total<<"."<< endl;
}
cout << endl;
return 0;
}
You're ending your while loop with a semicolon that's why it isn't entering the while loop
Here is the while loop without the semicolon
while (choice==1) {
cout << "To purchase another bag, please enter 1 (anything else to
quit)" << endl;
cin >> choice;
cout << "Please enter the product number for your next bag choice" <<
endl;
cin >> product;
cout << "The respective price for this bag is: " << endl;
cin >> cost;
cout << "Please enter the quantity you would like to purchase for this
bag choice" << endl;
cin >> amount;
total = cost*amount;
cout <<"Your total purchase price for " <<amount<< " qty of product
number " <<product<< " is " <<total<<"."<< endl;
}
Okay so as the title said its refusing to execute the stuff right under the "do" function even though as far as i can tell all the parameters for a repeat have been fulfilled. So far what i get when i run the program is something along the lines of...
"Would you like to search another name?
Please enter Y for yes and n for no:"
looping over and over when i press y
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cstdlib>
using namespace std;
int main()
{
vector <string> vName, vID, vClass;
string sName, sID, sClass, sSearch, cQuestion;
int iSize, iStudent;
// Display initial vector size
iSize = vName.size();
cout << "Student list starts with the size:" << iSize << endl;
// Get size of list from user
cout << "How many students would you like to add?" << endl;
cin >> iStudent;
cin.ignore();
// Get names, ids, and classes
for (int i = 0; i < iStudent; i++)
{
cout << "Student" << i + 1 << ":\n";
cout << "Please enter the student name: ";
getline(cin, sName);
vName.push_back(sName);
cout << "Enter ID number ";
getline(cin, sID);
vID.push_back(sID);
cout << "Enter class name ";
getline(cin, sClass);
vClass.push_back(sClass);
}
// Display header
cout << "The list of students has the size of: " << iStudent << endl;
cout << "The Student List" << endl;
cout << "\n";
cout << "Name:" << setw(30) << "ID:" << setw(38) << "Enrolled Class : " << endl;
cout << "--------------------------------------------------------------------------";
cout << "\n";
// for loop for displying list
for (int x = 0; x < vName.size() && vID.size() && vClass.size(); x++)
{
cout << vName[x] << "\t \t \t" << vID[x] << "\t \t \t" << vClass[x] << endl;
}
// Sorting function
cout << "\n";
cout << "The Student List after Sorting:" << endl;
cout << "\n";
sort(vName.begin(), vName.end());
for (int y = 0; y < vName.size(); y++)
{
cout << vName[y] << endl;
}
cout << "\n";
// Search function
do
{
cout << "Please Enter a name to be searched:" << endl;
getline(cin, sSearch);
if (binary_search(vName.begin(), vName.end(), sSearch))
{
cout << sSearch << " was found." << endl << endl;
}
else
{
cout << sSearch << " was not found." << endl << endl;
}
cout << "Would you like to search another name?" << endl << endl;
cout << "Please enter Y for Yes and N for No:" << endl << endl;
cin >> cQuestion;
} while (cQuestion == "Y" || cQuestion == "y");
cout << "Thank you for using this program!" << endl;
return 0;
}
Edit:
Posted whole program, please excuse any grammatical mistakes, I'm just trying to get the program down before i go in there and make it pretty.
The tail of your loop does this:
cout << "Please enter Y for Yes and N for No:" << endl << endl;
cin >> cQuestion;
which will consume your string if you entered one, but leave the trailing newline in the input stream. Thus when you return to the top of the loop after entering Y or y, and do this:
cout << "Please Enter a name to be searched:" << endl;
getline(cin, sSearch);
the getline will extract an empty line.
How to consume the unread newline from the input stream is up to you. You will likely just end up using .ignore() as you did prior in your program. Or use getline to consume cQuestion. You have options. Pick one that works.
And as a side note, I would strongly advise you check your stream operations for success before assuming they "just worked". That is a hard, but necessary, habit to break. Something like this:
do
{
cout << "Please Enter a name to be searched:" << endl;
if (!getline(cin, sSearch))
break;
if (binary_search(vName.begin(), vName.end(), sSearch))
{
cout << sSearch << " was found." << endl << endl;
}
else
{
cout << sSearch << " was not found." << endl << endl;
}
cout << "Would you like to search another name?" << endl << endl;
cout << "Please enter Y for Yes and N for No:" << endl << endl;
} while (getline(cin,cQuestion) && (cQuestion == "Y" || cQuestion == "y"));
If cQuestion is a char array then you need to use strcmp or stricmp to compare it with another string i.e. "Y" and "y" in this case. If cQuestion is a single char then you need to compare with 'Y' and 'y' (i.e. with a single quote)
Strings in C++ are not first class types therefore they do not have some of the string operation that exist for other basic types like ints and floats. You do have std::string as part of the standard C++ library which almost fulfills the void.
If you just change the type of cQuestion to std::string your code should work but if you want to stick with chars then you will need to change the quote style.