I'm making a program for car company which has only 6 Salesman. The program will ask the user to input the name of each salesmen and his monthly salary, input the number of cars he sold in a month, as well as the brand and price of each car. The program will display the monthly income of each salesman (salary + commission), the average monthly income of the salesmen, the name(and income) of the salesman who has the highest income, the name (and income)of the salesman with the lowest income, the number of cars sold for each brand and the most popular
brand.
I'm stuck at passing the value of totalCommission from Salesman1() and Salesman2(). Another problem is, i don't know if i can CIN a string into an array named SalesmanName. Here is what i have done so far.
int main()
{
int cycle=1;
int SalesMan1(),SalesMan2();
do
{
switch(cycle)
{
case 1: SalesMan1(); cycle++; break;
case 2: SalesMan2(); cycle++; break;
/*case 3: SalesMan3(); cycle++; break;
case 4: SalesMan4(); cycle++; break;
case 5: SalesMan5(); cycle++; break;
case 6: SalesMan6(); cycle++; break;*/
default: cout<<"Printing out reports"<<endl;
system("\npause");
}
}while(cycle<=6);
return 0;
}
int SalesMan1()
{
char NameOne[40];
int numCar=1,carSold,carType,audiSold=0,bmwSold=0,mbenzSold=0;
double salary,carPrize,commission,totalCommission,allMonthlyIncome;
cout<<"Name of 1st Salesman:"<<endl;
cin.getline(NameOne,39);
cout<<"\nMonthly Salary: RM";
cin>>salary;
cout<<"\nNumber of car sold in a month: ";
cin>>carSold;
system("CLS");
do{
cout<<"\nEnter type of car sold as below: "<<endl;
cout<<"\n1-Audi"<<endl;
cout<<"2-BMW"<<endl;
cout<<"3-Mercedes Benz"<<endl;
cout<<"\nCar Type:";
cin>>carType;
switch(carType)
{
case 1:
cout<<"\nEnter the price sold: RM";
cin>>carPrize;
commission=0.02*carPrize;
totalCommission+=commission;
audiSold++;
system("CLS");
break;
case 2:
cout<<"\nEnter the price sold: RM";
cin>>carPrize;
commission=0.02*carPrize;
totalCommission+=commission;
bmwSold++;
system("CLS");
break;
case 3:
cout<<"\nEnter the price sold: RM";
cin>>carPrize;
commission=0.035*carPrize;
totalCommission+=commission;
mbenzSold++;
system("CLS");
break;
default :
cout<<"\nNot available!"<<endl;
system("pause");
return 1;
}
numCar++;
}while(numCar<=carSold);
allMonthlyIncome=allMonthlyIncome+totalCommission+salary;
}
int SalesMan2()
{
char NameTwo[40];
int numCar=1,carSold,carType,audiSold=0,bmwSold=0,mbenzSold=0;
double salary,carPrize,commission,totalCommission,allMonthlyIncome;
cout<<"Name of 2nd Salesman"<<endl;
cin.getline(NameTwo,39);
cout<<"\nMonthly salary: RM";
cin>>salary;
cout<<"\nNumber of car sold in a month: ";
cin>>carSold;
system("CLS");
do{
cout<<"\nEnter type of car sold as below: "<<endl;
cout<<"\n1-Audi"<<endl;
cout<<"2-BMW"<<endl;
cout<<"3-Mercedes Benz"<<endl;
cout<<"\nCar Type:";
cin>>carType;
switch(carType)
{
case 1:
cout<<"\nEnter the price sold: RM";
cin>>carPrize;
commission=0.02*carPrize;
totalCommission+=commission;
audiSold++;
system("CLS");
break;
case 2:
cout<<"\nEnter the price sold: RM";
cin>>carPrize;
commission=0.02*carPrize;
totalCommission+=commission;
bmwSold++;
system("CLS");
break;
case 3:
cout<<"\nEnter the price sold: RM";
cin>>carPrize;
commission=0.035*carPrize;
totalCommission+=commission;
mbenzSold++;
system("CLS");
break;
default :
cout<<"\nNot available!"<<endl;
system("pause");
return 1;
}
numCar++;
}while(numCar<=carSold);
allMonthlyIncome=allMonthlyIncome+totalCommission+salary;
}
In the SalesmanN() functions, end it with:
return totalCommission;
In main(), do:
int allCommissions = 0;
and then in your loop you do:
case 1: allCommissions += SalesMan1(); cycle++; break;
You can CIN into any character array but it is not preferred for names as CIN limits its input on spaces. The SalesmanName will not have any spaces.
char arr[1000];
cin`>>arr;
If you enter "XYZ ABC" as name, then arr[] will store only "XYZ" while "ABC" will remain in the input stream.
For this question you can create a Salesman Class which will encapsulate all the functions nd data required for each Salesman . As the company has only 6 workers you can make a array of 6 object of Salesman Class. So that you can refer to particular salesman's commission as SalesMan[i].getCommission(). Moreover class will help to expand to more number of workers later.
Related
How can I add these bills to total bill in c++, It is printing the bills of every product separately how to add these to total bill
#include<iostream>
using namespace std;
int main(){
cout<<"Welcome to ABC SuperMarket\n";
char cont;
cont = 'Y';
do{
cout<<"\nlist of Products\n ******\n1.Apple = $0.25\n2.Banana = $0.30\n3.Milk = $3\n4.Eggs = $0.50\n5.Bread = $3\n6.Chicken breasts = $5\n7.Spinach = $0.50\n8.Ground beef = $7\n";
int basket,qty;
float bill;
cout<<"\nSelect the product number you want to add in your basket. ";
cin>>basket;
switch(basket)
{
case 1:
cout<<"How many apples do you want? ";
cin>>qty;
bill = 0.25 * qty;
cout<<"$"<<bill;
break;
case 2:
cout<<"How many bananas do you want? ";
cin>>qty;
bill = 0.30 * qty;
cout<<"$"<<bill;
break;
case 3:
cout<<"How many cartons of milk do you want? ";
cin>>qty;
bill = 3 * qty;
cout<<"$"<<bill;
break;
case 4:
cout<<"How many eggs do you want? ";
cin>>qty;
bill = 0.50 * qty;
cout<<"$"<<bill;
break;
case 5:
cout<<"How many bread do you want? ";
cin>>qty;
bill = 3 * qty;
cout<<"$"<<bill;
break;
case 6:
cout<<"How many pieces of chicken breast do you want? ";
cin>>qty;
bill = 5 * qty;
cout<<"$"<<bill;
break;
case 7:
cout<<"How many spinach do you want? ";
cin>>qty;
bill = 0.50 * qty;
cout<<"$"<<bill;
break;
case 8:
cout<<"How much ground beef do you want? ";
cin>>qty;
bill = 7 * qty;
cout<<"$"<<bill;
break;
default:
cout<<"Invalid input";
}
cout<<"\n\nDo you want to add more items in your basket? Type Y or N. ";
cin>>cont;
} while (cont == 'Y');
return 0;
}
How to add these bills in total bill at the end?
How can I add these bills to total bill in c++,
ok so I haven't found a topic that is completely like my situation and haven't found anything that works for my situation. Currently I am working on a project for school and in the snippet of code below, I need to input more than one word for the salesItem[i]. I have tried many variations of this and haven't gotten it to work. Also see the screen shot below. Thank you if you can help and would appreciate if you could explain an answer for a beginner to understand! In the output you will see that nothing has actually been input into the program. It skips right over it to asking for price of that item.
case 1:
i = 1;
cout<<"Intial setup: Enter sales items"<<endl;
do
{
cout<<"Add sales item "<<i<<": ";
getline (cin,name);
salesItem[i] = name;
cout<<salesItem[i]<<" was entered."<<endl;
cout<<"Add a price for "<<salesItem[i]<<": ";
cin>> salesItemPrice[i];
i = i + 1;
cout<<"Are you done? Enter 'yes' or 'no'."<<endl;
cin>>exitChoice;
}
while((exitChoice != "yes") && (exitChoice == "no"));
Here is the full code for my project, the above is the snippet. I am having issues with.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int i = 30;
int choice;
string exitChoice = "";
string salesItem[i];
float salesItemPrice[i];
int numOfSalesItems[i];
string itemSearch = "";
string newItemName = "";
string itemNameUpdate ="";
float newSalesPrice;
string name = "";
do
{
cout<<" Main Menu "<<endl;
cout<<"----------------------------"<<endl;
cout<<"Please select a menu number:"<<endl;
cout<<"1. Initial sales item setup"<<endl;
cout<<"2. Modify sales item"<<endl;
cout<<"3. Display all sales items"<<endl;
cout<<"4. Add new sales item(s)"<<endl;
cout<<"10. Exit Program"<<endl;
cin>>choice;
switch(choice)
{
case 1:
i = 1;
cout<<"Intial setup: Enter sales items"<<endl;
do
{
cout<<"Add sales item "<<i<<": ";
getline (cin,name);
salesItem[i] = name;
cout<<salesItem[i]<<" was entered."<<endl;
cout<<"Add a price for "<<salesItem[i]<<": ";
cin>> salesItemPrice[i];
i = i + 1;
cout<<"Are you done? Enter 'yes' or 'no'."<<endl;
cin>>exitChoice;
}
while((exitChoice != "yes") && (exitChoice == "no"));
break;
case 2:
cout<<"Modify sales item"<<endl;
cout<<"Enter the name of the item to be modified"<<endl;
cin>>itemSearch;
for(int x = 1; x<i; x++)
{
if(salesItem[x] == itemSearch)
{
int menuChoice;
cout<<"Found Item: "<< salesItem[x]<<endl;
cout<<"Please select a menu number."<<endl;
cout<<"1. Modify name"<<endl;
cout<<"2. Modify item price"<<endl;
cout<<"3. Exit Menu"<<endl;
cin>>menuChoice;
switch(menuChoice)
{
case 1:
cout<<" Modify Name "<<endl;
cout<<"----------------------------"<<endl;
cout<<"Enter the new name for "<<salesItem[x]<<endl;
cin>>newItemName;
salesItem[x] = newItemName;
cout<<"The new item name is: "<< salesItem[x]<<endl;
cout<<"Is this correct? Enter 'yes' or 'no'."<<endl;
cin>>itemNameUpdate;
if(itemNameUpdate == "yes")
{
cout<<"Item name updated."<<endl;
break;
}
else if (itemNameUpdate == "no")
{
do
{
cout<<"Try again."<<endl;
cout<<"Enter the new name for "<<salesItem[x]<<endl;
cin>>newItemName;
salesItem[x] = newItemName;
cout<<"The new item name is: "<< salesItem[x]<<endl;
cout<<"Is this correct? Enter YES or NO."<<endl;
cin>>itemNameUpdate;
}
while(itemNameUpdate != "yes");
}
break;
case 2:
cout<<" Modify Price "<<endl;
cout<<"----------------------------"<<endl;
cout<<"Enter the new price for "<<salesItem[x]<<endl;
cin>>newSalesPrice;
salesItemPrice[x] = newSalesPrice;
cout<<"The new price for this item is "<<salesItemPrice[x]<<endl;
cout<<"Is this correct? Enter 'yes' or 'no'."<<endl;
cin>>itemNameUpdate;
if(itemNameUpdate == "yes")
{
cout<<"Price has been updated."<<endl;
}
else if (itemNameUpdate == "no")
{
do
{
cout<<"Try again."<<endl;
cout<<"Enter the new price for "<<salesItem[x]<<endl;
cin>>newSalesPrice;
salesItemPrice[x] = newSalesPrice;
cout<<"The new price for this item is "<<salesItemPrice[x]<<endl;
cout<<"Is this correct? Enter 'yes' or 'no'."<<endl;
cin>>itemNameUpdate;
}
while(itemNameUpdate != "yes");
}
}
}
}
break;
case 3:
std::cout << std::fixed<<std::left;
cout<<" Display all sales items "<<endl;
cout<<"------------------------------------"<<endl;
cout<<" Sales Item Item Price "<<endl;
for(int x = 1; x < i; x++){
cout<<"Item "<<x<<": "<<setw(17)<<salesItem[x]<<"$"<<setprecision(2)<<salesItemPrice[x]<<endl;
}
break;
case 4:
cout<<"Add new sales item"<<endl;
do
{
cout<<"Add sales item "<<i<<": ";
cin>>salesItem[i];
cout<<salesItem[i]<<" was entered."<<endl;
cout<<"Add a price for "<<salesItem[i]<<": ";
cin>> salesItemPrice[i];
i = i + 1;
cout<<"Are you done? Enter 'yes' or 'no'."<<endl;
cin>>exitChoice;
}
while(exitChoice != "yes");
case 10:
cout<<"Exiting Program"<<endl;
break;
}
}
while(choice != 10);
}
Output.....
Main Menu
Please select a menu number:
Initial sales item setup
Modify sales item
Display all sales items
Add new sales item(s)
Exit Program
1
Intial setup: Enter sales items
Add sales item 1: was entered.
Add a price for :
also as you can see below here, works fine this way
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string name;
cout << "Enter Name: ";
getline (cin,name);
cout << "You entered: " << name;
}
output.....
Enter Name: John Smith
You entered: John Smith
I am a beginner that has been coding in c++ for a few weeks now. I have written a my program to have a menu from which a user selects options to perform different tasks. I have 2 questions: firstly how do I make it so the user gets sent back to the menu after performing a task and secondly, how do I make it so when the user is assigning variables (is that what you call it?) the numbers stay on the same line?
#include <iostream>
using namespace std;
int main() {
int choice;
float no0,no1,no2,no3,sum0,ave,pi,rad,areaC,base,height,areaT;
pi=3.142;
cout<<"______________________________________________\n";
cout<<"| MENU |\n";
cout<<"|--------------------------------------------|\n";
cout<<"|1. Calculate the average of 4 numbers. |\n";
cout<<"|2. Calculate the area of a circle. |\n";
cout<<"|3. Calculate the area of a triangle. |\n";
cout<<"|4. Print 'Hello World! |\n";
cout<<"|--------------------------------------------|\n";
cout<<"Enter a number to make a selection... ";
cin>>choice;
switch (choice)
{
case 1:
cout<<"Enter 4 numbers to find their average: ";
cin>>no0>>no1>>no2>>no3;
sum0=no0+no1+no2+no3;
ave=sum0/4;
cout<<"The average of your 4 numbers is: "<<ave<<"\n";
break;
}
switch (choice)
{
case 2:
cout<<"Enter the radius of a cirlce to find its area: ";
cin>>rad;
areaC=pi*rad*rad;
cout<<"The area of your circle is:"<<areaC<<"\n";
break;
}
switch (choice)
{
case 3:
cout<<"Enter the base and height of a triangle to find its area.""\n";
cout<<"Base: "; cin>>base;
cout<<"Height: "; cin>>height;
areaT=base*height*0.5;
cout<<"The area of your triangle is: "<<areaT<<"\n";
break;
}
switch (choice)
{
case 4:
cout<<"Hello World!";
}
return 0;
}
You can add an endless loop that will return your user to the beginning of the program indefinitely. If you want it to stop, you can add a case that sets active to false.
I also fixed your switch cases. As someone mentioned, it's not necessary to switch for each case - the program will automatically find the correct path.
Do something like this:
#include <iostream>
using namespace std;
int main() {
bool active = true;
while(active)
{
int choice;
float no0,no1,no2,no3,sum0,ave,pi,rad,areaC,base,height,areaT;
pi=3.142;
cout<<"______________________________________________\n";
cout<<"| MENU |\n";
cout<<"|--------------------------------------------|\n";
cout<<"|1. Calculate the average of 4 numbers. |\n";
cout<<"|2. Calculate the area of a circle. |\n";
cout<<"|3. Calculate the area of a triangle. |\n";
cout<<"|4. Print 'Hello World! |\n";
cout<<"|5. Quit. |\n";
cout<<"|--------------------------------------------|\n";
cout<<"Enter a number to make a selection... ";
cin>>choice;
switch (choice)
{
case 1:
cout<<"Enter 4 numbers to find their average: ";
cin>>no0>>no1>>no2>>no3;
sum0=no0+no1+no2+no3;
ave=sum0/4;
cout<<"The average of your 4 numbers is: "<<ave<<"\n";
break;
case 2:
cout<<"Enter the radius of a cirlce to find its area: ";
cin>>rad;
areaC=pi*rad*rad;
cout<<"The area of your circle is:"<<areaC<<"\n";
break;
case 3:
cout<<"Enter the base and height of a triangle to find its area.""\n";
cout<<"Base: "; cin>>base;
cout<<"Height: "; cin>>height;
areaT=base*height*0.5;
cout<<"The area of your triangle is: "<<areaT<<"\n";
break;
case 4:
cout<<"Hello World!";
break;
case 5:
active = false; // Could even just return 0 here
break;
} // End Switch
} // End Loop
return 0;
}
I recommend use an alternative the cycle for reset your program, also group the different case in the same switch for example:
#include <iostream>
using namespace std;
int main() {
float no0,no1,no2,no3,sum0,ave,pi,rad,areaC,base,height,areaT;
pi=3.142;
int choice;
do{
cout<<"______________________________________________\n";
cout<<"| MENU |\n";
cout<<"|--------------------------------------------|\n";
cout<<"|1. Calculate the average of 4 numbers. |\n";
cout<<"|2. Calculate the area of a circle. |\n";
cout<<"|3. Calculate the area of a triangle. |\n";
cout<<"|4. Print 'Hello World! |\n";
cout<<"|5. Quit. |\n";
cout<<"|--------------------------------------------|\n";
cout<<"Enter a number to make a selection... ";
cin>>choice;
switch (choice){
case 1:
cout<<"Enter 4 numbers to find their average: ";
cin>>no0>>no1>>no2>>no3;
sum0=no0+no1+no2+no3;
ave=sum0/4;
cout<<"The average of your 4 numbers is: "<<ave<<"\n";
break;
case 2:
cout<<"Enter the radius of a cirlce to find its area: ";
cin>>rad;
areaC=pi*rad*rad;
cout<<"The area of your circle is:"<<areaC<<"\n";
break;
case 3:
cout<<"Enter the base and height of a triangle to find its area.""\n";
cout<<"Base: "; cin>>base;
cout<<"Height: "; cin>>height;
areaT=base*height*0.5;
cout<<"The area of your triangle is: "<<areaT<<"\n";
break;
case 4:
cout<<"Hello World!\n";
break;
case 5:
cout<<"Bay\n";
break;
default:
cout<<"Wrong selection\n";
break;
} // End Switch
//If you want clear the screen you can use the instruction:cout<<"\033[2J\033[1;1H";
}while(choice != 5); // End Loop
return 0;
}
I am a novice programmer and I'm having trouble with one of my exercises. I need to create a program that reads the student information (SSN, First & last name, test score) from a .txt file called "students.txt", and then uses four global parallel arrays to store the student information. I'm required to use an int array to store SSN, a string array to store first names, a string array to store last names, and a double array to store scores. The operations include listing the students' info like this exactly how it appears:
SSN Last-Name First-Name Score
628130189 James, Paul 92.0
237698211 Cook , Daniel 86.0
201895367 Garza, Melessa 78.0
491066285 Barbara, Jessica 62.0
168606868 Bruce, Elizabeth 90.0
378205732 Lee, Sarah 91.5
118453900 Brian, David 87.0
583192186 Garza, Cody 92.0
226665118 Lewis, Gage 78.0
175382843 Collins, James 69.5
816231095 White, Ann 88.5
376651608 Jackson, Mark 72.0
508234567 Freeman, Mark 86.0
763211099 William, Jack 52.0
286204723 Rodriguez, John 69.5
But of course, the .txt file is just SSN First-name last-name and score unaligned and separated by comas. Then I have to display the student with the highest score, and then the lowest, and the average of all scores using "void". I just dont know how to get it to read the file in the first place and make the data so that it's organized into columns.
Also...I have to report an error message and exit the program immediately if the file cannot be found? Otherwise, the program should display a main menu as follows to allow the user to complete the listed
operations.
Here's what I have so far:
#include <iostream>
#include<iomanip>
#include<fstream>
using namespace std;
void mainmenu();
/*
void sort_name();();
void sort_ssn();
void sort_score ();
*/
void average();
void lowest_score();
void highest_score();
void open_file();
const int totNum = 15; //my global variables
int ssn[totNum];
string fname [totNum];
string lname [totNum];
double score [totNum];
int main ()
{
char choice;
ifstream fin;
fin.open("students.txt")
for (1=0; i <= totNum; i++)//this is where I'm stuck
{
?????
}
do
{ mainmenu(); //calling main menu funtion
cin >> choice;
cin.ignore (10, '\n'); //spacing
switch(choice) // switch statment for multiple cases for flexibility
{
case 'l':
case 'L':
case '1': open_file(); break;
/*
case 'h':
case 'H':
case '2': highest_score(); break;
case 'o':
case 'O':
case '3': lowest_score(); break;
case 'a':
case 'A':
case '4': average(); break;
case 's':
case 'S':
case '5': sort_ssn(); break; //operation 5-7 is a bonus work
case 'n':
case 'N':
case '6': sort_name(); break;
case 'c':
case 'C':
case '7': sort_score();break;
*/
case 'e':
case 'E':
case '0': break;
default: cout << "Wrong choice!" << endl; break;
}
cout << endl;
} while(choice != '0'); // && choice !='E' && choice!='e')
}
void mainmenu() //output main menu
{
cout << "Main Menu (Assignment 8)" << endl;
cout << "1. List students' infromation (L)" << endl;
/*
cout << "2. Find the highest score (H)" << endl;
cout << "3. Find the lowest score (O)" << endl;
cout << "4. Calculate the average score (A)" << endl;
cout << "5. Sort students by SSN (S)" << endl;
cout << "6. Dort students by name (N)" << endl;
cout << "7. Sort students by score (C)" << endl;
*/
cout << "0. Exit" << endl;
cout << "Please select an option: ";
}
it doesn't make any sense now but that's why I'm desperate for help...
//my program to long and have error..i want to make it simple and easy to read..how to change it in switch program?? help me please!! =(
#include <iostream>
using namespace std;
int main ()
{
int movie1;
int movie2;
int movie3;
int seats;
int price;
int select;
char response;
cout<<"______Wellcome to Strawberry Gold Cinema____\n"
<<"Now you are booking a ticket cinema and please choose your movie... ";
cout << "\nPress 1 for= Harry Potter \n"
<< "Press 2 for = Iron Man\n"
<<"Press 3 for = Romeo and Juliet\n";
cout<<"Enter your choice > ";
cin>>select;
if (select ==1 )
{
cout<<"The price of the ticket per seat is RM10.00\n"
<<"Please enter the number of seat ";
cin>>seats;
if (seats<=30)
{
price = 10 * seats;
cout<<"The total price is RM"<<price<<endl;
}
else if (seats>=30)
{
cout<<"The movie is full.\n"
<<"Would like to choose another movie??\n"
<<"Section Y or N \n";
cout<<"Enter your choice = ";
cin>>response;
}
if ( toupper( response ) == 'Y' )
{
cout <<"Please choose movie 2 or movie 3\n"
<<"Enter your choice ";
cin>>select;
if (select == 2)
{
cout<<"The price of the ticket per seat is RM11.00\n"
<<"Please enter the number of seat";
cin>>seats;
if (seats<=30)
{
price = 11 * seats;
cout<<"The total price is RM"<<price<<endl;
}
}
else if (seats>=30)
{
cout<<"The movie is full.\n"
<<"Would like to choose another movie??\n"
<<"Section Y or N \n";
cout<<"Enter your choice = ";
cin>>response;
}
if ( toupper( response ) == 'Y' )
{
cout<<"The price of the ticket per seat is RM13.00\n"
<<"Please enter the number of seat";
cin>>seats;
if (seats<=30)
{
price = 13 * seats;
cout<<"The total price is RM"<<price<<endl;
}
else if (seats>=30)
{
cout<<"The movie is full.\n"
<<"Next movie in 5 hours...."<<endl;
}
else
cout << "Next movie in 5 hours.\n";
}
}
}
//for 2
if (select ==2 )
{
cout<<"The price of the ticket per seat is RM11.00\n"
<<"Please enter the number of seat";
cin>>seats;
if (seats<=30)
{
price = 11 * seats;
cout<<"The total price is RM"<<price<<endl;
}
else if (seats>=30)
{
cout<<"The movie is full.\n"
<<"Would like to choose another movie??\n"
<<"Section Y or N \n";
cout<<"Enter your choice = ";
cin>>response;
}
if ( toupper( response ) == 'Y' )
{
cout <<"Please choose movie 1 or movie 3\n"
<<"Enter your choice ";
cin>>select;
if (select == 2)
{
cout<<"The price of the ticket per seat is RM10.00"
<<"Please enter the number of seat";
cin>>seats;
if (seats<=30)
{
price = 10 * seats;
cout<<"The total price is RM"<<price<<endl;
}
}
else if (seats>=30)
{
cout<<"The movie is full.\n"
<<"Would like to choose another movie??\n"
<<"Section Y or N \n";
cout<<"Enter your choice = ";
cin>>response;
}
if ( toupper( response ) == 'Y' )
{
cout<<"The price of the ticket per seat is RM13.00\n"
<<"Please enter the number of seat";
cin>>seats;
if (seats<=30)
{
price = 13 * seats;
cout<<"The total price is RM"<<price<<endl;
}
else if (seats>=30)
{
cout<<"The movie is full.\n"
<<"Next movie in 5 hours...."<<endl;
}
else
cout << "Next movie in 5 hours.\n";
}
}
}
//for seat 3
if (select ==3 )
{
cout<<"The price of the ticket per seat is RM13.00\n"
<<"Please enter the number of seat";
cin>>seats;
if (seats<=30)
{
price = 13 * seats;
cout<<"The total price is RM"<<price<<endl;
}
else if (seats>=30)
{
cout<<"The movie is full.\n"
<<"Would like to choose another movie??\n"
<<"Section Y or N \n";
cout<<"Enter your choice = ";
cin>>response;
}
if ( toupper( response ) == 'Y' )
{
cout <<"Please choose movie 1 or movie 2\n"
<<"Enter your choice ";
cin>>select;
if (select == 1)
{
cout<<"The price of the ticket per seat is RM10.00\n"
<<"Please enter the number of seat";
cin>>seats;
if (seats<=30)
{
price = 10 * seats;
cout<<"The total price is RM"<<price<<endl;
}
}
else if (seats>=30)
{
cout<<"The movie is full.\n"
<<"Would like to choose another movie??\n"
<<"Section Y or N \n";
cout<<"Enter your choice = ";
cin>>response;
}
if ( toupper( response ) == 'Y' )
{
cout<<"The price of the ticket per seat is RM12.00\n"
<<"Please enter the number of seat";
cin>>seats;
if (seats<=30)
{
price = 12 * seats;
cout<<"The total price is RM"<<price<<endl;
}
else if (seats>=30)
{
cout<<"The movie is full.\n"
<<"Next movie in 5 hours...."<<endl;
}
else
cout << "Next movie in 5 hours.\n";
}
}
}
return 0;
}
As a rule of thumb, any time you find yourself copying and pasting more than one or two lines of code, you should stop and think whether it would be cleaner to refactor that code into a function.
You can in this case create a function do_movie_specific_stuff() that takes as its arguments the data that is different between the three movies (if there is anything different between the three switch cases). Then each case in your switch statement is a single statement: a call to that function with the correct arguments.
I would recommend putting your logic into a loop and breaking your logic into functions, or possibly classes, if you're familiar. Something like this (this is non-compiled/non-tested semi-pseudo code... just to give you an idea):
bool done = false;
do
{
cout << "Please choose a move (1, 2, or 3)" << endl;
cin >> movieId;
double price = getMoviePrice(movieId);
cout << "The price of the movie is: " + price << endl;
cout << "How many seats" << endl;
cin >> seats;
int availableSeats = getAvailableSeats(movieId);
// and so on... If the user indicates they want to quit, just set done to true!
} while (!done)
If you need to keep track of what movies the user has already tried, you can do that, you'll just have to keep track of that somewhere, and handle it in the logic.
The code you have shown is a good example with the problems of purely linear code: It just doesn't work.
As a tip: I would either create three functions to handle each movie, or more preferably, create a generic function to handle all movies, which will take its data from some class:
class Movie {
std::wstring name;
int seats;
int soldSeats;
int pricePerSeat;
};