嵌套for循环未完成其循环

时间:2012-02-16 05:24:16

标签: c++ loops cin

当我运行程序的这一段时,唯一可行的循环是涉及'week'变量的最后一个for循环。

cout << "Enter Building: ";
    cin >> building;
    cout << "\n";

    cout << "Enter Room: ";
    cin >> room;
    cout << "\n";

    cout << "Enter Numeric Month: ";
    cin >> monthIndex;
    monthIndex = monthIndex - 1;
    cout << "\n";

    cout << "Enter Week: ";
    cin >> week;
    cout << "\n";

    for (;building <= 30; building++)
    {
        for (;room <= 24; room++)
        {
            for (;monthIndex <= 11; monthIndex++)
            {
                for (;week <= 4; week++)
                {
                    cout << "Building: " << building << "\n";
                    cout << "Room: " << room << "\n";
                    cout << "Month: " << month[monthIndex] << "\n";
                    cout << "Week: " << week << "\n\n";
                }
            }
        }
    }

示例输出:

按1打印所有售票簿

按2打印特定的售票簿

按3退出程序

2 菜单选择

输入建筑物:26

输入房间:20

输入数字月份:8

输入Week:2

建筑:26 房间:20 月:八月 周:2

建筑:26 房间:20 月:八月 周:3

建筑:26 房间:20 月:八月 周:4

1 个答案:

答案 0 :(得分:1)

您需要在最后一个内部week循环结束时将1设置为for