-2

I edited this post to clarify my problem. Try to run the following program. It always stucks at the cin. you can cin as much number as u want but it wont continue. Thanks for all the answers


int main(){

int eingabe;
int zweieuro = 2;

cin >> eingabe;


if(eingabe < 2)
{
  eingabe = eingabe - zweieuro;
  cout << eingabe;
}
}
```
Wom Epic
  • 21
  • 4
  • 1
    There simply isn't enough here for us to work out what's going on. You mention a problem with reading from `cin`, but that isn't even shown in your code. Please provide a [mcve] that illustrates the problem. – BoBTFish Oct 23 '19 at 06:16
  • The program is "stuck" at some point before that, but you can't tell because that code isn't printing anything. – molbdnilo Oct 23 '19 at 06:22
  • If i delete the "input=input - two_euro;" in the if statement i get an unlimited loop as it should be. But idk why it stucks with input=input - two_euro; in it.. – Wom Epic Oct 23 '19 at 06:28
  • Please read this link again: [mcve] - I trimmed your code down to remove the unnecessary parts. – BoBTFish Oct 23 '19 at 06:30
  • @Sid Really? What if the input is 1? – BoBTFish Oct 23 '19 at 06:50
  • Off-topic: About [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Oct 23 '19 at 06:55
  • Are you aware that double cannot even represent such simple numbers like `0.1` precisely, as these are periodic in binary? Additionally, you should *never ever* compare double values for exact equality, which can deliver (pretty likely!) bad results due to rounding issues. If you need, then compare difference of the two values being smaller than some suitably small selected epsilon, e. g. `abs(d1 - d2) < 1e-6`. – Aconcagua Oct 23 '19 at 07:04
  • Better choice often (but not always) is using fixed comma arithmetics, in your example, you could calculate all values in cents instead of euros (solely reading inputs with decimals gets more difficult). – Aconcagua Oct 23 '19 at 07:04
  • Could it be that you tried to implement what [`fmod`](https://en.cppreference.com/w/cpp/numeric/math/fmod) does already? – Aconcagua Oct 23 '19 at 07:11
  • 1
    @WomEpic The latest edit of the question does not "get stuck" for me. It immediately completes, though it doesn't print anything. Please read again how to create a [mcve]. Note that it must be **reproducible** - I should be able to copy/paste and run it and see the exact problem, and it must be be **minimal** - you should remove any unnecessary parts of the code **and check it still reproduces the problem**. – BoBTFish Oct 23 '19 at 07:14
  • Then it is maybe because i use Repl.it , its an browser compiler. In the example above i always get stuck at the input of eingabe – Wom Epic Oct 23 '19 at 07:15
  • @WomEpic Get used to name all your functions and variables in English only. At some point in time, you *will* share the code with non-Germanophones (and if only here on SO), and for these, you make the code much harder to read, as they need to guess what variables/functions are intended for... Or would you know what's behind e. g. *'entrada'*? – Aconcagua Oct 23 '19 at 07:21
  • @WomEpic I tried your code above on [repl as well](https://repl.it/repls/AntiqueCheapComputer) - it terminates just fine. Notice that when the program completes the button at the top turns green and says "run >" so you can run it again. While it is running the button is grey and reads "stop []". – BoBTFish Oct 23 '19 at 07:26
  • Thanks i got it now. Can I close this question? Thanks for all the answers and yes i will use english variables in the future! – Wom Epic Oct 23 '19 at 07:29
  • @WomEpic Since this question has been edited a lot and doesn't clearly address a single problem, it probably won't be helpful for other people so it is probably appropriate to delete it. You will see a button near the bottom of the question. – BoBTFish Oct 23 '19 at 07:35
  • Don't edit the question in a way that invalidate existing answers. – Jarod42 Oct 23 '19 at 08:20

3 Answers3

2

The problem is here:

while(input != 0)
{
    if(input > two_euro)
    {
        input = input - two_euro;

If your input is at 2, then the while loop will keep going round, but the if well never be entered. You specifically check if the input is greater than 2, so you never completely reduce the input to 0.

Let's walk through an example:

input is 4
while (input != 0) => true, enter the loop
    if (input > 2) => true, enter the reduction step
        input = input-2 => input is 2
while (input != 0) => true, continue the loop
    if (input > 2) => false, skip the reduction step
while (input != 0) => true, continue the loop
    if (input > 2) => false, skip the reduction step
... continue forever

Supposing you fix the above problem by changing > to >=. What will happen if your input is not an even number? Not a whole number? Maybe your while condition shouldn't check for exactly 0, but instead check if the input is greater than 0?

You would need to change your reduction step to always take place while the input is greater than 0 as well, or you would get stuck in an infinite loop with your input at 1 (or any value between 0 and 2).

BoBTFish
  • 19,167
  • 3
  • 49
  • 76
  • i think i didnt explain my problem well. Please try to run the following program. Thats what my problem is : ` #include using namespace std; int main(){ int eingabe; int zweieuro = 2; cin >> eingabe; if(eingabe < 2) { eingabe = eingabe - zweieuro; cout << eingabe; } } ` – Wom Epic Oct 23 '19 at 07:05
0

With floating point computation, some rounding can prevent the result from being exact.
For example, if you accumulate ten times 0.1 and then test if this result is exactly 1.0, you will be surprised to see that it is not!
(on my computer I obtain an error of 1.1102230246251565e-16)

So, comparing the consequence of some alteration of input to exactly 0 will probably be incorrect.
Maybe should you consider an error margin.

prog-fh
  • 13,492
  • 1
  • 15
  • 30
  • 1
    While this is true, it's not the main problem in this case. This code exhibits a problem with any whole number inputs that are perfectly representable as a `double`. – BoBTFish Oct 23 '19 at 06:38
  • 1
    @BoBTFish I totally agree with you and your answer, but I think that as soon as the arithmetic part of the problem will be solved, the rounding problem will appear (exact comparison of two floating point numbers) and give the feeling that the arithmetic fix did not change anything. The two problems need to be considered simultaneously. – prog-fh Oct 23 '19 at 06:43
0

you need to change your while condition and your if also. Do some test/debug... check for example input equal to 2, 5...

moshe
  • 29
  • 1