How to use a menu - c++

#include <iostream>
using namespace std;
int main()
{
int food, product, total;
double price;
cout << " Welcome To Maggie’s Shopping Cart Calculator!" << endl;
char option; // user's entered option will be saved in this variable
do{
//Displaying Options for the menu
cout << " 1) Please add an item to your cart " << endl;
cout << " 2) Final Total Amount" << endl;
cout << " 3) Quit the program" << endl; //Prompting user to enter an option according to menu
cout << " Please select an option : ";
cin >> option; // users option
total = price;
if (option == 1) // Checking if user selected option 1
{
cout << " Please enter item and the price : " << endl;
cin >> food;
cin >> price;
cout << " Total amount so far: " << price << endl;
}
else if (option == 2)
cout << "Total Amount so Far: " << total << endl;
else if (option == 3) // Checking if user selected option 2
{
cout << " End Shopping Cart! " << endl;
}
}
while (option != 3);
system("pause");
return 0;
}
}
This is my code that I have, I just continues to repeat the menu, even when I select an option help! The assignment is to have three options for the user to choose, and the menu should be repeated after choice 1 was selected. My code isn't letting me enter in any information.

modified ur code , check it out
#include <iostream>
#include <string>
using namespace std;
int main()
{
int product, total=0;int flag =1;string food;
double price;
cout << " Welcome To Maggie’s Shopping Cart Calculator!" << endl;
int option; // user's entered option will be saved in this variable
do{
//Displaying Options for the menu
cout << " 1) Please add an item to your cart " << endl;
cout << " 2) Final Total Amount" << endl;
cout << " 3) Quit the program" << endl; //Prompting user to enter an option according to menu
cout << " Please select an option : ";
cin >> option; // users option
if (option == 1) // Checking if user selected option 1
{
cout << " Please enter item and the price : " << endl;
cin >> food;
cin >> price;total = total + price ;
cout << " Total amount so far: " << total << endl;
}
else if (option == 2)
cout << "Total Amount so Far: " << total << endl;
else if (option == 3) // Checking if user selected option 2
{
cout << " End Shopping Cart! " << endl;
flag=0 ;
}
}
while ( flag== 1);
return 0;
}
hope this is helpful

You defined the option variable as char, while you compare it as int later on.. Try to change its definition to int

Related

Program cannot run due to no operator "<<" matches these operands

I get an error of "no operator matches these operands" when using the part of the code outputFile << customerName << "your Monthly payments are " << monthlyPay << endl;. Overall, I need the code to Add the ability to save data to disk in one or more files and a menu should give the user the option to save or retrieve data. I have not gotten past the error to properly run the program. Can you please help fix error.
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <vector>
#include<fstream>
using namespace std;
void menu(void);
void writeData(void);
void readData(void);
const char FileName[] = "CourseProjectAvilaF.txt";
//Variables
vector <double> Loanlgth, Loanamt, interestRate, totalInterest;
vector <double> monthlyPay(100), loanTotal(100), creditScore(100);
vector <string> customerName;
int main()
{
menu();
return 0;
}
void menu(void)
{
const int INPUT_CUSTOMER = 1, DISPLAY_LOAN = 2, EXIT_PROGRAM = 3;
int option;
//Program
cout << "Thank you for choosing The Bank of UA for your loan requirements!\n\n";
do
{
cout << "UA Bank menu:\n\n"
<< "1. Enter your information\n"
<< "2. See your loan requirements\n"
<< "3. Exit program\n\n"
<< "Choose an option: ";
cin >> option;
while (option < INPUT_CUSTOMER || option > EXIT_PROGRAM)
{
cout << "Please enter a valid menu option: ";
cin >> option;
}
if (option == 1)
{
writeData();
}
if (option == 2)
{
readData();
}
} while (option != EXIT_PROGRAM);
}
//function to read customer information
void writeData(void)
{
fstream outputFile;
outputFile.open(FileName, fstream::app);
int index;
int numCustomers = 0;
cout << "Please enter the number of customers you would like\n"
<< " to enter loan information for: ";
cin >> numCustomers;
for (index = 0; index < numCustomers; index++)
{
string tempName;
double tempLoanamt, tempLoanlgth, tempcreditScore, tempinterestRate,
tempinterest;
cout << "Please enter your name: ";
cin >> tempName;
customerName.push_back(tempName);
cout << "Please enter the loan amount: $";
cin >> tempLoanamt;
Loanamt.push_back(tempLoanamt);
cout << "Please enter the length of the loan in months: ";
cin >> tempLoanlgth;
Loanlgth.push_back(tempLoanlgth);
cout << "What is your current credit score? ";
cin >> tempcreditScore;
creditScore.push_back(tempcreditScore);
//This will determine interest rate and overall loan amount when calculated
if (tempcreditScore <= 650)
tempinterestRate = .12;
else
tempinterestRate = .05;
interestRate.push_back(tempinterestRate);
//Calculations
tempinterest = Loanamt[index] * interestRate[index];
totalInterest.push_back(tempinterest);
loanTotal[index] = (Loanamt[index] + totalInterest[index]);
monthlyPay[index] = loanTotal[index] / Loanlgth[index];
// Out put files to write data to be saved
outputFile << customerName << "your Monthly payments are " << monthlyPay << endl;
outputFile << "Your total interest is " << totalInterest << endl;
outputFile << "You owe " << loanTotal << endl;
outputFile << "You have " << Loanlgth << " months to pay off your balance" << endl;
}
outputFile.close(); //Close file
}
//function loan information
void readData(void)
{
int index;
int numCustomers = 0;
ifstream inputFile;
inputFile.open(FileName, fstream::in);//Open the file with read mode
//Display monthly payment
cout << fixed << setprecision(2);
for (index = 0; index < numCustomers; index++)
{
cout << customerName[index] << " your total loan is " << loanTotal[index]
<< "\n"
<< "with a monthly payment of $" << monthlyPay[index] << "\n"
<< "for " << Loanlgth[index] << " months with an interest\n"
<< "rate of " << interestRate[index] << endl;
}
}
It's simple enough, you got it right everywhere else in your program.
When you want to access a particular element of a vector you use an index. Like this
outputFile << customerName[index] << "your Monthly payments are " << monthlyPay[index] << endl;
outputFile << "Your total interest is " << totalInterest[index] << endl;
outputFile << "You owe " << loanTotal[index] << endl;
outputFile << "You have " << Loanlgth[index] << " months to pay off your balance" << endl;
customerName and monthlyPay are vectors. You can't stream them directly. Instead you can do something like
for (auto const &name : customerName)
outputFile << name;

how to add all in all the total in loop?

It is me again. I want to add the prices in the purchase if I want to buy more items. But I do not know how to do that. For example if, I confirm my purchase and want to buy more items, I want it to add the prices that is confirmed to purchase, and if I finally do not want to buy more items and not to look for more items, that total price would be computed.
int main()
{
int choice;
int purchase;
int quantity;
double totalChoice1;
double totalChoice2;
char view;
char confirm;
char buyMore;
char look;
double alloy, apex, kraken, aorus;
double oppo, alpha, rog, huawei;
double ps4, nintendo, xbox, wii;
alloy = 69.99;
apex = 199;
kraken = 90;
aorus = 60;
do {
cout << "What type of items would you like to view?" << endl;
cout << " [1] Peripherals" << endl;
cout << " [2] Mobile Phones" << endl;
cout << " [3] Consoles" << endl;
cout << " [4] Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1) {
cout << "--------------------" << endl;
cout << "What peripherals would you like to purchase?" << endl;
cout << "[1] HyperX Alloy FPS PRO - $69.99" << endl;
cout << "[2] SteelSeries APEX PRO - $199" << endl;
cout << "[3] Razer Kraken X - $90" << endl;
cout << "[4] AORUS K7 - $60" << endl;
cout << "[5] BACK TO MENU" << endl;
cout << "Enter your choice: ";
cin >> purchase;
cout << "--------------------" << endl;
if (purchase == 1) {
cout << "How many would you like to purchase? ";
cin >> quantity;
totalChoice1 = quantity * alloy;
cout << "The total price for that is " << totalChoice1 << endl;
cout << "Confirm the Purchase? [Y]/[N]: ";
cin >> confirm;
if (confirm == 'Y') {
totalChoice1; // This is just a trial code.
cout << "Would you like to buy more items? [Y]/[N]: ";
cin >> buyMore;
}
else if (confirm == 'N') {
cout << "Do you still want to look for items? [Y]/[N]: ";
cin >> look;
if (look == 'N') {
break;
}
}
else {
break;
}
}
}
while (purchase == 5 || buyMore == 'Y' || look == 'Y');
cout << "The total price for your items is: " << totalChoice1; // This is also a trial code (totalChoice1)
}
You are simply missing a variable to keep track of that total. Don't forget to give it an initial value of 0!
There are plenty of minor other issues with your code, so you'll have some more learning ahead. For instance, we find it easier to do bookkeeping in cents, because you can treat them as integers.

most of the code doesn't show up only the welcome message and "enter pin"?

Assignment:
When you enter an incorrect number of digits( 3 or 5 digits pin number) a
message should display “You have entered the incorrect pin number!!...you
must enter a four digits pin number.”
By showing the message that was display previously the program should
allow you to re-enter the correct number of digits.
The number of attempts should be three times.
When you have entered the correct number of digits a message should display
“Your pin has been accepted!!”
Each time you enter any amount of pin number it must show in asterisk.
Code:
#include <conio.h>
#include <iostream>
//#include <cstdlib.h>
#include <cmath>
using namespace std;
int main() {
string pass = "";
int attempts = 3;
cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
cout << "< Welcome to The Bank >" << endl;
cout << "< >" << endl;
cout << "< Please Enter Pin Below >" << endl;
cout << " ^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
cout << "\nEnter Pin Number: " << endl;
cin >> pass;
// attempts= getch();
while (attempts <= 3) {
cout << "*";
getch();
attempts++; // take this out and it display to infinity
// }
if (pass == "1718") {
cout << " lOGIN IN..." << endl << endl;
attempts = -1;
}
else {
cout << "\nWRONT PIN-TRY AGAIN: " << endl << endl;
attempts--;
cout << " REMAINING ATTEMPTS: " << attempts << endl << endl;
}
if (attempts == 0) {
cout << "Exceed the Pin attempts. Try Later. " << endl << endl;
}
if (attempts == -1) {
cout << "********************************" << endl;
cout << "* Welcome to Magali's Bank *\n";
cout << "* Select Option: *\n";
cout << "* 1. Check Balance *\n";
cout << "* 2. Withdraw *\n";
cout << "* 3. Deposit *\n";
cout << "* 4. Exit *\n";
cout << "********************************\n";
int balance = 500;
float withdraw;
float deposit;
int user;
cout << "Enter Number: ";
cin >> user;
while (user != 4) {
switch (user) {
case 1:
cout << " Your balance is: " << balance << endl;
break;
case 2:
cout << "Enter the amount you want withdraw: ";
cin >> withdraw;
balance = balance - withdraw;
break;
case 3:
cout << "Enter the amount you want to deposit: ";
cin >> deposit;
balance = balance + deposit;
break;
default:
cout << " Need to type 1 for Balance, 2 to Withdraw, 3 to Deposit "
"and 4 to Exit. ";
}
cout << "Enter Number: ";
cin >> user;
}
cout << "\nTHANKS FOR USING THE SYSTEM!\n";
}
}
return 0;
}
Your code has lots of inefficiencies but as you requested. I'm only debugging only a part of it where you need to take input and display error.
string pass="";
int attempts=3;
const string pin = "1718";
//DO whatever you want
//while loop to take input
while (attempts >0)
{
cout << "\nEnter Pin Number: " << endl;
cin >> pass;
for(auto i : pass)
cout<<"*";
if(pass == pin){
attempts = 3;
cout<<"Your Pin has been accepted.\n";
break;}
else{
cout <<"\nWRONT PIN-TRY AGAIN: " << endl << endl;
attempts--;
cout << " REMAINING ATTEMPTS: " << attempts << endl << endl;
}
}
if (attempts == 0)
{
cout << "Exceed the Pin attempts. Try Later. "<< endl << endl;
cout<<"Your account has been locked for a day .\n";
exit(0);
}
Comment out if you didn't understand any part.

Cin not producing required results

i'm trying to set up a simple flight booking system program, but my second bit of cin code is not calling for input when i run the program. Unlike the initial cin that requires your name initially. the program just runs and returns 0. I'm a beginner at c++ and i know this is a simple fix so please be understanding . Thank you any guidance will be greatly appreciated.
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
int name;
int Seatnumber;
int optionnumber = 1-5 ;
std::string out_string;
std::stringstream ss;
ss << optionnumber;
out_string = ss.str();
cout << "Welcome to the COS1511 Flight Booking System" "\n" << endl;
cout << "Enter full name : " << endl;
cin >> name ; "\n";
cout << "\n" "The Available travel times for flights are:" << endl;
cout << " Depart Arrive" << endl;
cout << "1. 7.00 9.30" << endl;
cout << "2. 9.00 11.30" << endl;
cout << "3. 11.00 13.30" << endl;
cout << "4. 13.00 15.30" << endl;
cout << "5. 15.00 17.30" << endl;
cout << "Choose the time by entering the option number from the displayed list : " << endl;
cin >> optionnumber ;
if (optionnumber == 1-5){
cout << "\n" "The available seats for are as follows " << endl;
}
else
cout << "Incorrect option! Please Choose from 1-5 " << endl;
cout << "First Class(1920)" << endl;
cout << "|A1||A2||A3|----|A4||A5||A6|" << endl;
cout << "|B1||B2||B3|----|B4||B5||B6|" << endl;
cout << "|C1||C2||C3|----|C4||C5||C6|" << endl;
cout << "|D1||D2||D3|----|D4||D5||D6|" << endl;
cout << "| Economy Class(1600)" << endl;
cout << "|E1||E2||E3|----|E4||E5||E6|" << endl;
cout << "|F1||F2||F3|----|F4||F5||F6|" << endl;
cout << "|G1||G2||G3|----|G4||G5||G6|" << endl;
cout << "|H1||H2||H3|----|H4||H5||H6|" << endl;
cout << "|I1||I2|" << endl;
cout << "Please Key in a seat number to choose a seat(eg: A2)" << endl;
cin >> Seatnumber;
}
prompt the user to enter their name.
Then display a menu showing the available times for the flight.
the user can choose a preferred departure time(option 1-5)
the option selected should be validated for 1-5
if the user entered the correct time a seating arrangement for that particular flight time should be displayed to the next user for the user to choose a seat.
Warning
int optionnumber = 1-5 ;
does
int optionnumber = -4 ;
and
if (optionnumber == 1-5){
does
if (optionnumber == -4){
but you wanted if ((optionnumber >= 1) && (optionnumber <= 5))
if the user entered the correct time a seating arrangement for that particular flight time should be displayed to the next user for the user to choose a seat.
No, whatever the result of the test above you continue and write "First Class(1920)" etc so even when the choice is invalid
in
cin >> name ; "\n";
what did you expect about the "\n" ?
I encourage you to check the read success, currently if the user does not enter an integer you do not know that
But are you sure the name must be an integer ? Probably it must be s string
out_string is unused, it can be removed
Visibly Seatnumber is not an int but a string (A1 ...)
you probably want to loop until a valid time is enter, also fixing the other problems a solution can be :
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
string Seatnumber;
int optionnumber;
cout << "Welcome to the COS1511 Flight Booking System" "\n" << endl;
cout << "Enter full name : " << endl;
if (!(cin >> name))
// EOF (input from a file)
return -1;
cout << "\n" "The Available travel times for flights are:" << endl;
cout << " Depart Arrive" << endl;
cout << "1. 7.00 9.30" << endl;
cout << "2. 9.00 11.30" << endl;
cout << "3. 11.00 13.30" << endl;
cout << "4. 13.00 15.30" << endl;
cout << "5. 15.00 17.30" << endl;
cout << "Choose the time by entering the option number from the displayed list : " << endl;
for (;;) {
if (!(cin >> optionnumber)) {
// not an int
cin.clear(); // clear error
string s;
// flush invalid input
if (!(cin >> s))
// EOF (input from a file)
return -1;
}
else if ((optionnumber >= 1) && (optionnumber <= 5))
// valid choice
break;
cout << "Incorrect option! Please Choose from 1-5 " << endl;
}
cout << "\n" "The available seats for are as follows " << endl;
cout << "First Class(1920)" << endl;
cout << "|A1||A2||A3|----|A4||A5||A6|" << endl;
cout << "|B1||B2||B3|----|B4||B5||B6|" << endl;
cout << "|C1||C2||C3|----|C4||C5||C6|" << endl;
cout << "|D1||D2||D3|----|D4||D5||D6|" << endl;
cout << "| Economy Class(1600)" << endl;
cout << "|E1||E2||E3|----|E4||E5||E6|" << endl;
cout << "|F1||F2||F3|----|F4||F5||F6|" << endl;
cout << "|G1||G2||G3|----|G4||G5||G6|" << endl;
cout << "|H1||H2||H3|----|H4||H5||H6|" << endl;
cout << "|I1||I2|" << endl;
cout << "Please Key in a seat number to choose a seat(eg: A2)" << endl;
cin >> Seatnumber;
return 0;
}
Compilation and execution :
pi#raspberrypi:/tmp $ g++ -pedantic -Wextra -Wall cc.cc
pi#raspberrypi:/tmp $ ./a.out
Welcome to the COS1511 Flight Booking System
Enter full name :
bruno
The Available travel times for flights are:
Depart Arrive
1. 7.00 9.30
2. 9.00 11.30
3. 11.00 13.30
4. 13.00 15.30
5. 15.00 17.30
Choose the time by entering the option number from the displayed list :
aze
Incorrect option! Please Choose from 1-5
7
Incorrect option! Please Choose from 1-5
2
The available seats for are as follows
First Class(1920)
|A1||A2||A3|----|A4||A5||A6|
|B1||B2||B3|----|B4||B5||B6|
|C1||C2||C3|----|C4||C5||C6|
|D1||D2||D3|----|D4||D5||D6|
| Economy Class(1600)
|E1||E2||E3|----|E4||E5||E6|
|F1||F2||F3|----|F4||F5||F6|
|G1||G2||G3|----|G4||G5||G6|
|H1||H2||H3|----|H4||H5||H6|
|I1||I2|
Please Key in a seat number to choose a seat(eg: A2)
qsd
pi#raspberrypi:/tmp $
Note entering the name with cin >> name does not allow it to contain several names separated by a space, to allow composed name getline can be used

How can I use arrays to ask my customers the quantity they want and print a receipt from the arrays?

I need to create arrays that save the quantity of the item the user selects and also prints out a receipt with the product, quantity and the total price. Please help me understand how to do this. I've got a basic understanding of what an array is. I just couldn't figure out how to save the users input.
#include <iostream>
#include <Windows.h>
#include <cstdlib>
#include <string>
#include "customerclass.h"
using namespace std;
//***** Functions to calculate the price of multiple items *****
void finalPrice1(int itemQuantity) {
float price;
price = itemQuantity * 3.00;
cout << "Your total is $" << price << endl;
cout << "Thank you for using my shop" << endl;
exit(0);
}
void finalPrice2(int itemQuantity) {
float price;
price = itemQuantity * 2.50;
cout << "Your total is $" << price << endl;
cout << "Thank you for using my shop" << endl;
exit(0);
}
void finalPrice3(int itemQuantity) {
float price;
price = itemQuantity * 1.25;
cout << "Your total is $" << price << endl;
cout << "Thank you for using my shop" << endl;
exit(0);
} //***** End of functions that calculate price of multiple items *****
int main(void)
{
char selection = ' ';
string lname = "";
string luserAddress;
int itemQuantity;
string orderFinalized;
CustomerInfo myCustomerInfo;
do
{ // Displaying menu
cout << "Hello, welcome to my online shop! What is your name? " << endl;
cin >> lname;
cout << " And what is your shipping address? " << endl;
cin >> luserAddress;
myCustomerInfo.setName(lname);
myCustomerInfo.setAddress(luserAddress);
cout << lname + ", nice to meet you. Here are the items in my shop followed by the price, please enter the number that corresponds to the item you want. \n " << endl;
cout << "Products \n";
cout << "1 - Chocolate candy bar - $3.00" << endl;
cout << "2 - Sour hard candy - $2.50" << endl;
cout << "3 - Mints - $1.25" << endl;
cout << "4 - Exit" << endl << endl;
cout << "Enter selection ";
// Reading User Selection
cin >> selection;
switch (selection)
{
case '1':
cout << "You've chosen a Chocolate candy bar. How many would you like? ";
cin >> itemQuantity;
cout << "Ok, will this finalize your order? Type and enter either 'Yes' or 'No' " << endl;
cin >> orderFinalized;
if (orderFinalized == "Yes" || orderFinalized == "yes" || orderFinalized == "YES") {
cout << myCustomerInfo.getName() + " your items will be shipped to " << myCustomerInfo.getAddress() << endl;
cout << "Printing your receipt now..." << endl;
finalPrice1(itemQuantity);
}
break;
case '2':
cout << "You've chosen Sour hard candy. How many would you like? ";
cin >> itemQuantity;
cout << "Ok, will this finalize your order? Type and enter either 'Yes' or 'No' " << endl;
cin >> orderFinalized;
if (orderFinalized == "Yes" || orderFinalized == "yes" || orderFinalized == "YES") {
cout << myCustomerInfo.getName() + " your items will be shipped to " << myCustomerInfo.getAddress() << endl;
cout << "Printing your receipt now..." << endl;
finalPrice2(itemQuantity);
}
break;
case '3':
cout << "You've chosen Mints. How many would you like? ";
cin >> itemQuantity;
cout << "Ok, will this finalize your order? Type and enter either 'Yes' or 'No' " << endl;
cin >> orderFinalized;
if (orderFinalized == "Yes" || "yes" || "YES") {
cout << myCustomerInfo.getName() + " your items will be shipped to " << myCustomerInfo.getAddress() << endl;
cout << "Printing your receipt now..." << endl;
finalPrice3(itemQuantity);
}
break;
case '4':
cout << "Thank you for using my shop. <exiting now...>" << endl;
break;
default: cout << "Invalid selection. Please try again";
}
cout << endl << endl;
} while (selection != '4');
return 0;
}
You need dynamic array. For example:
cin >> itemQuantity;
// create a array during runtime
// and the size is itemQuantity
// you can access the ith array element by items[i]
Item *items= new Item[itemQuantity];
Or you can use the vector,
vector<Item> items;//you can also access the ith element by items[i]
items.push_back(hard_candy);//items = {hard_candy}
items.push_back(soft_candy);//items = {hard_candy, soft_candy}
items.pop_back();//items = {hard_candy}
BTW, the case 3 in your code has some error:
orderFinalized == "Yes" || "yes" || "YES"//wrong
orderFinalized == "Yes" || orderFinalized == "yes" || orderFinalized == "YES"//right