0

On c++, im trying to make a program that will repeatedly accept the input of the user unless -999 is pressed. Additionally, if the input is not divisible by 5 or 20, then I asked for it to output "must enter divisible by 5 or 20." I want this to continue being done until they enter -999 but I do not know where to put my while loop or what to put if it is not entered. I also do not know where to put the "when finished enter -999 to leave" while making it eligible for all times and not just the start. Thank you!!!

#include <iostream> using namespace std; int main(void) { int amountEntered; cout << "Please enter the amount of money you would like to dispense (must be in 20's or 5's)" << endl; cout << "when finished, enter -999 to leave" << endl; if (amountEntered == -999) { cout << "Thank you for doing business." << endl; } cin >> amountEntered; if (amountEntered % 20 == 0) { cout << amountEntered / 20 << endl; } else { if (amountEntered % 5 == 0) { cout << amountEntered / 5 << endl; } else { cout << "You must enter multiples of twenty or five only!" << endl; } } { while (amountEntered != -999); while (amountEntered % 5 == 0); else { if (amountEntered % 5 != 0) { cout << "You must enter multiples of twenty or five only!" << endl; } } while (amountEntered % 20 == 0); } if (amountEntered % 20 != 0); { cout << "You must enter a number divisible by 20 or 5!" << endl; } if (amountEntered = -999) { cout << "Thank you for doing business." << endl; } } 
1
  • 1
    You have some quite interesting while-loops in your code. And an else without a matching if. Commented Dec 2, 2018 at 1:04

3 Answers 3

2

Here is some pseudocode to illustrate:

while true get input if input is -999 (or other conditions) break out of loop else // rest of code goes here 

So basically, wrap the whole thing in a while true loop and then use the conditional logic to break out of the loop when certain conditions are met.

Sign up to request clarification or add additional context in comments.

4 Comments

Too complicated.
@Swordfish How is this "complicated"?!
I kind of like it
Not overly complicated, just high-level. More pedagogically beneficial to explicate a pattern that can be used in other scenarios, rather than a rewrite of their code."Teach a man to fish and he eats for a lifetime"
1

On c++, im trying to make a program that will repeatedly accept the input of the user unless - 999 is pressed. Additionally, if the input is not divisible by 5 or 20, then I asked for it to output "must enter divisible by 5 or 20."

#include <limits> // std::numeric_limits<> #include <iostream> int main() { for (;;) { // forever int value; while (std::cout << "Thou must enter a value divisible by 5 or 20. When finished enter -999 to leave.\n", !(std::cin >> value) || value != -999 && value % 5 != 0 && value % 20 != 0) // ^^ extraction failed or value does not conform to requirements { std::cerr << "Input error :(\nYou must enter a number divisible by 5 or 20.\n"; std::cin.clear(); // clear the flags that might have been set by a // failed input operation. std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // ^^ discards up to the maximum value of std::streamsize characters // until a newline character ('\n') is encountered. If we don't do that // the next input operation will choke on the same erroneous input. } if (value == -999) break; // do sth with value } } 

19 Comments

Does not do what was asked, and the part that does is functionally-equivalent to but more complicated than the other solution already posted that you rejected as "too complicated" lol
Oh and you dropped the - from -999 for some reason
@LightnessRacesinOrbit Look again.
@LightnessRacesinOrbit Does what is asked now.
No, it doesn't.
|
0

I want this to continue being done until they enter -999 but I do not know where to put my while loop or what to put if it is not entered. I also do not know where to put the "when finished enter -999 to leave" while making it eligible for all times and not just the start.

Perhaps you might want to break the problem down. Firstly, you would know that the "end condition" of this loop would be that the user keyed in -999. Hence you would want your loop to look something like

while userinput != -999 // do something here end while loop 

With that, all we need is to place the capturing of user input. One would be at the start, and one just before the while loop ends.

get userinput while userinput != -999 // do something here get userinput end while loop 

There are several ways to approach this problem, and that's depending on how you want to design your code. Here's my approach for this:

#include <iostream> void Request(int& amount) { std::cout << "Please enter the amount of money you would like to dispense (must be in 20's or 5's)" << std::endl; std::cout << "when finished, enter -999 to leave" << std::endl; std::cout << ">"; std::cin >> amount; } int main(void) { int amount = 0; for (Request(amount); amount != -999; Request(amount)) { // Since 20 is a multiple of 5, just check if its divisble by 5 will do if (amount % 5) { std::cout << "You must enter multiples of twenty or five only!" << std::endl; continue; } // Otherwise print out (or do stuff here) std::cout << amount % 5 << std::endl; } std::cout << "Thank you for doing business." << std::endl; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.