content

closed account (3Cj3AqkS)
content
Last edited on
When isolating code seemingly solves the problem, analyzing the differences between the isolated and the actual code is recommended.


How does this, which occurs in the actual code:
1
2
3
4
            cin >> userMoney;
            userMoney+=userMoney;
            // ...
            if (userMoney < 0 || userMoney > 1.00) {  // ...      


differ from this, from the isolated code:
1
2
3
4
        cin>>userMoney;

        if (userMoney < 0 || userMoney > 1.00)
        {


Last edited on
Line 24: Do not loop on !eof(). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> operation as the condition in the while statement.
1
2
3
  while (cin >> var) 
  {  //  Good cin or istream operation
  }


Lines 51,58,66,73,81,100: What's the purpose of adding userMoney to itself?

Lines 52,59,67,74,82: Shouldn't the number of each type of soda be a member of your SodaMachine struct rather than having separate variables?

Lines 52,59,67,74,82: You decrement the number of sodas without checking that there are sufficient number of sodas to satisfy the request.

Lines 47-84: Every one of these if branches is essentially identical. You should have a function to handle the repeated logic.
Last edited on
Topic archived. No new replies allowed.