Im trying to figure out why the program runs in a loop. I just can't see how it starts the loop. would like to give you more info but just run the code and do everything right and the program just starts back over but not at the beginning. It will start from when I am selecting the payment option.
here is the code and the code is c++:
#include <iostream>
#include <functional>
using namespace std;
int option, pause, payment, selection, zipcode, error, pin, quantity_chips, quantity_milk, quantity_cola,
quantity_coffee, quantity_pennies, quantity_nickels, quantity_dimes, quantity_quarters, quantity_dollars,
quantity_fives, quantity_tens, quantity_twenties, quantity_total;
float chips, milk, cola, coffee, total, tax, final_cost, pennies, nickels, dimes, quarters,
dollars, fives, tens, twenties, cash_total, owed, change;
void checkout(), debitcard (), creditcard (), cash (), receipt ();
void menu ()
{
cout << "Welcome to Kenjin Xer0's Item Pooper.\n"
<< "10 or less items.\n";
do
{
quantity_total = (quantity_chips) + (quantity_milk) + (quantity_cola) + (quantity_coffee);
cout << "Select from our menu: Selected\n"
<< "\t1. Potato Chip.....$1.50......" << quantity_chips << "\n"
<< "\t2. 2% Milk.........$2.00......" << quantity_milk << "\n"
<< "\t3. Off Brand Cola..$1.00......" << quantity_cola << "\n"
<< "\t4. Dark Coffee.....$2.50......" << quantity_coffee << "\n"
<< "\t5. Check out Total:" << quantity_total << "\n";
cout << "Enter your option: ";
cin >> option;
if (option == 1)
{
cout << "Quantity: ";
cin >> quantity_chips;
chips = (1.5 * quantity_chips);
}
else if (option == 2)
{
cout << "Quantity: ";
cin >> quantity_milk;
milk = (2 * quantity_milk);
}
else if (option == 3)
{
cout << "Quantity: ";
cin >> quantity_cola;
cola = (1 * quantity_cola);
}
else if (option == 4)
{
cout << "Quantity: ";
cin >> quantity_coffee;
coffee = (2.5 * quantity_coffee);
}
else if (option == 5)
{
if (total > 10)
{
cout << "Problem! 10 or less line, Man!\n";
option = 0;
}
else
{
checkout();
}
}
else
{
cout << "Invalid option.\n";
}
}
while (option !=5);
}
void checkout ()
{
do
{
cout << "Select payment option (1:Debit 2:Credit 3:Cash): ";
cin >> payment;
if (payment == 1)
{
debitcard ();
}
else if (payment == 2)
{
creditcard ();
}
else if (payment == 3)
{
cash ();
}
else
{
cout << "Weird Choice, try again.\n";
}
}
while (payment != 1||2||3);
}
void debitcard ()
{
error = 3;
do
{
error--;
cout << "Enter PIN:\n";
cin >> pin;
if (error == 0)
{
cout << "We are sorry but does this card acually belong to you.\n"
<< "Now Leave.\n";
break;
}
else if (pin != 0000)
{
cout << "Wrong. Try Again\n"
<< "You now have " << error << " more tries\n";
}
else
{
receipt();
}
}
while (pin != 0000);
}
void creditcard ()
{
error = 3;
do
{
error--;
cout << "Enter Zip:\n";
cin >> zipcode;
if (error == 0)
{
cout << "We are sorry but does this card acually belong to you.\n"
<< "Now Leave.\n";
break;
}
else if (zipcode != 77523)
{
cout << "Wrong. Try Again\n"
<< "You now have " << error << " more tries\n";
}
else
{
receipt();
}
}
while (zipcode != 77523);
}
void cash ()
{
total = (chips) + (milk) + (cola) + (coffee);
tax = (total * .10);
final_cost = tax + total;
cout << "You owe $"<< final_cost <<".\n";
do
{
cash_total = (pennies) + (nickels) + (dimes) + (quarters) + (dollars) + (fives) + (tens) + (twenties);
owed = final_cost - cash_total;
cout << "Select the Amount:\n"
<< "\t1. Penny................$0.01......" << quantity_pennies << "\n"
<< "\t2. Nickel...............$0.05......" << quantity_nickels << "\n"
<< "\t3. Dime.................$0.10......" << quantity_dimes << "\n"
<< "\t4. Quarter..............$0.25......" << quantity_quarters << "\n"
<< "\t5. One Dollar Bill......$1.00......" << quantity_dollars << "\n"
<< "\t6. Five Dollars Bill....$5.00......" << quantity_fives << "\n"
<< "\t7. Ten Dollar Bill......$10.00....." << quantity_tens << "\n"
<< "\t8. Twenty Dollar Bill...$20.00....." << quantity_twenties << "\n"
<< "\t9. Cash Out\n"
<< "Cash you Have: $" << cash_total << " Still owe: $" << owed << "\n";
cout << "Enter your selection: ";
cin >> selection;
if (selection == 1)
{
cout << "Quantity: ";
cin >> quantity_pennies;
pennies = (0.01 * quantity_pennies);
}
else if (selection == 2)
{
cout << "Quantity: ";
cin >> quantity_nickels;
nickels = (0.05 * quantity_nickels);
}
else if (selection == 3)
{
cout << "Quantity: ";
cin >> quantity_dimes;
dimes = (0.10 * quantity_dimes);
}
else if (selection == 4)
{
cout << "Quantity: ";
cin >> quantity_quarters;
quarters = (0.25 * quantity_quarters);
}
else if (selection == 5)
{
cout << "Quantity: ";
cin >> quantity_dollars;
dollars = (1.00 * quantity_dollars);
}
else if (selection == 6)
{
cout << "Quantity: ";
cin >> quantity_fives;
fives = (5.00 * quantity_fives);
}
else if (selection == 7)
{
cout << "Quantity: ";
cin >> quantity_tens;
tens = (10.00 * quantity_tens);
}
else if (selection == 8)
{
cout << "Quantity: ";
cin >> quantity_twenties;
twenties = (20.00 * quantity_twenties);
}
else if (selection == 9)
{
receipt ();
}
else
{
cout << "Invalid option.\n";
}
}
while (selection != 9);
}
void receipt ()
{
total = (chips) + (milk) + (cola) + (coffee);
tax = (total * .10);
final_cost = tax + total;
change = owed * -1;
cout << "Receipt Take:\n";
if (quantity_chips > 0)
{
cout << "Potato Chips: $1.50 x " << quantity_chips << " = $" << chips << endl;
}
if (quantity_milk > 0)
{
cout << "2% Milk: $2.00 x " << quantity_milk << " = $" << milk << endl;
}
if (quantity_cola > 0)
{
cout << "Off Brand Cola: 1.00 x " << quantity_cola << " = $" << cola << endl;
}
if (quantity_coffee > 0)
{
cout << "Dark Coffee: $2.50 x " << quantity_coffee << " = $" << coffee << endl;
}
cout << "Tax (10.0%): $" << tax << endl;
cout << "Total: $" << final_cost << endl;
cout << "Change Returned: $" << change << endl;
}
int main ()
{
menu ();
return 0;
}
in ur checkout function
replace
while (payment != 1||2||3)
by
while (payment != 1 && payment != 2 && payment != 3)
The while loop that you have in checkout() is incorrect. You have:
while (payment != 1||2||3);
Which is not how you do mutiple or's. You would need to write it as
while (payment != 1 || payment != 2 || payment != 3);
To remove the confusing set of conditions that you've done to exit the do-while loop in the checkout function, this can be done instead:
void checkout ()
{
bool choice_made;
do
{
choice_made = true;
cout << "Select payment option (1:Debit 2:Credit 3:Cash): ";
cin >> payment;
if (payment == 1)
{
debitcard ();
}
else if (payment == 2)
{
creditcard ();
}
else if (payment == 3)
{
cash ();
}
else
{
cout << "Weird Choice, try again.\n";
choice_made = false;
}
}
while (!choice_made);
}
You only exit the loop until a valid choice is made. A simple boolean is all you need to control the logic.
As to your attempt, this line:
while (payment != 1||2||3)
applies the logical or to the values of 1, 2, and 3. The result is always true, which is 1. So what it all boils down to is
while (payment != 1)
Related
I have an assignment to write a C++ program for basic atm functioning. There is a rule to be followed that in one day, a max of 50,000 cash can flow through the atm, that includes all cash transfers, withdrawals or fast cash. I put a while loop over the whole body which uses a "atmlimit" integer to test the condition. Every successful cash transaction gets added into it but the loop is not working for some reason.
#include <iostream>
using namespace std;
int main()
{
int pincode, pindigit1;
int availbalance, userchoice;
int transmoney, inputFC, ubinput;
int cwmoney, ctmoney, spam;
int atmlimit;
cout << "\n Please enter your 4-digit pin code \n Enter 'break' at any point to close the process \n" << endl;
cin >> pincode;
pindigit1 = pincode;
for (pindigit1; pindigit1 >= 10; pindigit1 /= 10);
availbalance = 10000 * pindigit1;
while (atmlimit <= 50000) {
menu:
cout << "\n *******************************************************************************" << endl;
cout << "\n * Please choose the service your require by entering its respective number *" << endl;
cout << "\n * [1] Fast Cash *" << endl;
cout << "\n * [2] Cash Withdrawl *" << endl;
cout << "\n * [3] Balance Inquiry *" << endl;
cout << "\n * [4] Cash Transfer *" << endl;
cout << "\n ******************************************************************************* \n" << endl;
cin >> userchoice;
if (userchoice == 1) {
ta:
cout << "\n Choose the amount of money you want to send by entering its respective number or enter 0 to go back to menu" << endl;
cout << "\n [1] 500Rs [2] 1000Rs" << endl;
cout << "\n [3] 2000Rs [4] 3000Rs" << endl;
cout << "\n [5] 4000Rs [6] 5000Rs" << endl;
cout << "\n [7] 10000Rs [8] 20000Rs \n" << endl;
cin >> inputFC;
if (inputFC == 8) {
transmoney = 20000;
}
if (inputFC == 7) {
transmoney = 10000;
}
if (inputFC == 6) {
transmoney = 5000;
}
if (inputFC == 5) {
transmoney = 4000;
}
if (inputFC == 4) {
transmoney = 3000;
}
if (inputFC == 3) {
transmoney = 2000;
}
if (inputFC == 2) {
transmoney = 1000;
}
if (inputFC == 1) {
transmoney = 500;
}
if (inputFC == 0) {
goto menu;
}
if (transmoney <= availbalance) {
cout << "\n Your transaction completed successfully" << endl;
cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl;
atmlimit += transmoney;
availbalance -= transmoney;
goto menu;
}
if (transmoney > availbalance) {
cout << "\n You have insufficient balance to perform this transaction. Enter '0' to go back the menu or '1' to change the transaction amout" << endl;
cin >> ubinput;
}
if (ubinput == 0) {
goto menu;
}
if (ubinput == 1) {
goto ta;
}
}
if (userchoice == 2) {
cwmenu:
cout << "\n Please enter the amount of cash you want to withdraw from your bank account \n Enter 0 if you want to return to the menu \n" << endl;
cin >> cwmoney;
cout << cwmoney; //TBR
if (cwmoney == 0) {
goto menu;
}
if (cwmoney <= availbalance) {
cout << "\n Your transaction bas been processed, Thank you!\n" << endl;
cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl;
atmlimit += cwmoney;
availbalance -= cwmoney;
goto menu;
}
else if (cwmoney > availbalance) {
cout << "\n You have insufficient balance for this transaction. \n" << endl;
goto cwmenu;
}
}
if (userchoice == 3) {
cout << "\n Your current bank balance is " << availbalance << "Rs" << endl;
cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl;
goto menu;
}
if (userchoice == 4) {
ctmenu:
cout << "\n Enter the receiver's 6 digit bank account number \n Enter 0 to return to the menu" << endl;
cin >> spam;
if (spam == 0) {
goto menu;
}
cout << "\n Please enter the amount of cash you want to transfer/send \n Enter 0 if you want to return to the menu \n" << endl;
cin >> ctmoney;
if (ctmoney == 0) {
goto menu;
}
cout << "\n" << endl;
cout << ctmoney << endl; //TBD
if (ctmoney <= availbalance) {
cout << "\n Your transaction bas been processed, Thank you!\n" << endl;
cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl;
atmlimit += availbalance;
availbalance -= ctmoney;
goto menu;
}
else if (ctmoney > availbalance) {
cout << "\n You have insufficient balance for this transaction. \n" << endl;
goto ctmenu;
}
}
}
cout << "\n You have reached the maximum amount of cash flow through your account, Please try later \n" << endl;
}
After all the suggestions, I re-wrote the code like this:
#include <iostream>
using namespace std;
int main()
{
int pincode, pindigit1;
int availbalance, userchoice;
int transmoney, inputFC;
int cwmoney, ctmoney;
int atmlimit = 0;
cout << "\n Please enter your 4-digit pin code \n Enter 'break' at any point to close the process \n" << endl;
cin >> pincode;
pindigit1 = pincode;
for (pindigit1 = pincode; pindigit1 >= 10; pindigit1 /= 10)
;
cout << pindigit1 << endl; //TBR
availbalance = 10000 * pindigit1;
cout << availbalance << endl; //TBR
while (atmlimit <= 50000) {
cout << "\n *******************************************************************************" << endl;
cout << "\n * Please choose the service you require by entering its respective number *" << endl;
cout << "\n * [1] Fast Cash *" << endl;
cout << "\n * [2] Cash Withdrawl *" << endl;
cout << "\n * [3] Balance Inquiry *" << endl;
cout << "\n * [4] Cash Transfer *" << endl;
cout << "\n ******************************************************************************* \n" << endl;
cin >> userchoice;
switch (userchoice) {
case 1:
cout << "\n Choose the amount of money you want to send by entering its respective number or enter 0 to go back to menu" << endl;
cout << "\n [1] 500Rs [2] 1000Rs" << endl;
cout << "\n [3] 2000Rs [4] 3000Rs" << endl;
cout << "\n [5] 4000Rs [6] 5000Rs" << endl;
cout << "\n [7] 10000Rs [8] 20000Rs \n" << endl;
cin >> inputFC;
if (inputFC == 8) {
transmoney = 20000;
}
if (inputFC == 7) {
transmoney = 10000;
}
if (inputFC == 6) {
transmoney = 5000;
}
if (inputFC == 5) {
transmoney = 4000;
}
if (inputFC == 4) {
transmoney = 3000;
}
if (inputFC == 3) {
transmoney = 2000;
}
if (inputFC == 2) {
transmoney = 1000;
}
if (inputFC == 1) {
transmoney = 500;
}
if (inputFC == 0) {
continue;
}
if (transmoney <= availbalance) {
cout << "\n Your transaction completed successfully" << endl;
cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl;
atmlimit += transmoney;
availbalance -= transmoney;
cout << atmlimit << endl; //TBD
cout << availbalance; //TBD
continue;
}
if (transmoney > availbalance) {
cout << "\n You have insufficient balance to perform this transaction" << endl;
continue;
}
break;
case 2:
cout << "\n Please enter the amount of cash you want to withdraw from your bank account \n Enter 0 if you want to return to the menu \n" << endl;
cin >> cwmoney;
cout << cwmoney; //TBR
if (cwmoney == 0) {
continue;
}
if (cwmoney <= availbalance) {
cout << "\n Your transaction bas been processed, Thank you!\n" << endl;
cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl;
atmlimit += cwmoney;
availbalance -= cwmoney;
cout << atmlimit << endl;
cout << availbalance;
continue;
}
else if (cwmoney > availbalance) {
cout << "\n You have insufficient balance for this transaction. \n" << endl;
continue;
}
break;
case 3:
cout << "\n Your current bank balance is idk" << availbalance << "Rs" << endl;
cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl;
break;
case 4:
cout << "\n Please enter the amount of cash you want to transfer/send \n Enter 0 if you want to return to the menu \n" << endl;
cin >> ctmoney;
if (ctmoney == 0) {
continue;
}
cout << "\n" << endl;
cout << ctmoney << endl; //TBD
if (ctmoney <= availbalance) {
cout << "\n Your transaction bas been processed, Thank you!\n" << endl;
cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl;
atmlimit += ctmoney;
availbalance -= ctmoney;
cout << atmlimit << endl; //TBD
cout << availbalance;
continue;
}
else if (ctmoney > availbalance) {
cout << "\n You have insufficient balance for this transaction. \n" << endl;
continue;
}
break;
}
}
cout << "\n You have reached the maximum amount of cash flow through your account, Please try later \n" << endl;
}
Fixed the issue by replacing goto statements with break/continue and some other small tweaks as mentioned in the comments.
I'm currently in a class that wants me to make a craps game.
The problem is in int main on the second while statement comparing the point with the roll. It ignores the if statement and does the loop again, even though it hits the point or 7. Sometimes it works like it should and other times it repeats the loop a few times.
#include "pch.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int diceRoll() {
int x = 0, y = 0;
x = rand() % 6 + 1;
y = rand() % 6 + 1;
cout << "You rolled a " << x << " and a " << y << " which comes out to ------> " << x + y << " <-------" << endl;
return x + y;
}
bool playAgain() {
char ans;
cout << "Do you want to play again ?? Y to continue, N to quit." << endl;
cin >> ans;
while (ans != 'Y' || ans != 'y' || ans != 'n' || ans != 'N') {
if (ans == 'Y' || ans == 'y')
{
return true;
}
if (ans == 'N' || ans == 'n')
{
return false;
}
cout << "Do you want to play again ?? Y to continue, N to quit." << endl;
cin >> ans;
}
}
int main()
{
srand(time(NULL));
int dices, bid, point = 0;
int money = 50;
bool gameRunning = true;
bool didTheyWin;
while (gameRunning == true) {
if (money == 0) {
cout << "You have no money, ending game." << endl;
break;
}
cout << "Please enter a bid. You currently have $" << money << endl;
cout << "$";
cin >> bid;
while (bid > money) {
cout << "Please bet below your current balance: $" << money << endl;
cout << "$";
cin >> bid;
}
dices = diceRoll();
didTheyWin = false;
if ((dices == 7) || (dices == 11)) {
cout << "You won $" << bid << " !" << endl;
money = money + bid;
}
else if ((dices == 2) || (dices == 3) || (dices == 12)) {
cout << "You LOSE! You lost $" << bid << " !" << endl;
money = money - bid;
}
else {
point = dices;
cout << "The target number is > " << point << " <" << endl;
cout << "If you hit a 7 you lose, if you hit the target you win. \nYou automatically roll until one of these two things happen.\n";
while (didTheyWin == false) {
diceRoll();
dices = diceRoll();
if (dices == point) {
cout << "You won $" << bid << " !" << endl;
money = money + bid;
cout << "You now have $" << money << endl;
didTheyWin = true;
}
else if (dices == 7) {
cout << "You LOSE! You lost $" << bid << " !" << endl;
money = money - bid;
cout << "You now have $" << money << endl;
didTheyWin = true;
}
}
}
gameRunning = playAgain();
}
cout << "Thanks for playing. _END_" << endl;
return 0;
}
You call diceRoll twice, and ignore what you get back from the first call. You'll see the results of that first roll displayed, but they'll be ignored and you'll roll again.
I have been receiving an issue that I do not understand. The issue is:
Run-Time Check Failure #3 - The variable 'win' is being used without being initialized.
I am struggling to comprehend where my error is within the code. From what I understand the run time error comes from win not being initialized meaning it hasn't been used or set, however, it clearly is set in the fightScene() function.
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include "Character.h"
#include "Item.h"
#include "Blacksmith.h"
using namespace std;
void startup();
void createCharacter();
void existCharacter();
void secondFighter();
void fightScene();
int fightMoves(int att, int sp, int num);
void roundWon();
void characterLost();
void use_bar();
void newlines();
void changeCase(string &convert);
void bSmith();
Character existingCharacter[4] = { Character("Shabu", 10, 10, 10, 0, "Empty"), Character("Calil", 15, 15, 15, 0, "Empty"), Character("Goltero", 20, 20, 20, 0, "Empty"), Character("Balrogg", 30, 30, 30, 0, "Empty") };
Character cCharacter[2] = { Character(), Character() };
Character cCharacterSave("Empty", 20, 0, 0, 100, "Empty");
Item bars(0, 0, 0);
int timesWon = 0;
int purposeIgnore = 0;
char repeat = 'y';
int main() {
startup();
while (repeat == 'y' || repeat == 'Y') {
fightScene();
if (repeat == 'y' || repeat == 'Y')
secondFighter();
}
return 0;
}
void startup() {
int playerChoice;
cout << "**************************" << endl;
cout << "* Loading Game *" << endl;
cout << "**************************" << endl;
cout << "Which Would You Prefer?" << endl;
cout << "1: New Character..." << endl;
cout << "2: Load Character..." << endl;
do {
cout << "Enter Selection: ";
cin >> playerChoice;
} while (playerChoice <= 0 || playerChoice >= 3);
if (playerChoice == 1)
createCharacter();
else if (playerChoice == 2)
existCharacter();
return;
}
void createCharacter() {
string Name;
string inventory;
int h, att, sp, gc;
cout << "***************************" << endl;
cout << "* Creating New Player *" << endl;
cout << "***************************" << endl;
cout << "What Shall We Call You?" << endl;
cout << "Enter Name Here: ";
cin >> Name;
changeCase(Name);
cout << Name << ", let's set your stats!" << endl;
cout << "Set your ATTACK between 1 - 10" << endl;
do {
cout << "Enter Attack Here: ";
cin >> att;
} while (att <= 0 || att >= 11);
cout << "Set your SPEED between 1 - 10" << endl;
do {
cout << "Enter Speed Here: ";
cin >> sp;
} while (sp <= 0 || sp >= 11);
h = 20;
gc = 100;
cout << "The HEALTH stat wil increase over time, but as of now, " << Name << ", has " << h << " health!" << endl;
cout << Name << " you have been granted " << gc << " gold!" << endl;
cCharacter[0] = Character(Name, h, att, sp, gc, inventory);
cin.ignore();
return;
}
void existCharacter() {
string characterChoice;
cout << "***********************" << endl;
cout << "* Existing Player *" << endl;
cout << "***********************" << endl;
for (int i = 0, j = 1; i >= 0 && i <= 3; i++, j++) {
cout << j << ": " << existingCharacter[i].getName();
cout << "\t" << "Health: " << existingCharacter[i].getHealth();
cout << "\t" << "Attack: " << existingCharacter[i].getAttack();
cout << "\t" << "Speed: " << existingCharacter[i].getSpeed();
}
cout << "Enter Selection: ";
cin.ignore();
getline(cin, characterChoice);
changeCase(characterChoice);
if (characterChoice == "1" || characterChoice == "Shabu") {
cCharacter[0] = existingCharacter[0]; cCharacterSave.setHealth(existingCharacter[0].getHealth());
}
else if (characterChoice == "2" || characterChoice == "Calil") {
cCharacter[0] = existingCharacter[1]; cCharacterSave.setHealth(existingCharacter[1].getHealth());
}
else if (characterChoice == "3" || characterChoice == "Goltero") {
cCharacter[0] = existingCharacter[2]; cCharacterSave.setHealth(existingCharacter[2].getHealth());
}
else if (characterChoice == "4" || characterChoice == "Balrogg") {
cCharacter[0] = existingCharacter[3]; cCharacterSave.setHealth(existingCharacter[3].getHealth());
}
else {
cout << "Program Failure....Invaid Input!" << endl;
}
secondFighter();
return;
}
void secondFighter() {
string cFighter;
cout << "***************" << endl;
cout << "* Fighter *" << endl;
cout << "***************" << endl;
cout << "Choose Who To Fight!" << endl;
for (int i = 0, j = 1; i >= 0 && i <= 3; i++, j++) {
cout << j << ": " << existingCharacter[i].getName();
cout << "\t" << "Health: " << existingCharacter[i].getHealth();
cout << "\t" << "Strength: " << existingCharacter[i].getAttack();
cout << "\t" << "Speed: " << existingCharacter[i].getSpeed();
cout << endl;
}
cout << "Enter Selection: ";
if (purposeIgnore >= 1)
cin.ignore();
getline(cin, cFighter);
changeCase(cFighter);
if (cFighter == "1" || cFighter == "Shabu")
cCharacter[1] = existingCharacter[0];
else if (cFighter == "2" || cFighter == "Calil")
cCharacter[1] = existingCharacter[1];
else if (cFighter == "3" || cFighter == "Goltero")
cCharacter[1] = existingCharacter[2];
else if (cFighter == "4" || cFighter == "Balrogg")
cCharacter[1] = existingCharacter[3];
else { cout << "Invalid Input! Program Failure....Please Close and Restart" << endl; }
return;
}
void fightScene() {
int fight_choice;
int i = 0;
int j = 5;
bool win;
if (timesWon >= 1)
j = 6;
while (cCharacter[0].getHealth() > 0 && cCharacter[1].getHealth() > 0) {
newlines();
cout << "\t\t" << cCharacter[0].getName() << " choose ";
if (i == 0)
cout << "an attack.";
else
cout << "another attack.";
do {
cout << "\n\n\t\t<1. Punch / 2. Kick>\n\t\t<3. Slam / 4. Drop kick>";
if (j == 6)
cout << "\n\t\t<5. Items>";
cout << endl << "\t\t";
cin >> fight_choice;
} while (fight_choice <= 0 || fight_choice >= j);
if (fight_choice == 1) {
cCharacter[1].setHealth(cCharacter[1].getHealth() - fightMoves(cCharacter[0].getAttack(), cCharacter[0].getSpeed(), 1));
cout << "\n\t\tYou Punched " << cCharacter[1].getName() << ", his health is now: ";
if (cCharacter[1].getHealth() <= 0)
{
cout << "0" << endl << endl; cCharacter[1].setHealth(-1);
}
else
cout << cCharacter[1].getHealth() << endl << endl;
}
else if (fight_choice == 2) {
cCharacter[1].setHealth(cCharacter[1].getHealth() - fightMoves(cCharacter[0].getAttack(), cCharacter[0].getSpeed(), 2));
cout << "\n\t\tYou Kicked " << cCharacter[1].getName() << ", his health is now: ";
if (cCharacter[1].getHealth() <= 0)
{
cout << "0" << endl << endl; cCharacter[1].setHealth(-1);
}
else
cout << cCharacter[1].getHealth() << endl << endl;
}
else if (fight_choice == 3) {
cCharacter[1].setHealth(cCharacter[1].getHealth() - fightMoves(cCharacter[0].getAttack(), cCharacter[0].getSpeed(), 3));
cout << "\n\t\tYou Slamed " << cCharacter[1].getName() << " on the ground, his health is now: ";
if (cCharacter[1].getHealth() <= 0)
{
cout << "0" << endl << endl; cCharacter[1].setHealth(-1);
}
else
cout << cCharacter[1].getHealth() << endl << endl;
}
else if (fight_choice == 4) {
cCharacter[1].setHealth(cCharacter[1].getHealth() - fightMoves(cCharacter[0].getAttack(), cCharacter[0].getSpeed(), 4));
cout << "\n\t\tYou Drop Kicked " << cCharacter[1].getName() << ", his health is now: ";
if (cCharacter[1].getHealth() <= 0)
{
cout << "0" << endl << endl; cCharacter[1].setHealth(-1);
}
else
cout << cCharacter[1].getHealth() << endl << endl;
}
else if (timesWon >= 1 && fight_choice == 5) { // Bars
use_bar();
}
// Second fighter attack
srand(time(NULL));
int ran_pick = rand() % 5 + 1;
if (cCharacter[1].getHealth() < 0) {
ran_pick = 0;
win = 1;
}
else if (cCharacter[1].getHealth() > 0) {
cout << "\n\t\t<" << cCharacter[1].getName() << "'s turn>" << endl;
win = 0;
}
if (ran_pick == 0)
continue;
else if (ran_pick == 1) {
cCharacter[0].setHealth(cCharacter[0].getHealth() - fightMoves(cCharacter[1].getAttack(), cCharacter[1].getSpeed(), 1));
cout << "\n\t\t" << cCharacter[1].getName() << " punched you! Your health is now: ";
if (cCharacter[0].getHealth() <= 0)
{
cout << "0" << endl << endl; cCharacter[0].setHealth(-1);
}
else
cout << cCharacter[0].getHealth() << endl << endl;
}
else if (ran_pick == 2) {
cCharacter[0].setHealth(cCharacter[0].getHealth() - fightMoves(cCharacter[1].getAttack(), cCharacter[1].getSpeed(), 2));
cout << "\n\t\t" << cCharacter[1].getName() << " kicked you! Your health is now: ";
if (cCharacter[0].getHealth() <= 0)
{
cout << "0" << endl << endl; cCharacter[0].setHealth(-1);
}
else
cout << cCharacter[0].getHealth() << endl << endl;
}
else if (ran_pick == 3) {
cCharacter[0].setHealth(cCharacter[0].getHealth() - fightMoves(cCharacter[1].getAttack(), cCharacter[1].getSpeed(), 3));
cout << "\n\t\t" << cCharacter[1].getName() << " slammed you on the ground! Your health is now: ";
if (cCharacter[0].getHealth() <= 0)
{
cout << "0" << endl << endl; cCharacter[0].setHealth(-1);
}
else
cout << cCharacter[0].getHealth() << endl << endl;
}
else if (ran_pick == 4) {
cCharacter[0].setHealth(cCharacter[0].getHealth() - fightMoves(cCharacter[1].getAttack(), cCharacter[1].getSpeed(), 4));
cout << "\n\t\t" << cCharacter[1].getName() << " drop kicked you! Your health is now: ";
if (cCharacter[0].getHealth() <= 0)
{
cout << "0" << endl << endl; cCharacter[0].setHealth(-1);
}
else
cout << cCharacter[0].getHealth() << endl << endl;
}
else if (ran_pick == 5)
cout << "\t\t" << cCharacter[1].getName() << " missed you! Your health is still: " << cCharacter[0].getHealth() << endl << endl;
if (cCharacter[0].getHealth() > 0) {
cout << "\n\t\t";
system("PAUSE"); // Lets them read the attacks
}
i++; // Increases i to change some wording
}
if (win == 1)
roundWon();
else
characterLost();
return;
}
int fightMoves(int st, int sp, int num) {
srand(time(NULL));
int rand_num;
int addition;
switch (num) {
case 1: {
rand_num = rand() % 40 + 1;
addition = (st + sp) / rand_num;
cout << "\n\t\tDamage inflicted: " << addition;
return addition; break;
}
case 2: {
rand_num = rand() % 20 + 1;
addition = (st + sp) / rand_num;
cout << "\n\t\tDamage inflicted: " << addition;
return addition; break;
}
case 3: {
rand_num = rand() % 30 + 1;
addition = (st + sp) / rand_num;
cout << "\n\t\tDamage inflicted: " << addition;
return addition; break;
}
case 4: {
rand_num = rand() % 30 + 1;
addition = (st + sp) / rand_num;
cout << "\n\t\tDamage inflicted: " << addition;
return addition; break;
}
}
}
int start()
{
while (repeat == 'y' || repeat == 'Y') {
fightScene();
if (repeat == 'y' || repeat == 'Y')
secondFighter();
}
return 0;
}
void roundWon() {
srand(time(NULL));
timesWon++;
purposeIgnore++;
cCharacter[0].setHealth(cCharacterSave.getHealth());
cCharacter[1].setHealth(100);
int item_won = rand() % 3 + 1;
cout << "\t\tYou won!";
cout << "\n\n\t\tYou have been rewarded ";
if (item_won == 1) {
cout << "a small protein bar! (+20 health)";
bars.bar_small++;
}
else if (item_won == 2) {
cout << "a medium protein bar! (+30 health)";
bars.bar_medium++;
}
else if (item_won == 3) {
cout << "a large protein bar! (+40 health)";
bars.bar_large++;
}
cout << "\n\n\t\tItems can be used in game at the cost of a turn.";
cout << "\n\n\t\t";
system("PAUSE"); // Lets them read
int ability_gain[3] = { rand() % 10 + 1, rand() % 10 + 1, rand() % 10 + 1 };
cCharacter[0].setHealth(ability_gain[0] += cCharacter[0].getHealth());
cCharacterSave.setHealth(cCharacter[0].getHealth());
cCharacter[0].setAttack(ability_gain[1] += cCharacter[0].getAttack());
cCharacter[0].setSpeed(ability_gain[2] += cCharacter[0].getSpeed());
cout << "\n\t\tYour health is now: " << cCharacter[0].getHealth();
cout << "\n\t\tYour strength is now: " << cCharacter[0].getAttack();
cout << "\n\t\tYour speed is now: " << cCharacter[0].getSpeed();
cout << "\n\n\t\tWould you like to fight another person? (y/n) ";
cin >> repeat;
return;
}
void characterLost() {
cCharacter[0].setHealth(cCharacterSave.getHealth());
cCharacter[1].setHealth(100);
purposeIgnore++;
cout << "\n\n\t\tYou have lost.";
cout << "\n\t\tWould you like to fight another person? (y/n) ";
cin >> repeat;
return;
}
void use_bar() {
int choose_bar;
char another_bar;
do {
cout << "\n\n\t\tWhich bar would you like to use? ";
cout << "\n\t\t 1: Small Bar: " << bars.bar_small;
cout << "\n\t\t 2: Medium Bar: " << bars.bar_medium;
cout << "\n\t\t 3: Large bar: " << bars.bar_large;
do {
cout << "\n\n\t\tI choose: ";
cin >> choose_bar;
} while (choose_bar <= 0 || choose_bar >= 4);
if (choose_bar == 1 && bars.bar_small >= 1) {
cCharacter[0].setHealth(cCharacter[0].getHealth() + 20);
bars.bar_small--;
cout << "\n\n\t\tYour health is now: " << cCharacter[0].getHealth();
}
else if (choose_bar == 2 && bars.bar_medium >= 1) {
cCharacter[0].setHealth(cCharacter[0].getHealth() + 30);
bars.bar_medium--;
cout << "\n\n\t\tYour health is now: " << cCharacter[0].getHealth();
}
else if (choose_bar == 3 && bars.bar_large >= 1) {
cCharacter[0].setHealth(cCharacter[0].getHealth() + 40);
bars.bar_large--;
cout << "\n\n\t\tYour health is now: " << cCharacter[0].getHealth();
}
else
cout << "\n\t\tNot enough bars!";
cout << "\n\n\t\tWould you like to use another bar? (y/n) ";
cin >> another_bar;
} while (another_bar == 'y' || another_bar == 'Y');
return;
}
void newlines() {
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
}
void changeCase(string &convert) {
for (int i = 0; convert[i]; i++) {
if (isupper(convert[i]))
convert[i] = tolower(convert[i]);
else
continue;
}
convert.at(0) = toupper(convert.at(0));
return;
}
void bSmith()
{
Blacksmith shopKeeper; //The shop keeper
int responce; //Menu navigation
cout << "*******************************" << endl;
cout << "* Welcome to the Blacksmith *" << endl;
cout << "*******************************" << "\n" << endl;
cout << "*****************************" << endl;
cout << "* 1: Purchase Items *\n";
cout << "* 2: Sell Items *\n";
cout << "* 3: List Your Items *\n";
cout << "* 4: Currency *\n";
cout << "* 5: Exit *\n";
cout << "*****************************" << endl;
do
{
cout << "Enter Option Here: ";
cin >> responce;
switch (responce)
{
case 1:
shopKeeper.buyItem(cCharacter[0]);
cout << endl;
break;
case 2:
cout << "*****************************" << endl;
cout << "* Items To Sell *" << endl;
cout << "*****************************" << endl;
shopKeeper.sellItem(cCharacter[0]);
cout << endl;
break;
case 3:
cout << "*****************************" << endl;
cout << "* Character Inventory *" << endl;
cout << "*****************************" << endl;
cCharacter[0].listInv();
cout << endl;
break;
case 4:
cout << "*****************************" << endl;
cout << "* Current Gold Currency *" << endl;
cout << "*****************************" << endl;
cout << "\t" << cCharacter[0].getCurrency() << endl;
cout << endl;
break;
case 5:
cout << "*****************************" << endl;
cout << "* Thanks for shopping *" << endl;
cout << "*****************************" << endl;
return;
default:
cout << "Please enter valid data." << "\n";
break;
}
cout << "*****************************" << endl;
cout << "* 1: Purchase Items *\n";
cout << "* 2: Sell Items *\n";
cout << "* 3: List Your Items *\n";
cout << "* 4: Currency *\n";
cout << "* 5: Exit *\n";
cout << "*****************************" << endl;
} while (responce != 5);
return;
}
All I can see that sets win is this part of the code,
if (cCharacter[1].getHealth() < 0) {
ran_pick = 0;
win = 1;
}
else if (cCharacter[1].getHealth() > 0) {
cout << "\n\t\t<" << cCharacter[1].getName() << "'s turn>" << endl;
win = 0;
}
However, if cCharacter[1].getHealth()==0, win will be left unset.
I've been trying to complete a task but I am not allowed to use global variables.
The task if that you have to make a coffee shop program where user can buy coffee (small, medium or large), then show the total number of coffee cups sold and then then total sales made from selling coffee. I completed half of the code where user can buy but I am stuck at summing coffee sales/cups.
The function cupsSold and totalAmount return 0 or garbage value. Also I am not allowed to use extra library functions.
Is there any way to call variables from another function without using global variables?
This is my main code:
#include <iostream>
using namespace std;
void showChoices()
{
cout << "Welcome to Jason's Coffee Shop\n\n";
cout << "1. Buy Coffee" << endl;
cout << "2. Show total cups sold by size"<< endl;
cout << "3. Show total amount of coffee sold"<<endl;
cout << "4. Show total amount of money made"<< endl;
cout << "0. Exit" << endl;
cout << "\nYour choice: ";
}
void buyCoffee(int numberSmCups,float totalSmCups,int numberMedCups,float totalMedCups, int numberLgCups,float totalLgCups)
{
char coffeeSize, order = 'y';
bool coffeeSelect = true;
float smCoffee = 1.75, mdCoffee = 1.90, lgCoffee = 2.00, totalCoffee = 0;
while(coffeeSelect)
{
if (order == 'y' ||order =='Y'){
cout << "Size of Coffee [S, M, L]: ";
cin >> coffeeSize;
if (coffeeSize == 's' || coffeeSize == 'S')
{
cout << "Quantity of Small Coffee: ";
cin >> numberSmCups;
totalSmCups = numberSmCups * smCoffee;
cout << "Small Coffee: "<<numberSmCups<<", Bill: "<<totalSmCups<<endl;
totalCoffee += totalSmCups;
cout << "Add another coffee [Y or N]: "<<endl;
cin >> order;
}
else if (coffeeSize == 'm' || coffeeSize == 'M')
{
cout << "Quantity of Medium Coffee: ";
cin >> numberMedCups;
totalMedCups = numberMedCups * mdCoffee;
cout << "Medium Coffee: "<<numberMedCups<<", Bill: "<<totalMedCups<<endl;
totalCoffee += totalMedCups;
cout << "Add another coffee [Y or N]: "<<endl;
cin >> order;
}
else if (coffeeSize == 'l' || coffeeSize == 'L')
{
cout << "Quantity of Large Coffee: ";
cin >> numberLgCups;
totalLgCups = numberLgCups * lgCoffee;
cout << "Large Coffee: "<<numberLgCups<<", Bill: "<<totalLgCups<<endl;
totalCoffee += totalLgCups;
cout << "Add another coffee [Y or N]: ";
cin >> order;
}
}
else
break;
}
cout << "--------------\n";
cout << "Your invoice: "<<endl;
cout<< endl;
if (numberSmCups>= 1)
cout << "Small Coffee: "<< numberSmCups <<" cups ($ "<< smCoffee <<"/cup) Amount: $ "<< totalSmCups << endl;
if (numberMedCups>=1)
cout << "Medium Coffee: "<< numberMedCups <<" cups ($ "<< mdCoffee << "/cup) Amount: $ "<< totalMedCups << endl;
if (numberLgCups>=1)
cout << "Large Coffee: " << numberLgCups <<" cups ($ " << lgCoffee << "/cup) Amount: $ " << totalLgCups << endl;
cout<< endl;
cout << "Total Amount: "<<"$ "<< (totalSmCups+ totalMedCups+ totalLgCups) << endl;
cout << "--------------" << endl;
}
void cupsSold(int numberSmCups,int numberMedCups,int numberLgCups){
int lifeSmCups=0;
int lifeMdCups=0;
int lifeLgCups=0;
lifeSmCups = lifeSmCups + numberSmCups;
lifeMdCups = lifeMdCups + numberMedCups;
lifeLgCups = lifeLgCups + numberLgCups;
cout << "Total Small cups: "<<lifeSmCups<<endl;
cout << "Total Medium Cups:"<<lifeMdCups<<endl;
cout << "Total Large Cups:"<<lifeLgCups<<endl;
}
void totalAmount(float totalCoffee)
{
cout << "Total: "<<"$"<<totalCoffee<<endl;
}
int main()
{
int numberSmCups = 0, numberMedCups = 0, numberLgCups = 0, choice;
float totalSmCups = 0, totalMedCups = 0, totalLgCups = 0, totalCoffee;
while (choice != 0)
{
showChoices();
cin >> choice;
cout << endl;
if (choice == 1)
{
cout <<"You selected to buy coffee."<< endl;
buyCoffee(numberSmCups,totalSmCups,numberMedCups,totalMedCups,numberLgCups,totalLgCups);
}
else if (choice == 2)
{
cout << "The total amount of cups sold by size is: "<<endl;
cupsSold(numberSmCups,numberMedCups,numberLgCups);
}
else if (choice == 3)
{
cout << "Total Coffee Sold: "<<endl;
// cupsSold(numberSmCups,numberMedCups,numberLgCups);
}
else if (choice == 4)
{
cout << "Money made from coffee sales: "<<endl;
totalAmount(totalCoffee);
}
else if (choice == 0)
{
cout << "EXIT"<<endl;
break;
}
else
cout << "Invalid Input" << endl;
}
return 0;
}
"Is there any way to call variables from another function without using global variables?"
You need to use by reference parameters, if you want to change the variables passed to a function like this
void cupsSold(int& numberSmCups,int& numberMedCups,int& numberLgCups){
// ^ ^ ^
otherwise these functions just operate on copies rather than your original variable values.
for some reason my subtotal variable doesn't store the prices of the items inputted by the user any suggestions? I don't know if I set up my loop wrong or if the subtotal has to be put outside the if else statements but then I don't know how I would know what to store from users input
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
const double tax_rate = 0.05;
struct menuItemType
{
string ItemName;
double ItemPrice;
};
void getData(ifstream &in, menuItemType menuList[]);
void showMenu(menuItemType menuList[]);
void printCheck(menuItemType menuList[]);
int main()
{
menuItemType menuList[10];
menuList[10].ItemName;
menuList[10].ItemPrice;
ifstream in;
in.open("Menu.txt");
cout << "Welcome to Johnny's Breakfast Diner!" << endl;
getData(in,menuList);
showMenu(menuList);
return 0;
}
void getData(ifstream &in, menuItemType menuList[])
{
int i = 0;
while (!in.eof())
{
in >> menuList[i].ItemName >> menuList[i].ItemPrice;
i++;
}
}
void showMenu(menuItemType menuList[])
{
int j = 0;
char answ;
cout << fixed << setprecision(2);
cout << "Would you like to take a look at our menu? (Y/N)";
cin >> answ;
if (answ == 'Y' || answ == 'y')
{
cout << left << setw(10) << "Item#" << left << setw(15) << "Item" << left << setw(18) << "Price" << endl;
do {
{
cout << left << setw(8) << j << " " << left << setw(15) << menuList[j].ItemName << " " << "$" << menuList[j].ItemPrice << endl;
j++;
}
} while (j < 8);
printCheck(menuList);
}
else if (answ == 'N' || answ == 'n')
{
system("cls");
cout << "Have a good day!" << endl;
}
}
void printCheck(menuItemType menuList[])
{
char answ;
int choice;
bool menu = true;
double subtotal = 0;
double tax = (subtotal * tax_rate);
double total = (tax + subtotal);
cout << "Would like to place your order (Y/N)";
cin >> answ;
if (answ == 'Y' || answ == 'y')
{
cout << "Please enter the number of the item, 8 to finish order:";
do {
cin >> choice;
if (choice == 0)
{
cout << menuList[0].ItemName << " " << "$" << menuList[0].ItemPrice << endl;
subtotal = subtotal + menuList[0].ItemPrice; \\ for some reason here it doesn't store the prices have no idea why
}
else if (choice == 1)
{
cout << menuList[1].ItemName << " " << "$" << menuList[1].ItemPrice << endl;
subtotal = subtotal + menuList[1].ItemPrice;
}
else if (choice == 2)
{
cout << menuList[2].ItemName << " " << "$" << menuList[2].ItemPrice << endl;
subtotal = subtotal + menuList[2].ItemPrice;
}
else if (choice == 3)
{
cout << menuList[3].ItemName << " " << "$" << menuList[3].ItemPrice << endl;
subtotal = subtotal + menuList[3].ItemPrice;
}
else if (choice == 4)
{
cout << menuList[4].ItemName << " " << "$" << menuList[4].ItemPrice << endl;
subtotal = subtotal + menuList[4].ItemPrice;
}
else if (choice == 5)
{
cout << menuList[5].ItemName << " " << "$" << menuList[5].ItemPrice << endl;
subtotal = subtotal + menuList[5].ItemPrice;
}
else if (choice == 6)
{
cout << menuList[6].ItemName << " " << "$" << menuList[6].ItemPrice << endl;
subtotal = subtotal + menuList[6].ItemPrice;
}
else if (choice == 7)
{
cout << menuList[7].ItemName << " " << "$" << menuList[7].ItemPrice << endl;
subtotal = subtotal + menuList[7].ItemPrice;
}
else if (choice == 8)
{
break;
}
}while(menu = true);
cout << "Taxes" << "$" << tax << endl;
cout << "Amount Due" << "$" << total << endl;
}
else if (answ == 'N' || answ == 'n')
{
system("cls");
cout << "Ok, maybe I can help you at a later time." << endl;
}
}
It looks like you're trying to use subtotal before you've actually put data into it.
The problem is these lines:
double tax = (subtotal * tax_rate);
double total = (tax + subtotal);
At that point in the program, subtotal still contains the initial value, which is 0, so the result of those calculations is also 0. You need to put those lines after the loop so that they work with the final value of subtotal.
What's the result. the subtotal will have
menuList[choice].ItemPrice value
if you want to change
subtotal += menuList[choice].ItemPrice;