-4

I found a post where FOR loop is used without CONDITION value. Here is a loop:

for (INITIALIZATION; CONDITION; AFTERTHOUGHT) 
{
    // Code for the for-loop's body goes here.
}

It's not safe to skip CONDITION value, but if you use if/else statement it can be done. Please take a look on my for loop: for (int i = 1; ; i++) and implementation inside. For some reason, I don't get proper logic with if/else statement.

#include <iostream>
using namespace std;

int main() {

    int boxes;
    int boxes_for_sale;

    cout << "Enter quantity of the boxes in warehouse: > " << flush;
    cin >> boxes;
    cout << "Enter quantity of the boxes for sale: > " << flush;
    cin >> boxes_for_sale;

    for (int i = 1;; i++) {

        if (boxes < boxes_for_sale) {
            cout << "There are not enough boxes in warehouse!" << endl;
            cout << "Enter quantity of the boxes for sale: > " << flush;
            cin >> boxes_for_sale;
        }
        else
            boxes -= boxes_for_sale;
            cout << "Car N:" << i << " is full\n" << "You have " << boxes << "boxes for sale" << endl;

        if (boxes == 0)
            cout << "Sold out!" << endl;
            break;

    }
    return 0;
}
Serge V.
  • 3,377
  • 3
  • 20
  • 28
  • 8
    You're missing some curly brackets `{` `}` in a couple of places. – 1201ProgramAlarm Jan 21 '16 at 04:10
  • 3
    As @1201ProgramAlarm says, you need braces around your code following the if and else statements. Also, "It doesn't work properly" is not a good problem description. You should add information about *how* it doesn't work and what you expected to happen. – elixenide Jan 21 '16 at 04:15

4 Answers4

4

Since you did not specify the desired behaviour of program, I will only state what is happening in the code right now.

important snippets of code that may be doing something unexpected:

 for (int i = 1;; i++) 

This is perfectly legal, but notice there is no termination condition specified. you will run into an infinite loop unless you break out of it.

else
    boxes -= boxes_for_sale;
    cout << "Car N:" << i << " is full\n" <<
    "You have " << boxes << "boxes for sale" << endl;

I can guess based on the indentation that you want both lines to execute only when the if statement preceding the else is false, but this is not what is happening.

Since you have not specified the else block with curly braces, only the first statement after the else will execute when you need it to. The cout will always execute.

You have the same issue with:

if (boxes == 0)
        cout << "Sold out!" << endl; //will output only if boxes==0
        break; //will break out of loop in any case
ForeverStudent
  • 2,487
  • 1
  • 14
  • 33
1
#include <iostream>
using namespace std;

int main() {

    int boxes;
    int boxes_for_sale;

    cout << "Enter quantity of the boxes in warehouse: > " << flush;
    cin >> boxes;
    cout << "Enter quantity of the boxes for sale: > " << flush;
    cin >> boxes_for_sale;

    for (int i = 1;; i++) {

        if (boxes < boxes_for_sale) {
            cout << "There are not enough boxes in warehouse!" << endl;
            cout << "Enter quantity of the boxes for sale: > " << flush;
            cin >> boxes_for_sale;
        }
        else{
            boxes -= boxes_for_sale;
}
            cout << "Car N:" << i << " is full\n" << "You have " << boxes << "boxes for sale" << endl;

        if (boxes == 0)
 {           cout << "Sold out!" << endl;
            break;}

    }
    return 0;
}
1

Take care of braces, you need to put braces for each for loop, if statement and else statement.

Also try to take care of indentation for good programming practice.

#include <iostream>
using namespace std;

int main() 
{

    int boxes;
    int boxes_for_sale;

    cout << "Enter quantity of the boxes in warehouse: > " << flush;
    cin >> boxes;
    cout << "Enter quantity of the boxes for sale: > " << flush;
    cin >> boxes_for_sale;

    for (int i = 1;; i++) {

        if (boxes < boxes_for_sale) 
        {
            cout << "There are not enough boxes in warehouse!" << endl;
            cout << "Enter quantity of the boxes for sale: > " << flush;
            cin >> boxes_for_sale;
        }
        else
        {
            boxes -= boxes_for_sale;
            cout << "Car N:" << i << " is full\n" << "You have " << boxes <<       boxes for sale" << endl;
        }
        if (boxes == 0)
       {
            cout << "Sold out!" << endl;
            break;
       }

    }
    return 0;
}
Muhammad Zeeshan
  • 470
  • 7
  • 24
1

Sorry, guys for lack of description. This is my first question here, I'll be more specific next time.

This is what I tried to do.

int main() {

    int boxes_in_warehouse;
    int boxes_for_sale;
    cout << "Enter quantity of the boxes in warehouse: " << flush;
    cin >> boxes_in_warehouse;
    cout << "Enter quantity of the boxes for sale: " << flush;
    cin >> boxes_for_sale;

    if (boxes_in_warehouse < boxes_for_sale) {
        cout << "No, Enter quantity of the boxes for sale: " << flush;
        cin >> boxes_for_sale;
    }

    boxes_in_warehouse -= boxes_for_sale;

    while (boxes_in_warehouse > 0) {
        cout << "You have " << boxes_in_warehouse << " left" << endl;
        cout << "Please enter quantity of the boxes for sale again" << endl;
        cin >> boxes_for_sale;
        boxes_in_warehouse -= boxes_for_sale;
    }

    cout << "we are sold out" << endl;
    return 0;
}

The code supposed to run till boxes_in_warehouse == 0 means sold out.

if you can optimize my last code, I will be happy to see your version.

Anton Savin
  • 40,838
  • 8
  • 54
  • 90
Serge V.
  • 3,377
  • 3
  • 20
  • 28
  • Three things to note: this isn't really an answer. It would work better as an addendum to your question. Second, note that you do not test that `boxes_for_sale` is less than or equal to `boxes_in_warehouse` in the while loop, allowing for some really dodgy results. Third, none of the `cin >> `s are tested to ensure the user is putting in valid numbers. The user could input a non-number and break your program, probably trapping it in an infinite loop, but the result of reading cin when it is in an error state is undefined. – user4581301 Jan 21 '16 at 06:38
  • Thanks for comments, it's useful info for me. How do I test cin >> for valid number? – Serge V. Jan 21 '16 at 20:28
  • See [Why would we call cin.clear() and cin.ignore() after reading input?](http://stackoverflow.com/questions/5131647/why-would-we-call-cin-clear-and-cin-ignore-after-reading-input) for a good example and how to clean up after bad input. `while (!cin >> boxes_in_warehouse) {cout << "bad input. Try again"<::max(), '\n');} ` will keep asking for a number until the user provides one. There are still a few things wrong with it (user can still put in impossibly long numbers and numbers followed by crap) but should cover what you need – user4581301 Jan 21 '16 at 20:44