How do I increment an integer by the user's input in C++ within a loop and keep it incremented when the loop starts over? -
this part of code aggregate taxi program 4 taxi companies. need increment each taxi company when user adds items cart , calculate cost chosen company. program needs return main menu. thanks!
#include <iostream> using namespace std; int main() { cout<<"welcome lawrence aggregation service"<< endl; //main menu int menu_select; cout<< "please select following options"<< endl << "purchase mileage vouchers from:"<< endl << "1 - checker cab"<< endl << "2 - gts lawrence"<< endl << "3 - jayhawk taxi"<< endl << "4 - redycab"<< endl << "or"<< endl << "5 - view cart"<< endl << "6 - exit" << endl; cin>> menu_select; while ((menu_select) >=1 && (menu_select) <= 6) { //user's menu choice //cab companies incrementing miles int checker_sum; int checker; int gts; int jhawk; int redyc; //taxi company selection if (menu_select >= 1 && menu_select <= 4) { int mileage; cout<< "how many miles purchase?"<< endl; cin>> mileage; //checker cab selection if (menu_select == 1) { int menu_opt; double price_flat; double price_miles; double mileage_cost; price_flat = 30.00; price_miles = 0.00; mileage_cost = mileage* price_miles + price_flat; checker_sum = (checker + mileage); cout<< "that $" <<(mileage_cost)<< " checker cab. thank you."<< endl<< endl; cout<<"you have " <<checker<< " miles checker cab."<< endl; } //gts lawrence selection else if (menu_select == 2) { int menu_opt; double price_flat; double price_miles; double mileage_cost; price_flat = 5.00; price_miles = .20; mileage_cost = mileage* price_miles + price_flat; cout<< "that $" << mileage_cost << " gts lawrence. thank you."<< endl<< endl; } else if (menu_select == 3) { int menu_opt; double price_flat; double price_miles; double mileage_cost; price_flat = 10.00; price_miles = .05; mileage_cost = mileage* price_miles + price_flat; cout<< "that $" << mileage_cost << " jayhawk taxi. thank you."<< endl<< endl; } else if (menu_select == 4) { int menu_opt; double price_flat; double price_miles; double mileage_cost; price_flat = 0; price_miles = 1.10; mileage_cost = mileage* price_miles + price_flat; cout<< "that $" << mileage_cost << " redycab. thank you."<< endl<< endl; } } } return 0; }
for starters should declare:
int checker_sum; int checker; int gts; int jhawk; int redyc;
outside while loop variables aren't declared multiple times
as increment mentioning create variable:
int totalcost;
you have declare variable somewhere in main outside of of while loops. instance:
int main() { int totalcost = 0; cout<<"welcome lawrence aggregation service"<< endl; //main menu int menu_select; cout<< "please select following options"<< endl;
you have can change value of total cost in of loops;
once have declared is:
if (menu_select == 1) { int menu_opt; double price_flat; double price_miles; double mileage_cost; price_flat = 30.00; price_miles = 0.00; mileage_cost = mileage* price_miles + price_flat; checker_sum = (checker + mileage); cout<< "that $" <<(mileage_cost)<< " checker cab. thank you."<< endl<< endl; cout<<"you have " <<checker<< " miles checker cab."<< endl; totalcost = totalcost + mileage_cost // totalcost += mileage_cost }
if in every loop (since declared variable outside loops) add payments make 1 variable. , when user wants view cart put:
else if(menu_select == 5) { //whatever code want cout << "total: " << totalcost; //whatever code want }
what want loop main menu put whole thing while loop:
#include<iostream> using namespace std; int main() { int totalcost = 0; cout<<"welcome lawrence aggregation service"<< endl; //main menu int menu_select; while(menu_select != 6) { cout<< "please select following options"<< endl << "purchase mileage vouchers from:"<< endl << "1 - checker cab"<< endl << "2 - gts lawrence"<< endl << "3 - jayhawk taxi"<< endl << "4 - redycab"<< endl << "or"<< endl << "5 - view cart"<< endl << "6 - exit" << endl; cin>> menu_select; //user's menu choice //cab companies incrementing miles int checker_sum; int checker; int gts; int jhawk; int redyc; while ((menu_select) >=1 && (menu_select) <= 6) { //taxi company selection if (menu_select >= 1 && menu_select <= 4) { int mileage; cout<< "how many miles purchase?"<< endl; cin>> mileage; //checker cab selection if (menu_select == 1) { int menu_opt; double price_flat; double price_miles; double mileage_cost; price_flat = 30.00; price_miles = 0.00; mileage_cost = mileage* price_miles + price_flat; checker_sum = (checker + mileage); cout<< "that $" <<(mileage_cost)<< " checker cab. thank you."<< endl<< endl; cout<<"you have " <<checker<< " miles checker cab." << endl; totalcost = totalcost + mileage_cost; // or totalcost += mileage_cost; } //gts lawrence selection else if (menu_select == 2) { int menu_opt; double price_flat; double price_miles; double mileage_cost; price_flat = 5.00; price_miles = .20; mileage_cost = mileage* price_miles + price_flat; cout<< "that $" << mileage_cost << " gts lawrence. thank you."<< endl<< endl; totalcost = totalcost + mileage_cost; // or totalcost += mileage_cost; } else if (menu_select == 3) { int menu_opt; double price_flat; double price_miles; double mileage_cost; price_flat = 10.00; price_miles = .05; mileage_cost = mileage* price_miles + price_flat; cout<< "that $" << mileage_cost << " jayhawk taxi. thank you."<< endl<< endl; totalcost = totalcost + mileage_cost; // or totalcost += mileage_cost; } else if (menu_select == 4) { int menu_opt; double price_flat; double price_miles; double mileage_cost; price_flat = 0; price_miles = 1.10; mileage_cost = mileage* price_miles + price_flat; cout<< "that $" << mileage_cost << " redycab. thank you."<< endl<< endl; totalcost = totalcost + mileage_cost; // or totalcost += mileage_cost; } else if(menu_select == 5) { /whatever code want cout << "total: " << totalcost; //whatever code want } } } } return 0;
}
notice how moved the: int menu_select
above while loop because if anywhere below while loop, compiler report error since using menu_select
before declared.
i hope helpful , have great time coding :).
Comments
Post a Comment