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.
Related
when I'm executing this program, it works fine just for one option, but if I try to choose an other option when executing, it will throw an error in this line:
cout << "The available quantity is: " << *(it->second.find(disp)) << endl;
here's my program:
void avail_art(void)
{
char c,con;
int quantity,disp=MAX;
bool b = true;
map<string, unordered_set<int>>m{
{"Mouse",{120,disp}},
{"Keyboard",{75,disp}},
{"Monitor",{750,disp}},
{"GPU",{1200,disp}},
{"CPU",{1000,disp}}
};
auto it = m.find("CPU");
cout << "M - for mouse." << endl;
cout << "K - for keyboard." << endl;
cout << "R - for monitor." << endl;
cout << "G - for GPU." << endl;
cout << "C - for CPU." << endl;
cout << "Q - for quit." << endl;
cout << "======================" << endl;
while (b)
{
cout << "Enter your choice: ";
cin >> c;
switch (c)
{
case 'M':
it = m.find("Mouse");
cout << "You've choosen the Mouse!" << endl;
cout << "The available quantity is: " << *(it->second.find(disp)) << endl;
l:
cout << "Enter the quantity: ";
cin >> quantity;
if (quantity > * (it->second.find(disp)))
{
cout << "Out of Stock! the Stock has only :" << *(it->second.find(disp)) <<" Piece."<< endl;
goto l;
}
cout << "Confirm? y/n: ";
cin >> con;
if (con == 'y')
{
auto its=it->second.find(disp);
it->second.insert(disp=(*its - quantity));
it->second.erase(its);
}
break;
case 'K':
it = m.find("Keyboard");
cout << "You've choosen the Keyboard!" << endl;
cout << "The available quantity is: " << *(it->second.find(disp)) << endl;
cout << "Enter the quantity: ";
cin >> quantity;
cout << "Confirm? y/n :";
cin >> con;
if (con == 'y')
{
auto its = it->second.find(disp);
it->second.insert(disp = (*its - quantity));
it->second.erase(its);
}
break;
}
}
}
can someone help me please
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 am working on an assignment. The problem I am having is every time I try to run my program to see what it displays, nothing shows up on the command prompt. However, if I press any key and then enter, the program starts looping uncontrollably. The program doesn't even display the initial cout message, just a blinking "_". Thanks
#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;
void PizzaMenu();
void SizePrices();
int main()
{
double personal = 10.00;
double medium = 14.50;
double large = 19.00;
double xlarge = 23.50;
double FlavorChoice=0;
int SizeChoice;
int PizzaCountP=(cin >> PizzaCountP, PizzaCountP);
int PizzaCountM = (cin >> PizzaCountM, PizzaCountM);
int PizzaCountL = (cin >> PizzaCountL, PizzaCountL);
int PizzaCountXL = (cin >> PizzaCountXL, PizzaCountXL);
double orderTotal = (personal * PizzaCountP) + (medium * PizzaCountM) + (large * PizzaCountL) + (xlarge * PizzaCountXL);
cout << "Welcome to Joes pizza place!" << endl;
do{
PizzaMenu();
cout << "\nPlease chose a pizza from the menu(1-6): ";
cin >> FlavorChoice;
SizePrices();
cin >> SizeChoice;
if (SizeChoice > 0 && SizeChoice < 5)
{
switch (SizeChoice)
{
case 1:
cout << "How many personal pizzas? "; cin >> PizzaCountP;
break;
case 2:
cout << "How many medium pizzas?"; cin >> PizzaCountM;
break;
case 3:
cout << "How many large pizzas?"; cin >> PizzaCountL;
break;
case 4: cout << "How many extra large pizzas?"; cin >> PizzaCountXL;
break;
default: cout << "please enter a choice (1-4)"; cin >> SizeChoice;
break;
}
}
if (PizzaCountP > 0 || PizzaCountM > 0 || PizzaCountXL > 0 || PizzaCountL > 0)
{
printf("Your total is: %a", orderTotal);
}
} while (FlavorChoice != 6);
cout << "Thank you for visiting Joes place pizza! "<<endl;
}
void PizzaMenu()
{
cout << "\nSpecialty Pizza Menu" << endl;
cout << "\n1)Pizza 1" << endl << "\n2)Pizza 2" << endl << "\n3)Pizza 3" <<endl << "\n4)Pizza 4" << endl << "\n5)Pizza 5" << endl << "\n6)Pizza 6" << endl;
}
void SizePrices()
{
cout << "1) 10'' Personal" << "\t" << "- $10.00" << endl;
cout << "2) 14'' Medium" << "\t" << "- $14.50" << endl;
cout << "3) 16'' Large" << "\t" << "- $19.00" << endl;
cout << "4) 18'' Extra Large" << "\t" << "- $23.50" << endl;
cout << "Your choice (1-4)? ";
}
There were a few logical errors in the program. Right now, it should work fine...
#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;
void PizzaMenu()
{
cout << "Specialty Pizza Menu:" << endl;
cout << "1) Pizza 1" << endl << "2) Pizza 2" << endl << "3) Pizza 3" << endl << "4) Pizza 4" << endl << "5) Pizza 5" << endl << "6) Exit" << endl;
}
void SizePrices()
{
cout << "Size Prices:" << endl;
cout << "1) 10'' Personal" << "\t" << "- $10.00" << endl;
cout << "2) 14'' Medium" << "\t" << "- $14.50" << endl;
cout << "3) 16'' Large" << "\t" << "- $19.00" << endl;
cout << "4) 18'' Extra Large" << "\t" << "- $23.50" << endl;
cout << "Your choice (1-4)? ";
}
int main()
{
double personal = 10.00;
double medium = 14.50;
double large = 19.00;
double xlarge = 23.50;
int FlavorChoice = 0;
int SizeChoice = 0;
int PizzaCountP = 0;
int PizzaCountM = 0;
int PizzaCountL = 0;
int PizzaCountXL = 0;
double orderTotal = 0.0;
cout << "Welcome to Joes pizza place!" << endl;
cout << "Please choose from the main menu(1-6): " << endl;
PizzaMenu();
cin >> FlavorChoice;
while(FlavorChoice != 6) {
SizePrices();
cin >> SizeChoice;
if (SizeChoice > 0 && SizeChoice < 5)
{
switch (SizeChoice)
{
case 1:
cout << "How many personal pizzas? ";
cin >> PizzaCountP;
orderTotal += personal * PizzaCountP;
break;
case 2:
cout << "How many medium pizzas?";
cin >> PizzaCountM;
orderTotal += medium * PizzaCountM;
break;
case 3:
cout << "How many large pizzas?";
cin >> PizzaCountL;
orderTotal += large * PizzaCountL;
break;
case 4: cout << "How many extra large pizzas?";
cin >> PizzaCountXL;
orderTotal += xlarge * PizzaCountXL;
break;
default: cout << "please enter a choice (1-4)";
cin >> SizeChoice;
break;
}
}
// orderTotal = (personal * PizzaCountP) + (medium * PizzaCountM) + (large * PizzaCountL) + (xlarge * PizzaCountXL);
if (PizzaCountP > 0 || PizzaCountM > 0 || PizzaCountXL > 0 || PizzaCountL > 0)
{
// printf("Your total is: %a", orderTotal);
cout << "Your total is: $" << orderTotal << endl;
}
cout << "Please choose from the main menu(1-6): " << endl;
PizzaMenu();
cin >> FlavorChoice;
}
cout << "Thank you for visiting Joes place pizza! " << endl;
// system("pause");
return 0;
}
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)
Sorry for being a noob. How do I return to the line of code just after where attackRat() is called? After the rat is dead I want the program to come to here, how do I do that? Thanks and sorry if I'm being stupid.
void ratCave() {
location = "rat cave";
system("cls");
cout << "\n You arrive at the cave where two rats greet you at the entrance." << endl;
cout << "\n What would you like to do?" << endl;
cout << "\n 1. Attack the rats" << endl;
cout << "\n 2. Open backpack" << endl;
cout << "\n> ";
cin >> input;
switch (input) {
case 1:
attackRat();
// I want the program to come here once the rat is dead.
case 2:
backpack();
}
}
void attackRat() {
srand (time(NULL));
damage = rand() % 10 + 10;
ratDamage = rand() % 5 + 5;
if (start == 1) {
ratHealth = rand() % 20 + 30;
turn = rand() % 2;
if (turn == 1) {
system("cls");
cout << "\n The rat have the first turn." << endl;
cout << "\n ";
system("pause");
health = health - ratDamage;
system("cls");
cout << "\n The rat attacks you for " << ratDamage << " health!" << endl;
cout << "\n ";
system("pause");
}
else {
system("cls");
cout << "\n You have the first turn." << endl;
cout << "\n ";
system("pause");
}
}
start = 0;
system("cls");
cout << "\n Your health: " << health << endl;
cout << "\n Rat health: " << ratHealth << endl;
cout << "\n What do you do?" << endl;
cout << "\n [1] Attack rat" << endl;
cout << "\n [2] Use a potion" << endl;
cout << "\n> ";
cin >> input;
switch (input) {
case 1:
ratHealth = ratHealth - damage;
system("cls");
cout << "\n You attack the rat for " << damage << " damage!" << endl;
cout << "\n ";
system("pause");
if (ratHealth < 1) {
ratHealth = 0;
system("cls");
cout << "\n You killed the rat!" << endl;
cout << "\n ";
system("pause");
// Does something go here to return it to the bit I want?
}
health = health - ratDamage;
system("cls");
cout << "\n The rat attacks you for " << ratDamage << " damage!" << endl;
cout << "\n ";
system("pause");
if (health == 0) {
system("cls");
cout << "\n You died!" << endl;
cout << "\n ";
system("pause");
exit(0);
}
case 2:
attackRat();
}
}
Instead of calling attackRat() from inside attackRat itself to do a loop use a normal loop with:
for(;;) {
... code to repeat forever ...
}
then you can use return to go back to who called the function
Just return from attackRat():
// Does something go here to return it to the bit I want?
return;
Then once you're back in ratCave():
// I want the program to come here once the rat is dead.
cout << "\nYup, the rat's dead and we're back in the rat cave.";