为什么前两行在第一个循环之后会一起循环?

时间:2019-03-21 03:17:56

标签: c++ loops

我正在此循环上打印要订购的餐点。我想将多个对象存储在一个数组中,以便在要输入多个订单时可以打印它们。它运行第一个循环没有任何问题,但是,当它运行到第二个循环时,则输入了前两行,而用户无法进入该主菜,则直接进入了侧面。任何建议将不胜感激。

#include "pch.h"
#include <iostream>
#include <string>

using namespace std;

class ComboMeal {
public:

    string entree;
    string side;
    string sideSize;
    string drink;
    string drinkSize;

    void printMeal() {
        std::cout << "Your combo meal is: " << endl;
        std::cout << "Entree: " << entree << endl;
        std::cout << "Side: " << sideSize << ' ' << side << endl;
        std::cout << "Drink: " << drinkSize << ' ' << drink<< endl;
    }



};

int main()
{
    ComboMeal comb[20];
    char userresponse = 'Y';

    if (userresponse != 'N')
        for (int i = 0; i <= 20; i++) {

            std::cout << " " << endl;
            std::cout << "Would you like a Hamburger, Cheeseburger, or chicken?" << endl;
            std::getline(cin, comb[i].entree);
            std::cout << "Would you like fries, tots, or a salad for your side?" << endl;
            std::getline(cin, comb[i].side);
            std::cout << "What size would you like your side?" << endl;
            std::getline(cin, comb[i].sideSize);
            std::cout << "What would you like to drink?" << endl;
            std::getline(cin, comb[i].drink);
            std::cout << "What size drink would you like?" << endl;
            std::getline(cin, comb[i].drinkSize);
            std::cout << "Would you like another meal?" << endl;
            std::cin >> userresponse;
            std::cout << " " << endl;
        }

    else
    {
        for (int j = 0; j <= 20; j++) {
            comb[j].printMeal();
        }
    }
}

0 个答案:

没有答案