C++ 为什么我的for循环不起作用?

C++ 为什么我的for循环不起作用?,c++,for-loop,control-flow,C++,For Loop,Control Flow,代码如下。我希望它在您键入-1时退出循环。或者我想找到一个更好的解决方案,如果有的话 if (aa == "hours") { std::cout << "Please enter hours below: (Type -1 to stop)" << std::endl; for(x; x > -1; x++) { std::cin >> hours[x]; std::cout << hours

代码如下。我希望它在您键入
-1
时退出循环。或者我想找到一个更好的解决方案,如果有的话

if (aa == "hours") {
    std::cout << "Please enter hours below: (Type -1 to stop)" << std::endl;
    for(x; x > -1; x++) {
        std::cin >> hours[x];
        std::cout << hours[x];
    }
}
if(aa==“小时”){
标准:库特>小时[x];
std::cout使用简单的

if(aa==“小时”)
{
内部温度;
标准:温度>温度;
//检查温度是否为-1
如果(温度==-1)
{
打破
}
//此代码仅在temp不是-1时执行
小时[x]=温度;

std::我不能谢谢你,但我不想让我的程序退出,可以说,跳回到main上。@midcore
break
所做的就是停止循环。如果后面有代码,你的程序将不会退出。请补充你的问题,你看到的行为是什么,以及为什么这不是你所期望的。
if (aa == "hours") 
{
    int temp;
    std::cout << "Please enter hours below: (Type -1 to stop)" << std::endl;
    for(int x; x > -1; x++) {

        // Store input in temp
        std::cin >> temp;

        // Check if temp is -1
        if (temp == -1)
        {
            break;
        }

        // This code is only executed if temp is not -1
        hours[x] = temp;
        std::cout << hours[x];
    }
}