Guys I Want Help Regarding Code Below, I Want To Add A While Loop In Which A User Keeps Getting The Else Statement's Cout. Until User Satisfies If or Else-If Condition. In Other Words The Program Should Only End When User Writes 'g''G''b''B' Other Wise It Should Keep Showing The Else's Cout With Asking Again To Put The Right Value.
#include <iostream>
using namespace std;
int main()
{
string item;
float price;
int quantity;
float total;
char experience;
cout << "Write The Name Of Item You Want To Buy:" << endl;
getline(cin, item);
cout << "What Is The Price Of " << item << " In Dollars $ ?" << endl;
cin >> price;
cout << "What Is The Quantity Of " << item << endl;
cin >> quantity;
total = price*quantity;
cout << "Your Total Bill For " << quantity << " " << item << " Is " << total << "$" << endl<< endl;
cout << "How Was Your Shopping Experience Write (G) If Good And (B) For Bad" << endl;
cin >> experience;
if (experience == 'g' || experience == 'G')
{
cout << "We Appreciate Your Feedback THANKS For Shopping :)";
}
else if (experience == 'b' || experience == 'B')
{
cout << "Sorry For Bad Experience, We Will Do Better Next Time THANKS For Shopping :)";
}
else
{
cout << "Write \"G\" If Good And \"B\" If Bad";
}
Here's the answer, you should prompt for input, then check the input using a while loop instead of IF-ELSE to make sure the user provided the answer you desired. This is actually a simple logic question tho. You may explore do-while loop too.
#include <iostream>
using namespace std;
int main()
{
string item;
float price;
int quantity;
float total;
char experience;
cout << "Write The Name Of Item You Want To Buy:" << endl;
getline(cin, item);
cout << "What Is The Price Of " << item << " In Dollars $ ?" << endl;
cin >> price;
cout << "What Is The Quantity Of " << item << endl;
cin >> quantity;
total = price*quantity;
cout << "Your Total Bill For " << quantity << " " << item << " Is " << total << "$" << endl<< endl;
cout << "How Was Your Shopping Experience Write (G) If Good And (B) For Bad" << endl;
cin >> experience;
while (experience != 'G' && experience != 'g' && experience != 'B' && experience != 'b') {
cout << "Write \"G\" If Good And \"B\" If Bad\n";
cin >> experience;
}
if (experience == 'g' || experience == 'G') {
cout << "We Appreciate Your Feedback THANKS For Shopping :)";
} else if (experience == 'b' || experience == 'B') {
cout << "Sorry For Bad Experience, We Will Do Better Next Time THANKS For Shopping :)";
}
return 0;
}
Related
Line 38 and 43
I don't see the purpose of the second parameter in the deposit() and withdrawal() functions inside the main but I need it there to run those functions. Is there a better way to do this? Any suggestion is appreciated.
int deposit(int cBalance, int dBalance)
{
cout << "How much would you like to deposit?" << endl;
cin >> dBalance;
cout << "Processing Deposit......." << endl;
cBalance += dBalance;
return cBalance;
}
int withdrawl(int cBalance, int wBalance)
{
cout << "How much would you like to withdraw?" << endl;
cin >> wBalance;
cout << "Processing Withdrawl......." << endl;
while (wBalance > cBalance)
{
cout << "NOT ENOUGH FUNDS. Try Again" << endl;
cout << "How much would you like to withdraw?" << endl;
cin >> wBalance;
}
cBalance -= wBalance;
return cBalance;
}
int main()
{
int depositAmount, withdrawAmount;
string moneyActivity, confirmation;
int currentBalance = 100;
confirmation = "YES";
while (confirmation == "YES" || confirmation == "yes")
{
cout << "Yourour current balance is: $" << currentBalance << endl;
cout << "Do you want to deposit or withdraw money? " << endl;
cin >> moneyActivity;
if (moneyActivity == "deposit")
{
currentBalance = deposit(currentBalance, depositAmount);/*depositAmount is not needed*/
cout << "Transaction Successful! Your Final Balance is: $ " << currentBalance << endl;
}
else if (moneyActivity == "withdraw")
{
currentBalance = withdrawl(currentBalance, withdrawAmount);/*withdrawAmount is not needed*/
cout << "Transaction Successful! Your Final Balance is: $ " << currentBalance << endl;
}
cout << "Would You Like To Make More Transactions? YES/NO?" << endl;
cin >> confirmation;
if (confirmation != "YES" && confirmation != "Yes" && confirmation != "yes" && confirmation != "NO" && confirmation != "No" && confirmation != "no")
{
cout << "INVALID RESPONSE. Try Again";
cin >> confirmation;
}
}
cout << "Thank you for using this service :) " << endl;
}
The 2nd parameters should not be parameters at all, they should be local variables inside of the functions.
Also, since you are making main() pass in the currentBalance and modify it upon return, it would be better to pass in the currentBalance by reference instead of by value.
Also, your main while loop could use some tweaking to reduce redundancy (and to fix a small logic hole that allows the user to enter "Yes" without actually continuing the loop).
Try something more like this:
#include <iostream>
#include <string>
#include <algorithm>
#include <limits>
#include <cctype>
using namespace std;
bool iequals(const string& a, const string& b)
{
return equal(a.begin(), a.end(),
b.begin(), b.end(),
[](unsigned char a, unsigned char b) {
return tolower(a) == tolower(b);
}
);
}
int inputNumber(const string &prompt)
{
int number;
cout << prompt << endl;
while (!(cin >> number))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "BAD INPUT. Try Again" << endl;
cout << prompt << endl;
}
return number;
}
void deposit(int &cBalance)
{
int amount = inputNumber("How much would you like to deposit?");
cout << "Processing Deposit......." << endl;
while (amount <= 0)
{
cout << "NO FUNDS SELECTED. Try Again" << endl;
amount = inputNumber("How much would you like to deposit?");
}
cBalance += amount;
}
void withdrawl(int &cBalance)
{
int amount = inputNumber("How much would you like to withdraw?");
cout << "Processing Withdrawl......." << endl;
while ((amount <= 0) || (amount > cBalance))
{
if (amount <= 0)
cout << "NO FUNDS SELECTED. Try Again" << endl;
else
cout << "NOT ENOUGH FUNDS. Try Again" << endl;
amount = inputNumber("How much would you like to withdraw?");
}
cBalance -= amount;
}
int main()
{
int currentBalance = 100;
string input;
do
{
cout << "Your current balance is: $" << currentBalance << endl;
cout << "Do you want to deposit or withdraw money? " << endl;
cin >> input;
if (iequals(input, "deposit"))
{
deposit(currentBalance);
cout << "Transaction Successful! Your Final Balance is: $ " << currentBalance << endl;
}
else if (iequals(input, "withdraw"))
{
withdrawl(currentBalance);
cout << "Transaction Successful! Your Final Balance is: $ " << currentBalance << endl;
}
cout << "Would You Like To Make Another Transaction? YES/NO?" << endl;
cin >> input;
while (!(iequals(input, "YES") || iequals(input, "NO")))
{
cout << "INVALID RESPONSE. Try Again";
cout << "Would You Like To Make Another Transaction? YES/NO?" << endl;
cin >> input;
}
}
while (iequals(input, "YES"));
cout << "Thank you for using this service :) " << endl;
}
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.
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
I'm trying to make a little game, and I'm a beginner.
The problem is the parts with the ifs and the else, where it says "Lady" and "Sir".
#include <iostream>
#include <string>
using namespace std;
int main()
{
string playerName;
int age;
int playerGender;
cout << "Are you a Lady or a Sir?" << endl;
cin >> playerGender;
if (playerGender = Lady)
{
cout << "So you're a girl. " << endl;
}
else if (playerGender = Sir)
{
cout << "So you're a boy." << endl;
}
cout << "What is your name " << playerGender << "?" << endl;
cin >> playerName;
cout << "What is your age?" << playerName << endl;
cin >> age;
if (age <= 10)
{
cout << "You're too young " << playerName << "! " << endl;
}
else if (age >= 11)
{
cout << "You are old enough to play" << playerName << ". " << endl;
That's what I have, I don't know whats wrong. Help please!
When you perform the following:
std::cin >> playerGender;
You are retrieving a number from the user. You are then assigning that number to your playerGender with the following:
if (playerGender = Lady)
You're only using a single =, which assigns the value that's in Lady to playerGender. You probably mean to write == which will compare the value of playerGender and Lady.
I can't tell what Lady and Sir are, but for your purposes you will need to ensure they are an int:
const int Lady = 0, Sir = 1;
Or an enum:
enum genders { Lady = 0, Sir = 1 };
Now you can make the following comparison:
if (playerGender == Lady)
{
}
else if (playerGender == Sir)
{
}
The first iteration will work perfectly fine. I can enter the song title, artist, and rating. However, the next time, it will display "Enter the song title" AND "Enter the artist: " so that means I cannot enter a song title the next time up. I'm sure it's a simple mistake, but I can't find it. This is C++.
#include <iostream>
using namespace std;
void printResults(double ratingOfSong);
int main(void)
{
bool getInput = true;
char song[256];
char artist[256];
cout << "Thank you for taking the time to rate different songs." << endl
<< "This will better improve our quality to better serve you." << endl
<< "Please enter song title, artist, and a rating out of 5 stars." << endl << endl << endl;
while ( getInput )
{
cout << "Enter song title - XYZ to quit: ";
cin.getline(song,256);
if ( song[0] == 'X' && song[1] == 'Y' && song[2] == 'Z')
{
getInput = false;
cout << "Thank you for taking the time in rating the songs." << endl;
break;
}
else
{
double rating = 0;
cout << "Enter artist: ";
cin.getline(artist,256);
cout << "Enter rating of song: ";
cin >> rating;
printResults(rating);
}
}
}
void printResults(double ratingOfSong)
{
if (ratingOfSong <= 1.5)
{
cout << "We are sorry you didn't like the song. Thanks for the input." << endl << endl;
}
else if (ratingOfSong > 1.5 && ratingOfSong <= 3.0)
{
cout << "We hope the next song you buy is better. Thanks for the input." << endl << endl;
}
else if (ratingOfSong > 3.0 && ratingOfSong <= 4.0)
{
cout << "We are glad that you somewhat enjoy the song. Thanks for the input." << endl << endl;
}
else if (ratingOfSong > 4.0 && ratingOfSong < 5.0)
{
cout << "We are glad that you like the song! Thanks for the input." << endl << endl;
}
else if (ratingOfSong >= 5.0)
{
cout << "A perfect score, awesome! Thanks for the input." << endl << endl;
}
}
You need to discard the new line that was left in the stream from the last input operation. Use std::cin.ignore() for that:
std::cout << "Enter song title - XYZ to quit: ";
std::cin.ignore(); /*
^^^^^^^^^^^^^^^^^^ */
std::cin.getline(song, 256);