我正在学习c ++,代码到目前为止,直到 我做了数组变量来调用函数而不是字符串,但出了点问题,我无法弄清楚它是什么。问题是,它只能正确地传达前2个字母,然后它将其余部分称为└└└└符号
以下是代码:
#include <iostream>
#include <string>
#include <windows.h>
#include <conio.h>
using namespace std;
int land(){
HANDLE hConsole;
hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
char H = 72;
cout<<H;
}
int player(){
HANDLE hConsole;
hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE);
char X = 88;
cout<<X;
}
/*previously,i declared a string inside worldgen,and generated 2d array inside the for loop,but when i changed variables to call a function,first letters were X and H,but then it went └└└└└└└└└└└└└└└└└└└└└└└ for all the remaining characters.What's wrong here? */
int worldgen(int dimX,int dimY,int x,int y){
HANDLE hConsole;
hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
system("TITLE MyTitleText");
int H = land();
int X = player();
string world[dimX][dimY];
for(int c = 0;c<dimY;c++){
for(int count = 0;count<dimX;count++){
world[count][c] = H;
world[x][y] = X;
cout<<world[count][c];
}
cout<<endl;
}
}
int main(){
HANDLE hConsole;
hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
worldgen(70,15,10,10);
cin.get();
}
答案 0 :(得分:2)
land()和player()都没有返回任何内容,因此H和X都没有得到任何有意义的数据,只是未初始化的垃圾数据。
我很惊讶这甚至是编译,因为你有两个函数应该返回int但是没有设置返回任何东西。