for-loopoutputstream

No output from 'for' loop - indefinite run


As the title says, trying to find out what prevents for-loop from giving prompts and taking input, currently shows nothing on execution and sits running. The goal is to gather the info and store it in an array for later. Here is the relevant class and how it is being used: /////////////////////////////////////////////////////////////////////////////////////////

class beverage{
public:
    string name;
    string description;
    float serveSize;
    int calories;
    float price;

    void setName(){
        cout << "Please enter the beverage name: " << endl;
        cin >> name;
    }
    void setDescription(){
        cout << "Please enter the beverage description: " << endl;
        cin >> description;
    }
    void setServeSize(){
        cout << "Please enter the serving size: " << endl;
        cin >> serveSize;
    }
    void setCalories(){
        cout << "Please enter the amount of calories for the beverage: " << endl;
        cin >> calories;
    }
    void setPrice(){
        cout << "Please enter the price of the beverage: " << endl;
        cin >> price;
    }
    void getName(){
        cout << "Beverage: " << name << endl;
    }
    void getDescription(){
        cout << "Description: " << description << endl;
    }
    void getServeSize(){
        cout << "Serving Size: " << serveSize << endl;
    }
    void getCalories(){
        cout << "Calories: " << calories << endl;
    }
    void getPrice(){
        cout << "Price: " << price << endl;
    }
    void toString(){
        cout << "Beverage: " << name << endl;
        cout << "Description: " << description << endl;
        cout << "Serving Size: " << serveSize << endl;
        cout << "Calories: " << calories << endl;
        cout << "Price: " << price << endl;
    }
};
int main(){
vector<string> beverages(0,0);
string cont = "yes";
int i;
beverage obj;
while (cont == "yes"){
    for (i=0;i++;){
    obj.setName();
    beverages.insert(beverages.end(), obj.name);
    obj.setDescription();
    beverages.insert(beverages.end(), obj.description);
    obj.setServeSize();
    string serveSize= to_string(obj.serveSize);
    beverages.insert(beverages.end(), serveSize);
    obj.setCalories();
    string calories= to_string(obj.calories);
    beverages.insert(beverages.end(), calories);
    obj.setPrice();
    string price= to_string(obj.price);
    beverages.insert(beverages.end(), price);
    cout << "Create another beverage?" << endl;
    cin >> cont;
}
}
return 0;
};

Solution

  • Your for loop is infinite because your condition is infinite. for (i=0;i++;) should be for (i=0;i<n;++i). once 'i' is smaller than 'n' the loop stops.

    To me it seems though your for-loop is entirely obsolete. The correct way to do your code would be:

     while (cont == "yes") {
     
     '''''''''''''
    
     cout << "Create another beverage?" << endl;
     cin >> cont;
    }
    

    This code would give you an infinite loop that stops only when the user doesn't input 'yes'. The for-loop is completely unnecessary.

    Read While loop & For loop for better understanding.

    Hope I Helped, Cheers ;)