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++,
Related
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 want the code to read through the file that is created and if they find the same string of data for student ID, or student name it will make the user enter another value before moving on. I was able to create and store the data so that it is persistent at this point but I have yet to figure out how to make the code search through the text file and find the user entered duplicates while at the same time forcing the user enter a new value.
I am unable to think of a way to accuracy show you all the code with including the entire program
#include<iostream>
#include<fstream>
#include <cstdlib>
#include <string>
#include "student.h"
#include "course.h"
#include "session.h"
using namespace std;
void fillStudents(student arg[], int size)
{
for (int i = 0; i < size; i++)
{
cout <<"Student #"<<i+1<<endl;
cout <<"=========="<<endl;
cout <<"Enter a student ID (i.e 97626): ";
cin >>arg[i].stuID;
cout <<"Enter the first name (i.e Ryan): ";
cin >>arg[i].fname;
cout <<"Enter the last name (i.e Brown): ";
cin >>arg[i].lname;
system("CLS");
}
}
void fillCourses(course c[], int size)
{
for (int i = 0; i < size; i++)
{
cout <<"Course #"<<i+1<<endl;
cout <<"=========="<<endl;
cout <<"Enter course name (i.e Data-Structures (put - instead of spaces)): ";
cin >>c[i].CourseName;
cout <<"Enter course ID (i.e CS-230 (put - instead of spaces)): ";
cin >>c[i].CourseID;
cout <<"Enter number of credits (i.e 3): ";
cin >>c[i].numberOfCredits;
system("CLS");
}
}
string CourseN(int choice, course ch[])
{
switch(choice)
{
case 1:
return ch[0].CourseID+" "+ch[0].CourseName;
break;
case 2:
return ch[1].CourseID+" "+ch[1].CourseName;
break;
case 3:
return ch[2].CourseID+" "+ch[2].CourseName;
break;
case 4:
return ch[3].CourseID+" "+ch[3].CourseName;
break;
case 5:
return ch[4].CourseID+" "+ch[4].CourseName;
break;
case 6:
return ch[5].CourseID+" "+ch[5].CourseName;
break;
case 7:
return ch[6].CourseID+" "+ch[6].CourseName;
break;
case 8:
return ch[7].CourseID+" "+ch[7].CourseName;
break;
case 9:
return ch[8].CourseID+" "+ch[8].CourseName;
break;
case 10:
return ch[9].CourseID+" "+ch[9].CourseName;
break;
default:
cout<<"Invalid decison!! Good Bye!"<<endl;
exit(0);
}
}
int creditTotal(int sel, course ch[])
{
switch(sel)
{
case 1:
return ch[0].numberOfCredits;
break;
case 2:
return ch[1].numberOfCredits;
break;
case 3:
return ch[2].numberOfCredits;
break;
case 4:
return ch[3].numberOfCredits;
break;
case 5:
return ch[4].numberOfCredits;
break;
case 6:
return ch[5].numberOfCredits;
break;
case 7:
return ch[6].numberOfCredits;
break;
case 8:
return ch[7].numberOfCredits;
break;
case 9:
return ch[8].numberOfCredits;
break;
case 10:
return ch[9].numberOfCredits;
break;
default:
cout<<"Invalid Decision!! Good Bye!"<<endl;
exit(0);
}
}
void fillSession(student arg[], course c[], session s[])
{
int c1, c2, c3, c4;
string ch1, ch2, ch3, ch4;
string startDate, endDate;
int cr1, cr2, cr3, cr4;
for (int i = 0; i < 4; i++)
{
for(int j = 0; j < 10; j++)
{
cout<<"class #"<<j+1<<" \t\t"<<c[j].CourseID<<" \t\t"<<c[j].CourseName<<" \t\t\t"<<"Credits: "<<c[j].numberOfCredits<<"\n\n";
}
cout<<"Enter your 1st class choice: ";
cin>>c1;
ch1=CourseN(c1,c);
cout<<"Enter your 2nd class choice: ";
cin>>c2;
ch2=CourseN(c2,c);
cout<<"Enter your 3rd class choice: ";
cin>>c3;
ch3=CourseN(c3,c);
cout<<"Enter your 4th class choice: ";
cin>>c4;
ch4=CourseN(c4,c);
s[i].courseID=ch1+"\n"+ch2+"\n"+ch3+"\n"+ch4;
cout<<"Enter a start date for your classes (i.e mm/dd/yyyy): ";
cin>>startDate;
s[i].startDate=startDate;
cout<<"Enter a end date for your classes (i.e mm/dd/yyyy): ";
cin>>endDate;
s[i].endDate=endDate;
cr1=creditTotal(c1, c);
cr2=creditTotal(c2, c);
cr3=creditTotal(c3, c);
cr4=creditTotal(c4, c);
s[i].totalCredits=cr1+cr2+cr3+cr4;
c1=0;
c2=0;
c3=0;
c4=0;
system("CLS");
}
}
void display_report(student s[], course c[], session se[])
{
for (int i = 0; i < 4; i++)
{
cout<<"Student ID: "<<s[i].stuID<<"\n"<<endl;
cout<<"Student Name: "<<s[i].lname<<", "<<s[i].fname<<"\n"<<endl;
cout<<"Course IDs: \n"<<se[i].courseID<<"\n"<<endl;
cout<<"Start Date: \n"<<se[i].startDate<<"\n"<<endl;
cout<<"End Date: \n"<<se[i].endDate<<"\n"<<endl;
cout<<"Total Credits: "<<se[i].totalCredits<<"\n"<<endl;
cout<<"\n"<<endl;
}
}
This is the function that I am using to create the file and continuously store the user entered information. it is within this function that I would want to try to search for the duplicate words
/**
* Writes session data to file.
* #param {session} sess sessions array.
*/
void writeSessionsToFile(session* sess, student* stude)
{
ofstream file_out("sessions.txt", ios::app);
// for now let us just assume there'll always be just 4 sessions.
for (size_t i = 0; i < 4; ++i)
{
file_out << stude[i].lname << ", "<<stude[i].fname<<"\n";
file_out << stude[i].stuID << "\n";
file_out << sess[i].courseID << "\n";
file_out << sess[i].startDate << "\n";
file_out << sess[i].endDate << "\n";
file_out << sess[i].totalCredits << "\n";
// empty line in the end of each entry.
file_out << endl;
}
}
int main()
{
cout<<"Welcome to the CS 230 Data Structures class add program!"<<endl;
cout<<"Please follow the prompt to gain a report of your courses for the year: "<<endl<<endl;
//declare and initialize 4 students
student stu[4];
fillStudents(stu, 4);
//declare and initialize 10 courses
course courses[10];
fillCourses(courses, 10);
//assign 4 courses for each student
cout<<"Enter a class selection based on the chart above (1-10), please do not choose the same class twice: "<<endl;
cout<<"Each student goes by how their information was added"<<endl<<endl;
session sessions[4];
fillSession(stu, courses, sessions);
writeSessionsToFile(sessions, stu);
//display a report showing each student with his/her courses
//show the total number of credits for each student
cout<<"Please find your name and other important information below to see your classes and credit totals: \n\n"<<endl;
cout <<"=========="<<endl;
display_report(stu, courses, sessions);
cout <<"=========="<<endl;
// instead of system(pause), pause doesn't work on linux.
cin.get();
return 0;
}
I have searched the internet as well as this website I have found function that counts the lines of the word that I am searching for, as well as functions that does find and forces the user to enter another value but I have yet to find one that I could implement in my code that will allow me to continue with the goal I have in mind
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.
//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;
};