Can't get an int to update outside of the while loop - c++

When it runs the winnings or losses are taken or added from the bank and then it is run again the bank is set back to a 25$ bank not the updated bank
int main()
{
srand(time(0));
int bank = 25;
int total;
char answer;
cout << "Come play Spin the Wheel. The wheel has numbers from 1-10." << endl
<< "If you spin an even number you lose that amount. If you spin" << endl
<< "an odd number you win that amount. You start with a 25$ bank." << endl;
cout << "Your bank is $" << bank << ". Would you like to spin the wheel? (y/n):" << endl;
cin >> answer;
while (toupper(answer) == 'Y')
{
int num = rand() % 10 + 1;
if (bank <= 10)
{
cout << "Sorry you must have more than 10$ to play" << endl;
}
else if (num % 2 == 0 )
{
total = bank + num;
cout << "You spun a " << num << " and won $" << num << endl;
cout << "Your bank is now: $" << total << endl;
}
else
{
total = bank - num;
cout << "You spun a " << num << " and lost $" << num << endl;
cout << "Your bank is now: $" << total << endl;
}
cout << "Would you like to play Again (y/n) ?" << endl;
cin >> answer;
}
return 0;
}
When it runs the winnings or losses are taken or added from the bank and then it is run again the bank is set back to a 25$ bank not the updated bank

You initialize bank to be 25 dollar in this function. Inside the while loop only total is updated not bank.
Another problem arises when there is too few money to play. I suppose you would like to display your message one time, but you are stuck inside the loop because it is never broken out of.

I believe your else if and else statements are backwards.
num % 2 == 0
Would signify that the number was even, if true. The instructions say that you lose the money if the number is even.
Bank will never be less than or equal to 10 because you are never setting bank to anything after the initial 25. Its always 25.
The variable total seems redundant. Maybe add or subtract the amount rolled directly from bank.

You need to set
bank = total
else you never change its value

Related

how to divide money in to different amounts

I'm trying to do this program for many hours, but I couldn't.
The question is I've to take some amount of money from user. Then I've to ask how many 5s he have and how many 2s he have. I've to convert that amount of money to 5s and 2s and 1s. User have unlimited source of 1s. In short if user enters 27 I've to tell him 5 5s and 1 2s.
Now i did this type of program in which I converted time(from seconds to years and so on)
Now I did this in program:-
int money;
cout << "How much amount of money= ";
cin >> money;
cout << endl;
int ao5;
cout << "how many number of 5 coins available in your drawer= ";
cin >> ao5;
cout << endl;
int ao2;
cout << "how many number of 2 coins available in your drawer= ";
cin >> ao2;
cout << endl;
int fchange, tchange, ochange;
ochange = (money / 1) % ao5%ao2;
tchange = (money / ao2) % ao5;
fchange = money / ao5;
cout << " " << fchange;
cout << " " << tchange;
cout << " " << ochange;
I've tried other methods. I tried dividing money by 5 and then subtract it with 5s I've but it makes no sense.
Can anyone just take me to right path?
You can use this
void divid(int num){
if(num>=5){
int remainderAfterdevidingTo5 = num%5;
int numOf5s = num/5;
std::cout << " numOf5s "<< numOf5s<< std::endl;
divid(remainderAfterdevidingTo5);
}else if(num>=2){
int remainderAfterdevidingTo2 = num%2;
int numOf2s = num/2;
std::cout << " numOf2s "<< numOf2s<< std::endl;
divid(remainderAfterdevidingTo2);
}else if(num>=1){
std::cout << " numOf1s " <<num<< std::endl;
}
}

Car Loan Calculation (C++)

My objective is to calculate and output a loan repayment schedule. The thing I would like to get help on is putting the principles added to the equation and printing out the repayment schedule. I am not sure if I did the calculations right as I have not had a personal finance class yet, and still get to grasp the concept of loans.
The loan repayment schedule is based on full price of an auto, their interest rate and their payment, assuming no money is put down. All fees and taxes are included in the price and will be financed. I also have to out put the repayment schedule to both the screen and a file - one month per line. . If the user has a credit rate of 800, they get a 3% annual interest rate; 700+ gets 5% interest rate; 600+ get 7% interest rate; and less than 600 get 12% interest rate
The credit scores for 700, 600, and below 600 are left blank because I am just going to copy the 800 credit score part again but change the interest rates.
// This program calculates a loan depending on the pereson's credit score
// how much they can pay per month. It almost outputs the month, principal,
// payment, interest, and the money that's been applied
#include <iostream>
#include <cstdio>
#include <iomanip>
using namespace std;
int main() {
int month = 0, creditScore = 0, whichCar;
double principle, payment = 0.0, interestPaid, applied, interestRate;
cout << fixed << setprecision(2) << showpoint; // Sets total or whatever to 2 decimal points
cout << "---------------------------------------------" << endl; // Displays welcome banner
cout << "| |" << endl;
cout << "| JOLLY GOOD SHOW WE HAVE CARS AYEEE |" << endl;
cout << "| |" << endl;
cout << "---------------------------------------------" << endl;
cout << endl;
cout << "Hey, I see you want a car. You can only purchase one car though." << endl;
cout << endl;
cout << "1. Furawree: $6,969.69" << endl; // Displays menu of autos
cout << "2. Buggee: $420,420.420" << endl;
cout << "3. Sedon: $900" << endl;
cout << "4. Truck: $900,000.90" << endl;
cout << "5. Couppee: $22,222.22" << endl;
cout << endl;
cout << "Which car would you like to purchase?" << endl; // Asks user car type and user inputs car #
cout << "Please enter the number of the car: ";
cin >> whichCar;
cout << endl;
switch(whichCar) { // If user choses a number 1-5, then it asks them how much they can pay each month for the car and their credit score
case 1: // FURAWREE
principle = 6969.69;
break;
case 2: // BUGGEE
principle = 420420.42;
break;
case 3: // SEDON
principle = 900;
break;
case 4: // TRUCK
principle = 900000.90;
break;
case 5: // COUPPEE
principle = 22222.22;
break;
default: // If user doesn't pick a number from 1-5
cout << "Yea uhhmmm we don't have that sorry, go away." << endl;
}
cout << "Please enter how much you can pay each month for this Furawree: ";
cin >> payment;
cout << "Please enter your credit score: ";
cin >> creditScore;
if (creditScore >= 800) {
interestRate = .03 / 12;
do {
interestPaid = principle * interestRate;
applied = payment - interestPaid;
month++;
} while (principle < 0) ;
cout << "Month " << " Principle " << " Payment " << " Interest " << " Applied " << endl;
cout << month << " $" << principle << " $" << payment << " " << interestPaid << " $" << applied << endl;
} else if (creditScore >= 700) {
// Will be copied from the 800 credit score
} else if (creditScore >= 600) {
// Will be copied from the 800 credit score
} else {
// Will be copied from the 800 credit score
}
cout << endl;
cout << endl;
cout << "Your payment: $" << payment << endl;
cout << "Your credit score: " << creditScore << endl;
cout << endl;
cout << endl;
system("pause");
return 0;
}
Mate, you need to fix code under credit - 800.
loop condition is incorrect
cout is after the loop, therefore it will print only once .
principle is not incremented nor decremented . and you are checking if principle is less than 0, however principle is set more than 0. so the loop will execute only once.
you need a fix some thing like this. I have just fine tuned little bit. pls fix the rest
if (creditScore >= 800) {
interestRate = .03 / 12;
cout << "Month " << " Principle " << " Payment " << " Interest " << " Applied " << endl;
cout <<"-------------------------------------------------------" << endl;
do {
interestPaid = principle * interestRate;
applied = payment - interestPaid;
principle = principle - applied;
cout << month << " $" << principle << " $" << payment << " " << interestPaid << " $" << applied << endl;
month++;
} while (principle > 0) ;
} else if (creditScore >= 700) {
Note :-
The above code is not following any object oriented concepts. Its not even functional programming. Introduce classes, methods to reduce headache and it will help to debug.
use \t\t to get spaces instead of spaces.
This code will need a big re-work to make it look professional .

Telephone Bill Project: C++

Alright, I have edited some of the code, so now the question is how it calculates the right final total? I am working on the regular service, and I want it to subtract the 50 free minutes first, then add the extra minutes the user has overused, then multiply the extra minutes by $0.20 and make that into the finaltotal.
And if I have any more mistakes please tell me. I know I have made a lot of mistakes, sorry!
I have a project to do, and here is the requested functionalities list:
Prompts the user to enter an account number, a service code, and the number of minutes the service was used.
Regular: Values are “R” and “r”:
$10.00/month
First 50 min. are free
Charges for over 50 min. are $0.20 per min.
Premium: Values are “P” and “p”:
$25.00/month plus:
Calls between 6 - 18 first 75 min are free, after that they’re $0.10 per min
Calls between 18 - 6 first 100 min are free, after that they’re $0.05 per min
If any other character is used then display an error message
Calculate the total and print the bill (display it out)
This is my code:
#include <iostream>
using namespace std;
int main()
{
// VARIABLES //
int accnum, minnum, minnumm;
double total, finaltotal, grandtotal, finaltotall;
char scode;
cout << "Hello, thank you for paying your phone bill." << ends;
cout << endl;
cout << endl;
cout << "Please enter your account number: ";
cin >> accnum ; //Enter some number
cout << "Please enter your service code (r as regular service or p for premium service): ";
cin >> scode ; //Enter R or r for regular service and P or p for premium service
////////////// THIS IS FOR REGULAR SERVICE //////////////
if (scode == 'R' || scode == 'r') { // Values for Regular service
cout << "This service provides 50 minutes for phone calls for 50 minutes for free for $10.00 a month, and charge $0.20 every minute over 50 minutes." << endl;
cout << "How many minutes have you used up?: ";
cin >> minnum;
if (minnum > 50) {
total = minnum * .20;
finaltotal = total + 10;
cout << "You have made phone calls over 50 minutes, your total will be " << finaltotal << "." << endl;
cout << "Your account number is " << accnum << " , with a Regular service. You have used " << minnum << "/50 minutes. Your total is $" << finaltotal << " . Thank you for your time." << endl; //Displays the acc. #, type of service, # of min the phone service used, and amount due from user
cout << endl;
} else {
cout << "You have not made phone calls over 50 minutes, your total will be $" << finaltotal << "." << endl;
cout << "Your account number is " << accnum << " , with a Regular service. You have used " << minnum << "/50 minutes. Your total is $" << finaltotal << " . Thank you for your time." << endl; //Displays the acc. #, type of service, # of min the phone service used, and amount due from user
}
////////////// THIS IS FOR PREMIUM SERVICE //////////////
} else if (scode == 'P' || scode == 'p') { //Values for Premium service
cout << "This service provides your first 75 minutes phone calls from 6:00 - 18:00, and charge $0.10 for every minute over. Your first 100 minutes for phone calls from 18:00 - 6:00 are free, and charge $0.05 for every minute over." << endl;
cout << "How many minutes have you used up between 6:00 - 18:00? "; //Time between 6am - 6pm
cin >> minnumm;
if (minnumm > 75) { //If # of minutes is over 75
total = minnumm * .10; //Then it multiplies total * $.10
finaltotall = total + 25; //Then adds the $10/month
cout << "You have made phone calls over 75 minutes, your total will be $" << finaltotall << "." << endl;
} else {
cout << "How many minutes have you used up between 18:00 - 6:00? "; } //Time between 6pm - 6am
cin >> minnum;
if (minnum > 100) { //If # of minutes is over 100
total = minnum * .05; //Then it multiplies total * $.05
finaltotal = total + 25;
//Then adds the $25/month
cout << "You have made phone calls over 100 minutes, your total will be $" << finaltotal << "." << endl;
grandtotal = finaltotall + finaltotal; //Calculates both the 6am-6pm and 6pm-6am totals together
cout << "Your account number is " << accnum << " , with a Premium service. You have used " << minnumm << "/75 minutes from 6:00-18:00. You have used" << minnum << "/100 from 18:00-6:00. Your total is $" << grandtotal << " . Thank you for your time." << endl; //Displays the acc. #, type of service, # of min the phone service used, and amount due from user
} else {
cout << "You have not made phone calls over 100 minutes, your total will be $" << finaltotal << "." << endl;
cout << "Your account number is " << accnum << " , with a Premium service. You have used " << minnumm << "/75 minutes from 6:00-18:00. You have used " << minnum << "/100 from 18:00-6:00. Your total is " << grandtotal << " . Thank you for your time." << endl;
cout << endl;
cout << endl; } //Displays the acc. #, type of service, # of min the phone service used, and amount due from user
////////////// THIS IS IF THEY TYPED IN ANY LETTER OTHER THAN R, r, P, p //////////////
} else { //If user doesn't type P or R, then this is error message
cout << "Sorry, that service does not exist. Please try again." << endl;
cout << endl; }
system("pause");
return 0;
}
As far as I can see (and people in follow-up answers might find more errors), the glaring issue is that you have used the AND operator instead of the OR operator.
This statement:
if (scode == 'R' && scode == 'r') { // Values for Regular service
Should be:
if (scode == 'R' || scode == 'r') { // Values for Regular service
Similarly, this statement:
if (scode == 'P' && scode == 'p') { //Values for Premium service
Should be:
if (scode == 'P' || scode == 'p') { //Values for Premium service
A variable can't match two different values at the same time, so the if blocks will never be entered when using && instead of ||.

How to write a function that checks combinations of 3 numbers?

I'm currently making a 2 player dice game and i need to create a function that checks the value of the dice combination you rolled. Ex: I rolled 3-4-2, I need the function to check if there is a payout for 3-4-2, example rolls and their payouts + code below
//Rolling 1-1-1 would give you 5x your wager
//Rolling 3 of the same number (except 1-1-1) would give you 2x wager
//Rolling 3 different numbers (ex 1-4-6) would give you 1x your wager
//Rolling 1-2-3 makes the player automatically lose a round and pay opposing Player 2x wager
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
void roll_3_dice(int &dice1, int &dice2, int &dice3)
{
srand(time(NULL));
dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;
dice3 = rand() % 6 + 1;
return;
}
int main()
{
int cash = 90000;
int wager;
int r;
//dealer's die
int dealer1;
int dealer2;
int dealer3;
// your die
int mdice1;
int mdice2;
int mdice3;
while ( cash > 100 || round < 10 )
{
cout << "Set your wager: "<< endl;
cin >> wager;
while (wager < 100 || wager > 90000)
{
cout << "Minimum wager is 100; Maximum wager is 90000 ";
cin >> wager;
}
cout << "You wagered: " << wager << endl;
cout << "You have " << cash - wager << " remaining" << endl;
cash = cash - wager;
cout << endl;
cout << "Dealer will now roll the dice" << endl;
roll_3_dice(dealer1, dealer2, dealer3);
cout << "Dealer rolled the following: " << endl;
cout << dealer1 << "-" << dealer2 << "-" << dealer3 << endl;
cout << "It's your turn to roll the dice." << endl;
cout << endl;
cout << "Press any key to roll the dice" << endl;
cin >> r;
roll_3_dice(mdice1, mdice2, mdice3);
cout << "You rolled the following: " << endl;
cout << mdice1 << "-" << mdice2 << "-" << mdice3 << endl;
system ("pause`enter code here`");
}
}
I would suggest to write your algorithm on a paper first, like you did in the comments on top of the code.
Then, ask yourself what you need to process the final wager ? As in parameters. For instance, in your case, you may need a function which takes the initial wager, and the values of the three dices as entry parameters.
This function would return the final wager.
Finally, in the function itself, organize the algorithm by priority rules.
If 1-1-1 gives you 5x your wager, then it's a particular case which can be isolated at the top of the function maybe, and you can return 5 x initial wager directly, without any need to process further operations.
You'll just have to organize the different cases with if statements, regarding the priorities of each statement.
The 1-1-1 case has to come before the "same number for each dice" for example.
Hope this helps.

If else statement loop or repeat until true

I'm not sure how to loop or repeat a cin >> cashTendered if the amount tendered is less than the required amount.
So far, this is what I have.
if (cashTendered > || == total)
{
cout << "Your change is: $" << fixed << setprecision(2) << change << ".\n\n"
<< "Have a great day!\n\n\n\n\n";
}
else
{
cout << "You did not tender enough money to cover the total cost.\n"
<< "Please enter amount of cash tendered: $";
}
So now I want the if statement to repeat until true.
any suggestions?
Have a look at loops in c++. Also if condition can be shortened to >= instead of > || ==. Moreover, since you want to repeat until the cashTendered is >= total, you need a loop that checks the condition if cashTendered is < total.
while (cashTendered < total)
{
cout << "You did not tender enough money to cover the total cost.\n"
<< "Please enter amount of cash tendered: $";
cin >> cashTendered;
}
cout << "Your change is: $" << fixed << setprecision(2) << change << ".\n\n"
<< "Have a great day!\n\n\n\n\n";
while(true)
{
cin >> cashTendered
if (cashTendered >= total)
{
cout << "Your change is: $" << fixed << setprecision(2) << change << ".\n\n"
<< "Have a great day!\n\n\n\n\n";
break;
}
else
{
cout << "You did not tender enough money to cover the total cost.\n"
<< "Please enter amount of cash tendered: $";
}
}