【问题标题】:Different counters, same array. Array elements not changing. C++不同的计数器,相同的数组。数组元素不变。 C++
【发布时间】:2017-09-13 01:43:46
【问题描述】:

我试图让用户输入一个数字,然后仔细检查他们是否还没有输入该特定数字。我认为使用一个计数器将用户输入的数字存储在一个数组中,然后用一个 for 循环控制的计数器检查同一个数组就可以了,但是当我在数组元素中使用 for 循环计数器时,它似乎总是为零。有人可以在这里指出我正确的方向吗?我不知道我做错了什么。

#include <iostream>

using namespace std;

int main() {
    int guess[101] = { 0 };
    int count = 0;
    int check = 0;


    while (guess[count] != 1) {
        count++;
        cout << "Enter an integer." << endl;
        cin >> guess[count];
        if (guess[count] == 0) {
            return 0;
        }

        for (check = 0; check < count; check++) {
            cout << guess[check] << endl;
            if (guess[count] = guess[check]) {
                cout << "You already guessed " << guess[check] << ". Try a different integer between 1 and 100.\n" << endl;
            }
        }
    }
}

【问题讨论】:

  • if (guess[count] == guess[check]) double equal to 应该用于比较。
  • 哦。谢谢。每次迭代我都将元素改回零。这解决了它。

标签: c++ arrays counter


【解决方案1】:

if (guess[count] == guess[check]) double equal to 应该用于比较。

【讨论】:

    猜你喜欢
    • 2016-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    • 2023-01-10
    相关资源
    最近更新 更多