关于函数和循环;在循环中传递函数中的变量

Concerning functions and for loops; passing variables in functions within a loop

本文关键字:循环 传递函数 变量 于函数 函数      更新时间:2023-10-16

我的代码有问题。我正在尝试制作一个程序,在for循环之前初始化一系列变量,但当变量在循环中更改时,我希望它保持更改后的值。我正在尝试创建一个井字游戏,该游戏初始化了字符串变量,以表示当用户选择单元格编号和X或O时它将放置的位置。但是,它没有"记住"上一个回合,而是重置网格,只显示当前循环的X或O单元格选择。我全局声明了我的变量,所有函数都可以传递它们。

tl;dr:变量被重置为其原始赋值,这与我在循环之外声明和初始化它们的事实相反。

这是我的代码:

#include <string>
#include <fstream>
using namespace std;
string getCell();
string getSymbol();
void displayBoard(string c00,string c01,string c02,string c10,string c11,string c12,string c20,string c21,string c22);
string cell;
string xoro;
string line;
int menuchoice;
fstream file;
string c00,c01,c02,c10,c11,c12,c20,c21,c22;
int main(){
c00="-";
c01="-";
c02="-";
c10="-";
c11="-";
c12="-";
c20="-";
c21="-";
c22="-";
for (int i=1; i<=9; i++){
cout << "(1) Load previous grid" << endl << "(2) New Game" << endl;
cout << "Please enter one of the menu choices:  ";
cin >> menuchoice;
switch (menuchoice){  
case 1:
    file.open("savedgame.txt");
    if (!file){
        cout << "Error opening file";}
    while (file >> line){
        cout << line << endl;}
    file.close();
    break;
case 2:
    getCell();
    getSymbol();
    displayBoard(c00, c01, c02, c10, c11, c12, c20, c21, c22);
    break;
default:
    cout << "Please enter a valid Menu Choice.";
    }
}
}

string getCell(){
    do{
        cout << "Enter Valid Cell Name: ";
        cin >> cell;}
     while ((cell!="00")&&(cell!="01")&&(cell!="02")&&(cell!="10")&&(cell!="11")&&(cell!="12")&&(cell!="20")&&(cell!="21")&&(cell!="22"));
     return cell;}
string getSymbol(){
    do{
        cout << "Enter X or O: ";
        cin >> xoro;}
    while ((xoro != "O" && xoro != "o") && (xoro != "X" && xoro != "x"));
    return xoro;}
void displayBoard(string c00, string c01, string c02, string c10, string c11, string c12, string c20, string c21, string c22){
file.open("savedgame.txt");
        if (cell == "00")  
            c00 = xoro;
        else if (cell == "01")
            c01 = xoro;
        else if (cell == "02")
            c02 = xoro;     
        else if (cell == "10")
            c10 = xoro;     
        else if (cell == "11")
            c11 = xoro;     
        else if (cell == "12")
            c12 = xoro;     
        else if (cell == "20")
            c20 = xoro;     
        else if (cell == "21")
            c21 = xoro;     
        else if (cell == "22")
            c22 = xoro;

        cout <<"--"<<c00<<"--|--"<<c01<<"--|--"<<c02<<"--"<<endl<<"--"<<c10<<"--|--"<<c11<<"--|--"<<c12<<"--"<<endl<<"--"<<c20<<"--|--"<<c21<<"--|--"<<c22<<"--"<<endl;
        file <<"--"<<c00<<"--|--"<<c01<<"--|--"<<c02<<"--"<<endl<<"--"<<c10<<"--|--"<<c11<<"--|--"<<c12<<"--"<<endl<<"--"<<c20<<"--|--"<<c21<<"--|--"<<c22<<"--"<<endl;
        file.close();
}

这里的代码有各种问题。从最明显的一个开始:这里显示的程序不会编译,至少对我来说没有。你使用的是运算符cout和cin,而不使用头。此外,正如所指出的,您可能希望使用数组,而不是x个基本上相同的不同变量。我也没有看到保存用户输入到文件的行,你只需在displayboard()中打开它;可能是您的变量没有存储在任何地方的原因。

我建议您进一步研究数组以及如何操作数组中的元素。它将使您的代码更易于阅读,并为您和其他人维护。你也可以使用字符代替,因为你的单元格不会超过一个字符。因此,你可以简单地做:

char cell[10];
for(int i = 0; i < 10; i++){
cell[i] = '-';
}

这给了你一块3*3大小的木板。要操作单元格,只需向用户询问单元格以及他想在其中输入什么类型的字符即可。通过这样做,您可以使用一个以前有两个功能的函数:

int cellname;
cout << "Enter a valid cell name";
cin >> cellname;
cout << "Enter X or O: ";
cin >> xoro;
cell[cellname] = xoro;

我刚刚在你的代码中注意到的另一个问题是,在你的第一个for循环中,你要求用户从菜单中进行选择,你只让它重复9次。如果有人输入了9次默认菜单选项,程序将终止。您需要的是一个while语句,用于检查用户输入是否大于0或小于3。