逻辑错误,通过引用传递的数量明显更高

Logic error, number passed by reference significantly higher

本文关键字:错误 引用      更新时间:2023-10-16

下面我粘贴了一些我写的代码。我遇到的问题是,当 turnmessage 将一个数字传递给 found()(通过引用)时,它会以某种方式增加。例如,如果传入 3,我会收到 300 万。

(以下在文件Projectmain.cpp)

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include "project1.h"
using namespace std;
project1 game;
string str(int number){
    stringstream ss;
    ss << number;
    string result = ss.str();
    return result;
}
void project1::setNames(){
    string name;
    cout<< "Player one, enter your name: ";
    cin >> name;
    playerOneName = name;
    cout<< "Player two, enter your name: ";
    cin >> name;
}
void project1::resetBoard(){
    for(int i = 0; i < 3; i++){
    RowOne[i] = str(i + 1);
    RowTwo[i] = str(i + 4);
    RowThree[i] = str(i + 7);
    }
    int used[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1};
    turn = 1;
}
void project1::printBoard(){
    for(int i = 0; i < 3; i++){
        cout << " | " << RowOne[i] << " | ";
    }
    cout << endl;
    for(int i = 0; i < 3; i++){
        cout << " | " << RowTwo[i] << " | ";
    }
    cout << endl;
    for(int i = 0; i < 3; i++){
        cout << " | " << RowThree[i] << " | ";
    }
    cout << endl;
}
string project1::PlayerOneName(){
    return playerOneName;
}
string project1::PlayerTwoName(){
    return playerTwoName;
}
bool project1::found(int& number){
    cout << " your number after pass: " << number;
    int i = 0;
    bool found = false;
    while(i < 9 && found == false){
        cout << used[i];
    if(used[i] == number)
        found = true;
    i++;
    }
    return found;
}
void project1::turnmessage(){
    int number = -1;
    bool found;
    do{
    if(turn == 1)
        cout << PlayerOneName();
        else
        cout <<PlayerTwoName();
    cout << " it's your turn!" << endl << "Enter the number you wish to use: ";
    cin >> number;
    number--;
    bool found = game.found(number);
    if (found == true)
        cout << "Sorry, that has already been used. Please try again.";
    }while(found == true);
}

int main(){
    game.setNames();
    game.resetBoard();
    game.printBoard();
    game.turnmessage();
return 0;   
}

(以下在文件项目1.h中)

#include<string>
#include<iostream>

using namespace std;
class project1{
public:
    void setNames();
    void resetBoard();
    void printBoard();
    void turnmessage();
    string PlayerOneName();
    string PlayerTwoName();
    bool found(int&);
private:
    int playerOneScore;
    int playerTwoScore;
    string playerOneName;
    string playerTwoName;
    string RowOne[3];
    string RowTwo[3];
    string RowThree[3];
    int used[9];
    int turn;
};

我认为问题就在这里,resetBoard

int used[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1};

这将创建一个名为 used 的局部变量,该变量等于 -1 的数组,而不是更改used数据成员以保存所有 -1。 若要解决此问题,请尝试将此代码替换为以下内容:

for (int i = 0; i < 9; i++) {
    used[i] = -1;
}

那里可能还有其他问题,但这肯定是可疑的。

希望这有帮助!

number 没有

更改为某个大值,project1::found是这个std::cout的输出让你感到困惑:

while(i < 9 && found == false){
    cout << used[i];

如果将其更改为此内容,则很明显发生了什么:

while(i < 9 && found == false){
    cout << std::endl << used[i];
            ^^^^^^^^^

您也可以在此处添加std::endl

cout << " your number after pass: " << number << std::endl ;