//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;
};
Related
I really need some help and any suggestions would be appreciated.
In the alignment of outputs, the characters move once an excess character was involved. I was hoping that the program will output in one place regardless of how many characters there are. I tried cout.width() but no avail. In the search function, the search works once you entered the ID of the student you first registered but once you entered the second student, It can not detect the second student but it can still detect the first student. In the Edit function, I am stuck at the Check ID and cannot proceed to editing and I tried copying some codes from the net but to no avail because they are using public and class and not structure. There is some warning that says "234 48 [Warning] ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:" but the program still works and it will be set aside for now.
#include<iostream>
//fstream is needed because we will use file systems.
#include<fstream>
#include<string.h>
#include<stdlib.h>
#include<iomanip>
using namespace std;
/*declaring fstream global variable used
to open, write, read and close files in this program.*/
fstream storage;
struct student_profile{
char FName[100];
char LName[100];
char reg_year[100];
char course[100];
char id[100];
int no_courses;
char course_Name[100];
char course_code[100];
int credit_hour;
int quiz_one;
int quiz_two;
int quiz_three;
int mid;
int assignment;
int lab;
int final_exam;
char ID[100];
};
/* creating structure variable from student_profile.
the name this structure variable is student.
it is used to hold name, last name, id etc
*/
student_profile student;
/* function used to register student.
it accept student details then it write those data to file.
so student details will be saved in text file for later use.
*/
void function_to_register(){
cout<<" Write First Name of student: ";
cin>>student.FName;
cout<<" Write Last Name of student: ";
cin>>student.LName;
cout<<" Write Registration year: ";
cin>>student.reg_year;
cout<<" Write Course of student: ";
cin>>student.course;
cout<<" Write number of courses that student learns in this semester and register them one by one\n";
cout<<" Number of courses: ";
cin>> student.no_courses;
cout <<" Write ID: ";
cin>>student.ID;
/* for loop used to register multiple courses for one student
based on the student's number of courses entered.
*/
for(int a=1; a<=student.no_courses; a++) {
cout<<" Write course Name: ";
cin>>student.course_Name;
cout<<" Enter grade for quiz one: ";
cin>>student.quiz_one;
cout<<" Enter grade for quiz two: ";
cin>>student.quiz_two;
cout<<" Enter grade for quiz three: ";
cin>>student.quiz_three;
cout<<" Enter midterm grade: ";
cin>>student.mid;
cout<<" Enter assignment grade: ";
cin>>student.assignment;
cout<<" Enter lab grade: ";
cin>>student.lab;
cout<<" Enter final exam grade: ";
cin>>student.final_exam;
storage.open("marklist.txt",ios::app) ;
storage.write((char*)&student, sizeof(student));
//storage.write(student,sizeof(student));
storage.close();
}
}
/* function used to calculate grade of each course.
we call this function when the program is displaying grade of student.
*/
void grade_calculator(int totalMark) {
if(totalMark>=90 && totalMark<=100)
cout<<"A+";
else if(totalMark>=80 && totalMark<90)
cout<<"A";
else if(totalMark>=70 && totalMark<80)
cout<<"B";
else if(totalMark>=60 && totalMark<70)
cout<<"C+";
else if(totalMark>=50 && totalMark<60)
cout<<"D";
else if(totalMark>=0 && totalMark<50)
cout<<"F";
else
cout<<"NG";
}
/* function used to display registered students on the screen.
this function gets the registered student from that file
we used above to store student details.
*/
void seeRegistered() {
storage.open("marklist.txt",ios::app);
storage.close();
storage.open("marklist.txt",ios::in);
storage.read((char*)&student, sizeof(student));
while (storage.eof()==0) {
cout<<"\n\n\n";
cout<<" Full Name: "<<student.FName<<" "<<student.LName<<endl;
cout<<" ID: "<<student.ID<<endl;
cout<<" Registration Year: "<<student.reg_year<<endl;
cout<<" Faculty: "<<student.course<<endl;
cout<<" *****************************************************************************\n";
cout<<" ******************************STUDENT MARK LIST******************************\n";
cout<<" *****************************************************************************\n";
cout<<" Subject Quiz1 Quiz2 Quiz3 Mid Assignment Final Total Grade\n";
cout<<" _____________________________________________________________________________\n";
/* for loop used to print all courses and their marks
for a single student.
*/
for(int t=1; t<=student.no_courses; t++) {
int totalMark;
totalMark= (student.quiz_one+student.quiz_two+student.quiz_three+student.assignment+student.mid+student.final_exam) / 6;
cout.width(2); cout<<""<<student.course_Name;
cout.width(7); cout<<""<<student.quiz_one;
cout.width(6); cout<<""<<student.quiz_two;
cout.width(6); cout<<""<<student.quiz_three;
cout.width(7); cout<<""<<student.mid;
cout.width(4); cout<<""<<student.assignment;
cout.width(12); cout<<""<<student.final_exam;
cout.width(6); cout<<""<<totalMark<<" ";
cout <<" ";grade_calculator(totalMark);
cout<<endl;
storage.read((char*)&student,sizeof(student));
}
}
storage.close();
}
void searchStudent(){
std::string search;
cout<<"Please enter your ID"<< endl;
cin>>search;
storage.open("marklist.txt", ios::in);
storage.read((char*)&student,sizeof(student));
while (storage.eof()==0)
{
if(student.ID==search)
{
cout<<"\n\n\n";
cout<<" Full Name: "<<student.FName<<" "<<student.LName<<endl;
cout<<" ID: "<<student.ID<<endl;
cout<<" Registration Year: "<<student.reg_year<<endl;
cout<<" Faculty: "<<student.course<<endl;
cout<<" *****************************************************************************\n";
cout<<" ******************************STUDENT MARK LIST******************************\n";
cout<<" *****************************************************************************\n";
cout<<" Subject Quiz1 Quiz2 Quiz3 Mid Assignment Final Total Grade\n";
cout<<" _____________________________________________________________________________\n";
for(int t=1; t<=student.no_courses; t++) {
int totalMark;
cout.width(2); cout<<""<<student.course_Name;
cout.width(7); cout<<""<<student.quiz_one;
cout.width(6); cout<<""<<student.quiz_two;
cout.width(6); cout<<""<<student.quiz_three;
cout.width(7); cout<<""<<student.mid;
cout.width(4); cout<<""<<student.assignment;
cout.width(12); cout<<""<<student.final_exam;
cout.width(6); cout<<""<<totalMark<<" ";
cout <<" ";grade_calculator(totalMark);
cout<<endl;
}
}
else{
cout<< "\n\n***************Student not found***************\n\n"<<endl;
}
break;
}
storage.read((char*)&student, sizeof(student));
storage.close();
}
void editStudent(){
system("cls");
char checkID[100];
cout <<"Enter Student ID: ";
cin >> checkID;
storage.open("marklist.txt",ios::in|ios::out);
storage.read((char*)&student,sizeof(student));
while (storage.eof()==0);
{
if (student.ID==checkID)
{
cout << "Name: " <<student.FName <<" " <<student.LName <<endl;
cout << "Registration Year: " <<student.reg_year <<endl;
cout << "Course: "<<student.no_courses <<endl;
cout << "ID: " <<student.ID<<endl;
cout <<"\n Enter New Name: ";
cin >>student.FName;
cout <<"\n Enter New Last Name: ";
cin>>student.LName;
cout <<"\n Enter New Registration Year: ";
cin>>student.reg_year;
cout<<"\n Enter New Course: ";
cin>>student.course;
cout<<"\n Enter New ID: ";
cin >>student.ID;
storage.seekp(storage.tellg()-sizeof(student));
storage.write((char*)&student,sizeof(student));
cout<<"\n\n File Updated";
}
else{
cout<<"ID not found"<<endl;
}
storage.read((char*)&student,sizeof(student));
}
storage.close();
}
/* This is the main function which will be executed before all functions,
So in this program when the user select one option then by
the help switch statement the user redirects to appropriate function.
*/
int main() {
char select;
/* while loop that help us to excute the program all time.
this means the program will not exit without our interest.*/
while(1) {
cout<<endl; cout<<endl; cout<<endl;
cout<<endl;
cout<<" **********************************************\n";
cout<<" * STUDENT REGISTRATION AND MARK LIST SYSTEM *\n";
cout<<" **********************************************\n";
cout<<" *\n";
cout<<" Enter 1 To Register New student \n";
cout<<" Enter 2 To Show Registerd Students and Their Grade \n";
/*
you can improve this in which the displayed record is sorted (ascending/descending)
*/
cout<<" Enter 3 To Search for a student \n";
cout<<" Enter 4 To Edit a student record\n";
cout<<" Enter 5 To Delete a student record\n";
cout<<" Enter 0 To Exit \n";
cout<<" : ";
cin>>select;
switch (select) {
case '1':
function_to_register();
cout<<" Student Registered successfully \n";
break;
case '2':
seeRegistered();
break;
case '3':
searchStudent();
break;
case '4':
editStudent();
cout<<"\n\nStudent Profile Updated Successfully"<<endl;
break;
case '0':
exit(0);
}
}
}
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'm writing a program for my C++ class I've complete the program. but it won't compile. I'm new to programming so I don't really know what I'm doing wrong. If there is anyone on here that can point me in the right direction. Please help me!
Prompt Description:
Write a C++ program to calculate free estimate for carpet and furniture cleaning of residential and business customers. The program continues until end of file is reached.
Fro residential customers, specify and update number of small couches ($50 each), large couches ($80 each), rooms ($70 each) and steps ($5 each) until exit is selected. the bill is calculated based on the number of items. If the amount is more than 500 dollars, a discount of 10% is applied to the bill. Then the customer is offered to select from an installment of 1,2,3, or 4 or press 0 to exit. Based on an installment option, the bill is increased slightlenter code herey, and each installment amount is calculated.
For business customers, ask the user to enter the amount of square footage and then the bill is calculated at .45 per square foot.
Here is the code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int exit = 0;
int smallcouches = 1;
int largecouches = 2;
int rooms = 3;
int steps = 4;
const float SMALL_COUCH = 50.0;
const float LARGE_COUCH = 80.0;
const float ROOMS = 70.0;
const float STEPS = 5.00;
const float PER_SQUARE_FOOT = 0.45f;
const float DISCOUNT_QUALIFIED = 500.0;
const float DISCOUNT = 0.10f;
const int ONE = 1;
const int TWO = 2;
const int THREE = 3;
const int FOUR = 4;
const float RATEONE = 0.0f;
const float RATETWO = 0.0535f;
const float RATETHREE = 0.055f;
const float RATEFOUR = 0.0575f;
float billAmount;
float ResidentialEstimate(float SMALL_COUCH, float LARGE_COUCH, float ROOMS, float STEPS, float DISCOUNT_QUALIFIED);
void GetResidentialItems(int& smallcouches, int& largecouches, int& rooms, int& steps);
void InstallmentPlan(float billAmount);
void BusinessEstimate(float PER_SQUARE_FOOT);
int main()
{
cout << fixed << showpoint << setprecision(2);
int customerType, residential_customer, business_customer;
char B, b, R, r;
residential_customer = R || r;
business_customer = B || b;
cout << "Enter customer type: R, r (Residential) or B, b (Business): ";
cin >> customerType; //Enter customer type
cout << endl;
while (cin) //While there is input to read
{
if (customerType == R || customerType == r) //if residential customer
{
ResidentialEstimate(SMALL_COUCH, LARGE_COUCH, ROOMS, STEPS, DISCOUNT_QUALIFIED); // call function ResidentialEstimate
InstallmentPlan(billAmount); // all function Installmentplan
}
else if (customerType == B || customerType == b) //else if business customer
{
BusinessEstimate(PER_SQUARE_FOOT); //call function BusinessEstimate
}
cout << "Enter customer type: R, r (Residential) or B, b (Business): ";
cin >> customerType; // Enter cutomer type
cout << endl;
}
return 0;
}
float ResidentialEstimate(float SMALL_COUCH, float LARGE_COUCH, float ROOMS, float STEPS, float DISCOUNT_QUALIFIED)
{
GetResidentialItems(smallcouches, largecouches, rooms, steps); //Call function GetResidentialItems to get items to clean
billAmount = (SMALL_COUCH + LARGE_COUCH + ROOMS + STEPS); //Calculate the bill amount
if (billAmount > 500) //if bill amount is more than 500 dollars
{
DISCOUNT_QUALIFIED = billAmount * 0.10f;
billAmount = billAmount - DISCOUNT_QUALIFIED; //Apply a discount of 10% to the bill amount
}
return billAmount; //return bill Amount
}
void GetResidentialItems(int& smallcouches, int& largecouches, int& rooms, int& steps)
{
int count;
int choice = smallcouches || largecouches || rooms || steps;
//Ask user to select an item to update or press 0 to exit
cout << "0. Exit, 1. Small Couches, 2. Large Couches, 3. Rooms, 4. Steps " << endl;
cout << "Enter one of the above choices: ";
cin >> choice;
cout << endl;
while (choice > 0) //while user hasn't pressed 0
{
choice = count;
cout << "Please enter the number of " << choice; //ask the user to enter a number from the item selected
cin >> count;
cout << endl;
//Show the current selections and numbers
cout << "Current selections: " << count << " Small Couches, " << count << " Large Couches, " << count << " Rooms, " << count << " Steps.";
//Ask user to select an item to update or press 0 to exit
choice = 0;
count = 0;
cout << "0. Exit, 1. Small Couches, 2. Large Couches, 3. Rooms, 4. Steps " << endl;
cout << "Enter one of the above choices: ";
cin >> choice;
cout << endl;
}
}
void InstallmentPlan(float billAmount)
{
int num;
int installment = 0;
int bill = 0;
//Ask user to select number of installments or 0 to exit
cout << "Please enter the desired number of instalments (1, 2, 3, or 4) or 0 to exit : ";
cin >> num;
cout << endl;
while (num > 0) //while user hasn't pressed 0
{
//calculate the installments
if (num == 1)
installment = billAmount;
else if (num == 2)
{
bill = billAmount * 0.0535f;
installment = bill / num;
}
else if (num == 3)
{
bill = billAmount * 0.055f;
installment = bill / num;
}
else if (num == 4)
{
bill = billAmount * 0.0575f;
installment = bill / num;
}
cout << "With " << num << " installment your bill of " << billAmount << " will be worth " << bill << "." << endl;
cout << "Each installment will be worth " << installment << endl;
//Ask user to select number of installments or 0 to exit
cout << "Please enter the desired number of instalments (1, 2, 3, or 4) or 0 to exit : ";
cin >> num;
cout << endl;
}
}
void BusinessEstimate(float squarefootage)
{
//Ask user for the square footage
cout << " Enter the approximate square footage: ";
cin >> squarefootage;
cout << endl;
//Calculate the bill amount
billAmount = squarefootage * PER_SQUARE_FOOT;
cout << "Your free Business Customer Estimate for " << squarefootage << "square footage = " << billAmount;
}
I have to write the following program to control a bank account (as homework):
Create the program prototype using a named class Account. Each class is characterized by an owner name and balance.
The following banking operations should be added to the program: The owner can withdraw an amount from his account. Add account information. the owner can deposit an amount in his account. The owner can transfer an amount to another account. The owner can check the account balance.
In order to test the program the following program must be done: Create two Object account A1 and A2; Deposit 500 in A1. Deposit 200 in A2. Withdraw 100 from A1. Display the balance for A1. Withdraw 50 from A2. Display the balance for A2. Transfer 150 from A1 to A2.
Here is what I have done. I haven't added ant classes yet, it is quite uncompleted.
#include <iostream>
using namespace std;
int main () {
int password = 30718042;
int OperationNumber, amount, CurrentBalance, AccountNumber, selection;
cout <<"Welcome FATIH HANCER, please enter your password: ";
cin >> password;
while (password != 30718042) {
cout <<"Please enter your password again. ";
cin >> password;
}
while (password == 30718042) {
cout <<"Please enter your operation number: "<<endl<<"1. Withdraw"<<endl<<"2. Deposit"<<endl<<"3. Transfer"<<endl<<"4. Check the balance"<<endl;
cin >> OperationNumber;
if (OperationNumber == 1) {
cout <<"Please enter the amount you would like to withdraw. "<<endl;
cin >> amount;
int CurrentBalance -= amount;
cout <<"Deposit action is completed. Your new balance is "<<CurrentBalance<<endl;
}
cout <<"\nIf there is another operation to do, please press 1. Otherwise, you will be redirected to the main menu. "<<endl;
cin >> selection;
if (selection != 1) {
cout <<"Thank you for preferring us. "<<endl;
break;
}
}
if (OperationNumber == 2) {
cout <<"Please enter the amount you would like to deposit."<<endl;
cin >> amount;
int CurrentBalance += amount;
cout <<"Deposit action is completed. Your new balance is "<<CurrentBalance<<endl;
}
if (OperationNumber == 3) {
cout <<"Please enter the account number of whom you would like to transfer."<<endl;
cin >> AccountNumber;
while (AccountNumber == 30718059) {
cout <<"Please enter the amount of money you would like to transfer to ABDULRAHMAN SUBH's account"<<endl;
cin >> amount;
int CurrentBalance -= amount;
cout <<"Deposit action is completed. Your new balance is "<<CurrentBalance<<endl;
break;
};
}
while (AccountNumber != 30718059) {
cout <<"The user couldn't be found. ";
break;
}
if (OperationNumber == 4) {
cout <<"Your balance is "<<CurrentBalance<<endl;
}
return 0;
}
How can I fix [Error] expected initializer before '-=' token?
Besides, I want a balance being changeable by withdrawing, depositing and transferring money.
int CurrentBalance -= amount;
You need to declare and initialize CurrentBalance variable e.g.
int CurrentBalance = 100;
CurrentBalance -= amount;
-= - this operation means CurrentBalance = CurrentBalance - amount;
Greetings,
I'm just looking for a bit of help here. Here's the prompt:
For research purposes the admissions officers of your local university wants to know how well female and male students perform in certain courses. The user enters the number of courses to be considered. The student's data for each course is provided as a GPA followed by a letter code representing the gender of the student in this format: each line of input consists of a real number representing the student's GPA followed by the letter code f (for female students); m (for male students).
The number of entries (students) for each course is not known and the number 0.0 followed by the letter O indicates the end of data for specific course.
That being said, this is an introduction to c++ and as such; arrays, strings, and anything else outside of int, floats, doubles, and char is basically not allowed. In the code there needs to be the ability to type in various entries in any order (male entry followed by female and as well as the opposite.)
the issue i'm having is this, at the end of the program it is required to give an output of "General School Averages" which are sorted by female and male. I understand how to get the total in which to divide the problem, i just can't seem to get the sum. Anytime I try to get the sum, the value for the first course (first time through loop) is not kept so I can't figure out for the life of me how to do it. Any hints or assistance would be greatly appreciated. I know the code is long and kinda "brutish" so bear with me on that part. here's the code
//GPA calculator for Ghemri
//dealing with gpa range 0.0-4.0, set cap?
//try a do while loop
#include <iostream>
using namespace std;
int main(void)
{
int size, counter;
//int studentTotal= 0;
char gender;
double studentInfo,total,sum, avg;
double minRange = 0.0, maxRange = 4.0;
double maxGpa=0,gpaAvg,gpaSum;
double femaleSum, femaleAvg, femaleTotal;
double maleSum, maleAvg, maleTotal;
int femaleNumber,maleNumber, gpaNumber;
double sumFemaleAvg;// femaleGeneralAvg;//sumMaleAvg, maleGeneralAvg;
cout << "\nPlease enter the number of courses you want considered: ";
cin >> size;
while(size <=0)
{
cout << "\nInvalid entry, number of course must be greater than zero\n";
cin >> size;
}
//sumFemaleAvg+=femaleAvg;
for(int course =1; course <= size; course++)
{
maleTotal = 0;
femaleTotal=0;
total = 0;
femaleNumber = 0;
maleNumber = 0;
gpaNumber = 0;
maxGpa= 0;
gpaSum = 0;
//double doubleArray[course] = {femaleAvg};
cout << "\nEnter student information(0.0 O to end):\t";
cin >> studentInfo >> gender;
while(studentInfo < minRange || studentInfo > maxRange)
{
cout << "\nInvalid entry, try again...\n";
cout << "Enter student's information(0.0 O to end): \t";
cin >> studentInfo >> gender;
}
if(studentInfo > maxGpa)
{
maxGpa=studentInfo;
}
if(studentInfo > 3.00)
{
gpaSum=studentInfo;
gpaNumber=1;
}
if(gender == 'f' && studentInfo > minRange && studentInfo < maxRange)
{
femaleNumber=1;
femaleSum = studentInfo;
maleSum=0;
}
if(gender == 'm' && studentInfo > minRange && studentInfo < maxRange)
{
maleNumber=1;
maleSum = studentInfo;
femaleSum=0;
}
sum =studentInfo;
counter = 0;
counter++;
while(studentInfo != 0.0 && gender != 'O')
{
cout << "Enter student information(0.0 O to end):\t";
cin >> studentInfo >> gender;
if(studentInfo > maxGpa)
{
maxGpa=studentInfo;
}
if(studentInfo < minRange || studentInfo > maxRange)
{
cout << "\nInvalid entry, try again...\n";
cout << "Enter student's information(0.0 O to end): \t";
cin >> studentInfo >> gender;
}
if(gender != 'm' && gender !='f'&& gender != 'O')
{
cout << "Invalid entry, enter m for male or f for female\n";
cout << "Enter student's information(0.0 O to end): \t";
cin >> studentInfo >> gender;
}
sum +=studentInfo;
total+=counter;
avg = sum/total;
if(studentInfo > 3.00)
{
gpaSum+=studentInfo;
gpaNumber++;
gpaAvg= gpaSum/gpaNumber;
}
if(gender == 'f' || gender =='F')
{
femaleSum+=studentInfo;
femaleNumber++;
//femaleTotal+=femaleNumber;
femaleAvg = femaleSum/femaleNumber;
//sumFemaleAvg = femaleAvg;
}
if(gender == 'm' || gender == 'M')
{
maleSum+=studentInfo;
maleNumber++;
//maleTotal+=maleNumber;
maleAvg = maleSum/maleNumber;
}
if(studentInfo == 0 && gender == 'O')
{
cout << "\nResults for course "<< course<<":\n";
cout << "Female Student Average\t Male Student Average\n";
cout << "\t";
if(femaleNumber==0)
{
cout<< "N/A" << "\t\t\t";
}
else
{
cout<< femaleAvg <<"\t\t\t";//femaleAvg
}
if(maleNumber==0)
{
cout << "N/A\n";
}
else
{
cout<<maleAvg << endl;
//sumMaleAvg = maleAvg;
}
cout << "\nHighest GPA: " << maxGpa<<endl;
cout << "Highest average GPA for course "<< course << ": "<< gpaAvg<< endl;
}
}
sumFemaleAvg = femaleAvg;
}
/*double genAvg[]={femaleAvg};
result+=genAvg[course];*/
sumFemaleAvg+=femaleAvg;
cout<< "this is a test for the value sum " << sumFemaleAvg<<endl;
//cout<< "this is another test " << result <<endl;
//maleGeneralAvg = sumMaleAvg/course;
/*cout << "the sum is " << sumFemaleAvg<<endl;
cout << "the other sum is "<< sumFemaleAvg2<<endl;
cout << "the other other sum is " << femaleAvg;*/
return 0;
}
Try to avoid extreme repetition and factor common operations into functions. I'll "bear with you" for now, but really there's no reason I should. This is the first thing you need to learn as a programmer.
It looks like the variable sumFemaleAvg is supposed to be summed over loop iterations. However the line sumFemaleAvg = femaleAvg; overwrites the variable every time. Do
sumFemaleAvg += femaleAvg;
and likewise for other variables you wish to add up over multiple iterations.