如何修复:for循环不读取向量

时间:2019-06-15 19:53:54

标签: c++ for-loop vector

编写牛与牛游戏的学校作业。最终得分循环无效,我不确定原因是什么。

我尝试重命名向量,更改迭代器,更改在代码中声明/初始化向量的位置(仍然不确定确切的区别)

//Get Number to Guess
if (numlen == 0) {
    cout << "Enter the number to guess: ";
    cin >> num;
    cout << "Enter the number of digits in code: ";
    cin >> numlen;
    numstr = to_string(num);
    if (numstr.length() < numlen) {
        int diff = numlen - numstr.length();
        addz = (diff, "0");
        for (int z = 1; z <= diff; ++z) {
            numstr = addz + numstr;
        }
        num = stoi(numstr);
    }
    vector<int> numvct(numlen, 0);
    max1 = 1;
    for (l = 1; l < numlen; ++l) {
        max1 = max1 * 10;
    }
    for (j = max1, k = 0; j >= 1, k < numlen; j = j / 10, ++k) {
        int addval1 = num / j;
        num = num - (addval1 * j);
        numvct.at(k) = addval1;
    }
    cout << "Number to guess: ";
    for (r = 0; r < numlen; ++r) {
        if (r == (numlen - 1)) {
            cout << numvct.at(r) << endl;
        }
        else {
            cout << numvct.at(r) << "-";
        }
    }
}

else {
    //Fill vector to pick from
    for (i = 0; i <= 9; ++i) {
        pickvct.push_back(i);
    }

    //Pull to random number
    vector<int> numvct(numlen);
    for (k = 0; k < numlen; ++k) {
        tempnum1 = rand() % (pickvct.size() - 1);
        numvct.at(k) = pickvct.at(tempnum1);
        pickvct.erase(pickvct.begin() + tempnum1);
    }
    cout << "Number to guess: ";
    for (r = 0; r < numlen; ++r) {
        if (r == (numlen - 1)) {
            cout << numvct.at(r) << endl;
        }
        else {
            cout << r << "-";
        }
    }
}

//Get guess
do {
    do {
        cout << "Enter guess: ";
        cin >> guess;
        guessstr = to_string(guess);
        guesslen = guessstr.length();
        if (guesslen < numlen) {
            int diff = numlen - guesslen;
            addz = (diff, "0");
            for (int z = 1; z <= diff; ++z) {
                guessstr = addz + guessstr;
            }
            guess = stoi(guessstr);
            guesssame = true;
        }
        if (guesslen == numlen) {
            guesssame = false;
        }
        while (guesslen > numlen) {
            cout << "You can only enter " << numlen << " digits." << endl;
            cout << "Enter guess: ";
            cin >> guess;
            guessstr = to_string(guess);
            guesslen = guessstr.length();
        }
        for (s = 0; s < guesslen; ++s) {
            for (t = s + 1; t < guesslen; ++t) {
                if (guessstr.at(s) == guessstr.at(t)) {
                    guesssame = true;
                }
                else {
                    guesssame = false;
                }
            }
        }
        if (guesssame == true) {
            cout << "Each number must be different." << endl;
            guesssame = true;
        }
    } while (guesssame == true);
    vector<int> guessvct(guesslen, 0);
    max2 = 1;
    for (m = 1; m < guesslen; ++m) {
        max2 = max2 * 10;
    }
    for (n = max2, o = 0; n >= 1, o < guesslen; n = n / 10, ++o) {
        addval2 = guess / n;
        guess = guess - (addval2 * n);
        guessvct.at(o) = addval2;
    }

    //Check the guess
    for (p = 0; p < guesslen; ++p) {
        guessdigit = guessvct.at(p);
        cout << "Guess digit at " << p << ": " << guessdigit << endl;
        for (q = 0; q < guesslen; ++q) {
            numdigit = numvct.at(q);
            cout << "Num digit at " << q << ": " << numdigit << endl;
            if (numdigit == guessdigit && q == p) {
                bulls = bulls + 1;
                if (bulls == numlen) {
                    win = true;
                    break;
                }
                cout << bulls << " bulls" << endl;
            }
            else {
                if (numdigit == guessdigit && q != p) {
                    cows = cows + 1;
                    cout << cows << " cows" << endl;
                }
            }
        }
    }
} while (win == false);

为确保循环正常运行,我添加了cout语句,但它仅打印第一个语句: 输入猜测:#### 猜数字为0:# 程序完成

1 个答案:

答案 0 :(得分:0)

//Get Number to Guess
if (numlen == 0) {
  ...
}

else {
  //Fill vector to pick from
  ...
}

在if if块中,您有以下两行:

vector<int> numvct(numlen, 0);

vector<int> numvct(numlen);

这些行声明并初始化向量,这些向量在程序离开其各自的if / else块时超出范围。不过,您稍后会再次使用此向量numvct,因此我假设您还在任何显示的代码之前声明并初始化了它,并且它可能开始为空。因为if / else块中的这两个numvct向量超出了范围,所以您对它们所做的所有工作也会消失。这意味着,当您稍后再次尝试使用numvct时,您正在使用在程序一开始就声明的(大概)空值。

与其重新声明numvct,不如尝试调整其大小:

//vector<int> numvct(numlen);
      numvct.resize(numlen);

//vector<int> numvct(numlen, 0);
      numvct.resize(numlen, 0);

此外,您可能希望减少在此使用的变量数量,并尝试仅在真正需要它们的代码块内声明它们。这样可以更轻松地跟踪正在发生的事情。

编辑:我只想补充一点,我建议调整向量的大小,而不是可以对向量执行的任何其他操作,因为我真的不知道您对它们的确切用途是什么。通过这些更改,我运行了您的代码,并添加了〜25个其他变量声明,这些声明未包含在您的帖子中,并且它的工作看起来很合理,即允许我选择一个数字,猜测其数字,打印数字牛和公牛等。