I can't figure out how to make my code go to the next else if statement if my user input satisfies the previous if state.
#include <iostream>
using namespace std;
int main()
{
double input;
cout << "Time Calculator\n Enter the number of Seconds: " << endl;
cin >> input;
if (input < 60)
{
cout << "The time is " << input << " seconds." << endl;
}
else if (input >= 60)
{
cout << "The time is " << input << " minutes." << endl;
}
else if (input >= 3600)
{
cout << "The time is " << input << " hours." << endl;
}
else if (input >= 86400)
{
cout << "The time is " << input << " days." << endl;
}
return 0;
}
in your code if input > 60 it will satisfy condition and will not execute else part so first check wether input > 86400 if not then check if input > 36000 if not then check for input > 60
try below code in which if conditions are reversed
#include <iostream>
using namespace std;
int main()
{
double input;
cout << "Time Calculator\n Enter the number of Seconds: " << endl;
cin >> input;
if (input < 60)
{
cout << "The time is " << input << " seconds." << endl;
}
else if (input >= 86400)
{
cout << "The time is " << input << " days." << endl;
}
else if (input >= 3600)
{
cout << "The time is " << input << " hours." << endl;
}
else if (input >= 60)
{
cout << "The time is " << input << " minutes." << endl;
}
return 0;
}
Make it other way round.
#include <iostream>
using namespace std;
int main()
{
double input;
cout << "Time Calculator\n Enter the number of Seconds: " << endl;
cin >> input;
if (input >= 86400)
{
cout << "The time is " << input << " days." << endl;
}
else if (input >= 3600)
{
cout << "The time is " << input << " hours." << endl;
}
else if (input >= 60)
{
cout << "The time is " << input << " minutes." << endl;
}
else
{
cout << "The time is " << input << " seconds." << endl;
}
return 0;
}
Hope this is what you want!
There are multiple ways to do this, you can implement a check between 2 values using conditions like >= val1 and < val2 or ensure that the order in which the checks happen is different.
#include <iostream>
using namespace std;
int main()
{
double input;
cout << "Time Calculator\n Enter the number of Seconds: " << endl;
cin >> input;
if (input < 60)
{
cout << "The time is " << input << " seconds." << endl;
}
else if (input >= 60 && input < 3600)
{
cout << "The time is " << input << " minutes." << endl;
}
else if (input >= 3600 && input < 86400)
{
cout << "The time is " << input << " hours." << endl;
}
else if (input >= 86400)
{
cout << "The time is " << input << " days." << endl;
}
return 0;
}
or another way would be to change the order, once the particular if statement is valid, it doesn't check with the rest of the if statements.
#include <iostream>
using namespace std;
int main()
{
double input;
cout << "Time Calculator\n Enter the number of Seconds: " << endl;
cin >> input;
if (input >= 86400)
{
cout << "The time is " << input << " seconds." << endl;
}
else if (input >= 3600)
{
cout << "The time is " << input << " minutes." << endl;
}
else if (input >= 60)
{
cout << "The time is " << input << " hours." << endl;
}
else if (input < 60)
{
cout << "The time is " << input << " days." << endl;
}
return 0;
}
Your if else conditions are not completely defined... you have to set those in a range... otherwise is not going to work...
if (input < 60)
{
cout << "The time is " << input << " seconds." << endl;
}
else if (input >= 60 && input < 3600))
{
cout << "The time is " << input << " minutes." << endl;
}
else if (input >= 3600 && input < 86400))
{
cout << "The time is " << input << " hours." << endl;
}
else if (input >= 86400)
{
cout << "The time is " << input << " days." << endl;
}
Related
In the below code, I've got some bugs and I don't know why it doesn't work.
I used a fmod to do the decimals but I got some bugs in the part where it gives you the 0.10 and 0.05 $ and most of the time I never get the good amount that I'm supposed to give back if there is.
#include<iostream>
#include <cmath>
using namespace std;
int main()
{
int iAmount_due = { 0 };
int iGiven_money = { 0 };
int iMoney_back;
iMoney_back = iGiven_money - iAmount_due;
cout << "Enter the amount due please: " << endl;
cin >> iAmount_due;
cout << "Enter the amount given please: " << endl;
cin >> iGiven_money;
if (iGiven_money >= iAmount_due) {
iMoney_back = iGiven_money - iAmount_due;
cout << "We will give you : " << iMoney_back << " $ back" << endl;
}
else {
cout << "No money back" << endl;
}
if (iMoney_back >= 100) {
cout << "You will receive: " << iMoney_back % 100 << " x 100$" << endl;
}
else {
cout << "No 100$ bill" << endl;
}
if (iMoney_back >= 50) {
cout << "You will receive: " << iMoney_back % 50 << " x 50$" << endl;
}
else {
cout << "No 50$ bill" << endl;
}
if (iMoney_back >= 20) {
cout << "You will receive: " << iMoney_back % 20 << " x 20$" << endl;
}
else {
cout << "No 20$ bill" << endl;
}
if (iMoney_back >= 10) {
cout << "You will receive: " << iMoney_back % 10 << " x 10$" << endl;
}
else {
cout << "No 10$ bill" << endl;
}
if (iMoney_back >= 5) {
cout << "You will receive: " << iMoney_back % 5 << " x 5$" << endl;
}
else {
cout << "No 5$ bill" << endl;
}
if (iMoney_back >= 0.25) {
cout << "You will receive: " << fmod (iMoney_back, 0.25) << " x 0.25$ " << endl;
}
else {
cout << "No 0.25$ " << endl;
}
if (iMoney_back >= 0.10) {
cout << "You will receive: " << fmod (iMoney_back, 0.10) << " x 0.10$ " << endl;
}
else {
cout << "No 0.10$ " << endl;
}
if (iMoney_back >= 0.05) {
cout << "You will receive: " << fmod (iMoney_back, 0.05) << " x 0.05$ " << endl;
}
else {
cout << "No 0.05$ " << endl;
}
return 0;
}
int holds only integer values. 0.05 is not an integer value. You might be tempted to use double values instead that probably work for your simple problem; however, they generally have rounding errors which is something you really do not want to have with currency values.
The better solution will be to still use integers. You would need to define 1 as the smallest possible unit (e.g. 1 cent), meaning that all your values will be in cent. You would need to write extra code to be able to enter and display values as dollars instead of cents.
I am trying to add a countdown timer to this program. I would like the timer to start when the first math fact question is asked and upon expiration i want the program to give the grade. What's the code to do this in c++ if possible?
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
int main(int args, char* argv[])
{
int i;
int result;
int solution;
char fact;
bool done = false;
int correct = 0;
int count = 0;
do {
try {
cout << "Enter (m)ultiplication or "
<< "(a)ddition." << endl; /*or (s)ubstraction. */
cin >> fact;
while (!cin)
throw fact;
if (fact != 'A')
if (fact != 'a')
if (fact != 'M')
if (fact != 'm')
while (!cin)
throw fact;
cout << "Now, enter the number of the fact that
you would like to do." << endl;
cin >> i;
int wrong = 0;
int score = 0;
int j = 0;
while (!cin)
throw i;
switch (fact) {
case 'm':
case 'M':
while (j < 13) {
cout << "What's " << i << " x " << j << "?" << endl;
cin >> result;
while (!cin)
throw result;
solution = i * j;
if (result == solution) {
cout << "Great Job! That is the correct answer for the problem "
<< i << " x " << j << "." << endl;
cout << endl;
cout << endl;
cout << endl;
score++;
j++;
cout << endl;
}
if (result != solution) {
cout << "Oh no! " << result << " is NOT the correct answer for "
<< i << " x " << j << "." << endl;
wrong = wrong + 1;
count++;
}
if (count == 3) {
cout << "The correct answer is " << i * j << "." << endl;
j++;
wrong = wrong - 3;
count = 0;
}
if (count == 1) {
cout << endl;
count--;
wrong = wrong - 1;
}
if (count == 2) {
cout << endl;
count--;
wrong = wrong - 2;
}
}
case 'a':
case 'A':
while (j < 13) {
cout << "What's " << i << " + " << j << "?" << endl;
cin >> result;
while (!cin)
throw result;
solution = i + j;
if (result == solution) {
cout << "Great Job! That is the correct answer for the problem "
<< i << " + " << j << "." << endl;
cout << endl;
cout << endl;
cout << endl;
score++;
j++;
cout << endl;
}
if (result != solution) {
cout << "Oh no! " << result << " is NOT the correct answer for "
<< i << " + " << j << "." << endl;
wrong = wrong + 1;
count++;
}
if (count == 3) {
cout << "The correct answer is " << i + j << "." << endl;
j++;
wrong = wrong - 3;
count = 0;
}
if (count == 1) {
cout << endl;
count--;
wrong = wrong - 1;
}
if (count == 2) {
cout << endl;
count--;
wrong = wrong - 2;
}
}
if (j == 13) {
system("pause");
correct = score - wrong;
score = (correct * 100) / 13;
}
if (score >= 80) {
cout << "Excellent!!!!!" << endl;
cout << "You scored " << score << "%." << endl;
cout << "You got " << correct << " out of 13 correct." << endl;
cout << "Keep up the good work." << endl;
} else if (score >= 70) {
cout << "Congratulations!!!!!" << endl
cout << "You scored " << score << "%." << endl;
cout << "You got " << correct << " out of 13 correct." << endl;
cout << "Let's see if we can score even higher next time." << endl;
} else {
cout << "You scored below 70 which means that you may need some"
<< " more practice." << endl;
cout << "You scored " << score << "%." << endl;
cout << "You got " << correct << " out of 13 correct." << endl;
cout << "You might want to try the " << i << " facts again."
<< " Goodluck!!!!!" << endl;
}
}
} catch (char fact) {
cout << "Invalid input. You can only enter (m)ultiplication or"
<< " (a)ddition. Please try again." << endl;
cin.clear();
cin.ignore(100, '\n');
} catch (int i) {
cout << "Invalid input0. You can only enter a
number here. Please try again." << endl;
cin.clear();
cin.ignore(100, '\n');
} catch (...) {
cout << "Invalid input2. You can only enter a number here.
Please try again." << endl;
cin.clear();
cin.ignore(100, '\n');
}
} while (!done);
return 0;
}
The task is quite hard, but if you dare trying, I suggest doing it in two steps:
Implement inaccurate solution: timer expiration is checked between queries to user.
If there is some time left, next question is asked, otherwise statistics is shown. So program always waits for user input on the last question despite timer has run out. Not what exactly quizzes look like, but good move to start with.
Method: before starting quiz save current time, before each question take delta between saved time and current one and compare with time limit. Example with chrono (starting from C++11), example with oldschool clock
Add middle-question interruption
This part requires function, which will wait for user input not longer, than specified amount of time. So instead of using std::cin() you'll need to calculate amount of time left (time limit minus delta between cur time and start time) and call some sort of cin_with_timeout(time_left).
The hardest thing is implementing cin_with_timeout(), which requires solid knowledge of multithreading and thread synchronization. Great inspiration can be found here, but it is direction to start thinking rather than complete solution.
My question is this, how do I save an int value in a while loop, my code is all about gambling, you start with 1,000 and you want to make the most amount of cash, but when I roll again my cash restores back to its original value that I set.
My code is this (Note I am new so do not laugh at how bad it is)
#include <cmath>
#include <stdio.h>
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
char again = 'Y';
int test;
int yes;
int CashW;
CashW = 1000;
int CashL;
CashL = 1000;
int yLose;
yLose = 500;
int xCash;
xCash = 1000;
int xRan;
srand(time(0));
xRan = rand() % 100 + 1;
cout << " Welcome to the Gambling Game!" << endl;
cout << " If the number is above 50 I win!" << endl;
cout << " If the number is below 50 you lose!" << endl;
while (again == 'y' || again == 'Y')
{
cout << " The Number I Choose Is: " << xRan << endl;
CashL = xCash - xCash - xCash;
CashW = xCash + xCash;
if (xRan < 50) {
cout << " You win, rats!" << endl;
cout << " The cash you started with was: " << xCash << endl;
cout << " The cash you have now is: " << CashW << endl;
cout << " Type 1 to play again, type 2 to close the game." << endl;
cin >> yes;
}
if (xRan > 50) {
cout << " I win, you lose!" << endl;
cout << " The cash you started with was: " << xCash << endl;
cout << " The cash you have now is: " << CashL << endl;
cout << " Type 1 to play again, type 2 to close the game." << endl;
cin >> yes;
}
if (yes == 1) {
cout << " Cool, a gambling man! Time to make some cash" << endl;
}
}
}
In your code you currently display either CashW or CashL depending on the gampbling result.
Unfortunately, you only print out the result and never store it into xCash. So at next iteration you start again with the same xCash value !
You can easily solve this by adding xCash = CashW; or xCash = CashL; just under the line in which you display the result.
You are never updating xCash with the amount of each win/loss. You are not generating a new random number on each loop iteration. And you are stuck in an endless loop because you never update the loop variable again.
Try something more like this instead:
#include <cmath>
#include <stdio.h>
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
const int CashW = 1000;
const int CashL = 1000;
int xCash = 1000;
int xRan;
char answer;
srand(time(0));
cout << " Welcome to the Gambling Game!" << endl;
cout << " If the number is above 50 I win!" << endl;
cout << " If the number is below 50 you win!" << endl;
do
{
xRan = rand() % 100 + 1;
cout << " The Number I Choose Is: " << xRan << endl;
if (xRan < 50) {
cout << " You win, rats!" << endl;
cout << " The cash you started with was: " << xCash << endl;
xCash += CashW;
cout << " The cash you have now is: " << xCash << endl;
}
else if (xRan > 50) {
cout << " I win, you lose!" << endl;
cout << " The cash you started with was: " << xCash << endl;
xCash -= CashL;
cout << " The cash you have now is: " << xCash << endl;
}
else {
cout << " dang, a draw!" << endl;
}
cout << " play again? " << endl;
cin >> answer;
if ((answer != 'y') && (answer != 'Y')) {
cout << " All done? Come back again another time!" << endl;
break;
}
cout << " Cool, a gambling man! Time to make some cash" << endl;
}
while (true);
return 0;
}
Why is my else, cout << "You have entered an incorrect code" still executing and writing to the screen after I enter r or R and complete the calculation and dialogue. The same does not happen when I enter p or P and follow through with that portion of my program. Sorry for the incredibly nooby question.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char service;
int number;
int minutes;
int dayMinutes;
int nightMinutes;
double bill;
double dayCharge;
double nightCharge;
double const REG_FEE = 10.00;
double const PREM_FEE = 25.00;
double const REG_MIN = 0.20;
double const PREM_DAY = 0.10;
double const PREM_NIGHT = 0.05;
cout << "Please enter your account number: ";
cin >> number;
cout << "Please enter your service type (regular or premium): ";
cin >> service;
if (service == 'r' || service == 'R')
{
cout << "How many minutes have been used for this service?: ";
cin >> minutes;
if (minutes <= 50)
{
bill = REG_FEE;
cout << fixed << showpoint << setprecision(2);
cout << "Your bill is $" << bill << "." << endl;
cout << "The account number entered was: " << number << "." << endl;
cout << "The service type entered was: " << service << "." << endl;
cout << "You used: " << minutes << " minutes." << endl;
}
else
{
bill = ((minutes - 50) * REG_MIN) + REG_FEE;
cout << fixed << showpoint << setprecision(2);
cout << "Your bill is $" << bill << "." << endl;
cout << "The account number entered was: " << number << "." << endl;
cout << "The service type entered was: " << service << "." << endl;
cout << "You used: " << minutes << " minutes." << endl;
}
}
if (service == 'p' || service == 'P')
{
cout << "How many minutes were used during the day?: ";
cin >> dayMinutes;
cout << "How many minutes were used during the night?: ";
cin >> nightMinutes;
if (dayMinutes > 75)
{
dayCharge = ((dayMinutes - 75) * PREM_DAY);
}
if (nightMinutes > 100)
{
nightCharge = ((nightMinutes - 100) * PREM_NIGHT);
}
bill = dayCharge + nightCharge + PREM_FEE;
cout << fixed << showpoint << setprecision(2);
cout << "Your bill is $" << bill << "." << endl;
cout << "The account number entered was: " << number << "." << endl;
cout << "The service type entered was: " << service << "." << endl;
cout << "You used: " << dayMinutes + nightMinutes << " minutes." << endl;
}
else
{
cout << "You have entered an invalid service code." << endl;
cout << "The account number entered was: " << number << "." << endl;
cout << "The service type entered was: " << service << "." << endl;
}
return 0;
}
That's because you need this-
if (service == 'r' || service == 'R'){
// your code
}
else if(service == 'p' || service == 'P'){
//your code
}
else {
//your code
}
Problem right now with your code is that if you even enter 'r' or 'R', due to if else condition with 'p' or 'P' becomes false and else part gets executed .
That's why you needed to use if - else if format so that for an input only one part is executed.
How do I make a function that will terminate the program if the user says e, and loop if the user presses l at any time?
How do I make the program reask the user for number input if the user inputs letters instead of numbers? Currently, the program terminates when I input blah, for instance. My obstacle is the bool die definition: I'm not sure how to use bool die to loop instead of die (my teacher required bool die usage.)
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
bool die(const string & msg);
int main() {
// declaring variables:
unsigned QUARTERS;
unsigned DIMES;
unsigned NICKELS;
unsigned PENNIES;
double total;
cout << "********************************************************" << endl;
cout << " Welcome to Crazy Coin Counter! " << endl;
cout << "********************************************************" << endl << endl;
// user input:
//QUARTERS
do {
cout << " # QUARTERS: ";
cin >> QUARTERS;
if (cin){
if (QUARTERS < 1000)
cout << " --> Input Successful!" << endl;
}
else die(" --> :( Input Unsuccessful!");
cout << " You must put in less than 1000 quarters! Please try again." << endl << endl << endl;
} while (QUARTERS >= 1000 );
//DIMES
do{
cout << endl << " # DIMES: ";
cin >> DIMES;
if (cin){
if (DIMES < 1000)
cout << " --> Input Successful!" << endl;
}
else die(" --> :( Input Unsuccessful!");
if (DIMES >= 1000)
cout << " You must put in less than 1000 dimes! Please try again." << endl << endl << endl;
} while (DIMES >= 1000);
//NICKELS
do {
cout << endl << " # NICKLES: ";
cin >> NICKELS;
if (cin){
if (NICKELS < 1000)
cout << " --> Input Successful!" << endl;
}
else die(" --> :( Input Unsuccessful!");
if (NICKELS >= 1000)
cout << " You must put in less than 1000 nickels! Please try again." << endl << endl << endl;
} while (NICKELS >= 1000);
//PENNIES
do {
cout << endl << " # PENNIES: ";
cin >> PENNIES;
if (cin){
if (PENNIES < 1000)
cout << " --> Input Successful!" << endl;
}
else die(" --> :( Input Unsuccessful!");
if (PENNIES >= 1000)
cout << " You must put in less than 1000 pennies! Please try again." << endl;
} while (PENNIES >= 1000);
// calculations:
total = (QUARTERS * 0.25) + (DIMES * 0.1) + (NICKELS * 0.05) + (PENNIES * 0.01);
// output:
cout << endl <<endl<< "Congrats! You have $" << total << " worth of coins! " << endl << endl << endl;
}
// function definition
bool die(const string & msg){
cout << " " << msg << endl;
exit(EXIT_FAILURE);
}
Try something like this:
while ( true ) // Loop forever
{
cout << "Enter 'e' to exit:";
std::string answer;
getline(cin, answer);
if (answer == "e")
{
break; // Break out of the loop
}
else
{
cout << "\nWrong answer.\n";
continue; // The continue would start at the top of the loop.
}
}
There are many other techniques that you can find by searching StackOverflow for "c++ terminate loop".
Edit 1: Checking numerical input
The simplest method for checking numerical input is to test the result of inputting the number:
unsigned int quarters; // Using unsigned because quantities can't be negative.
cout << "Enter number of quarters: ";
if (cin >> quarters) // Input and test in same statement.
{
cout << "Your total is " << (quarters * 0.25) << "\n";
}
else
{
// Handle incorrect input
cout << "Invalid input, try again.\n";
}