C ++中的数组错误

用户名

问题是我想要'o',而不要重复...我试图找到但失败了!还向我解释这是怎么发生的。我是一个新的学习型程序员!

#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>

using namespace std;

int main()
{
    //all in-game variables here
    string o = "o";
    int oH = 0;
    int oW = 0;
    //variables ending point...

    bool run = true;
    bool frameShow = true;
    char input;
    int width = 53;
    int height = 22;

    string px[width][height];

    while (run)
    {
        for (int xEmp=0; xEmp<height;    xEmp++){
            for (int yEmp=0; yEmp<width; yEmp++) 
            {
                px[xEmp][yEmp]= " ";
            }

            if (frameShow)
            {
                clrscr();           // Must be at start

                px[oH][oW] = o;

                for (int x=0; x<height; x++)
                {
                    for (int y=0; y<width; y++)
                    {
                        cout << px[x][y];
                    }
                    cout << "\n";

                    frameShow = false;   // Must be at end
                }
            }

            if (kbhit())
            {
                input = getch();

                // Most Used ones are:
                // char 119 for "w"
                // char 97 for "a"
                // char 115 for "s"
                // char 100 for "d"
                // char 32 for "space"
                // char 27 for ESC

                if (input == 119)
                {
                    oH--;
                    frameShow = true;
                }
                else if (input == 115)
                {
                    oH++;
                    frameShow = true;
                }
                else if (input == 97)
                {
                    oW--;
                    frameShow = true;
                }
                else if (input == 100)
                {
                    oW++;
                    frameShow = true;
                }
                else if (input == 27)
                {
                    // Game exits...

                    // To terminate, use:
                    run = false;
                }

                if(oH > height)
                {
                    oH = height;
                }
                else if(oH < 0)
                {
                    oH = 0;
                }

                if(oW > width - 1)
                {
                    oW = width - 1;
                }
                else if(oW < 0)
                {
                    oW = 0;
                }

            }
        }
    }

    // Output for confiming program termination
    clrscr();
    cout << "\n  - Terminated! - \n";
    return 0;
}
皮门特尔

作为开始,widthheight使用被改变。px[width][height];px[xEmp][yEmp]xEmp<heightyEmp<width有一次你把它作为px[width][height];,那么px[height][width];保持您的代码连贯!

也是if(oH > height) { oH = height; }错误的。从高度减去一。

这也可能不符合您的要求:

for (int x=0; x<height; x++)
   for (int y=0; y<width; y++)
   {
       cout << px[x][y];
   }
   cout << "\n";

正确使用支架,如果您不知道如何使用支架,请始终将支架放上!

同样,我认为您没有正确使用方括号:

for (int xEmp=0; xEmp<height;    xEmp++){
    for (int yEmp=0; yEmp<width; yEmp++) 
    {
        px[xEmp][yEmp]= " ";
    }
... // do other things
}

我认为您想立即将其关闭以将所有内容都放回太空,而他们会做其他工作。就像现在一样,它将仅将空行下一行设置为空格,打印所有内容并运行您的代码。

for (int xEmp=0; xEmp<height;    xEmp++){
    for (int yEmp=0; yEmp<width; yEmp++) 
    {
        px[xEmp][yEmp]= " ";
    }
}
... // do other things

ps:clrscr()是非标准功能,我认为仅适用于Windows,适用于Linuxsystem('clr');

Ps2:std::string如果仅存储字符,为什么要使用而不是char?

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在C ++中索引数组错误

来自分类Dev

c中的分段错误错误数组

来自分类Dev

C ++中的多维数组代码中的错误

来自分类Dev

C中的二维数组错误

来自分类Dev

函数错误C中的静态数组

来自分类Dev

函数错误C中的静态数组

来自分类Dev

在C中打印字符数组错误

来自分类Dev

c-数组中的错误计数结果

来自分类Dev

在 C 中使用数组(输出中的错误)

来自分类Dev

使用数组时 C 中的分段错误

来自分类Dev

C++ cout 数组中的错误数字

来自分类Dev

使用C中的数组修复输出中的小错误

来自分类Dev

在 c# 中的标签数组中获取错误的文本

来自分类Dev

C#中的数组搜索引发错误

来自分类Dev

在C扩展段错误中创建numpy数组

来自分类Dev

C中的灵活数组并取消引用类型化指针错误

来自分类Dev

在C中初始化数组时出现分段错误

来自分类Dev

在C ++中访问数组内存会引发错误

来自分类Dev

数组被错误地传递给C ++中的函数

来自分类Dev

尝试在C ++中创建动态数组时引发内存错误

来自分类Dev

在C中创建动态数组读取器,分段错误

来自分类Dev

C语言中的多维数组程序中的错误

来自分类Dev

C ++ Linux:在类中声明数组会导致分段错误

来自分类Dev

传递结构数组以在C ++中起作用并得到错误

来自分类Dev

C中的分段错误,同时声明了大指针数组

来自分类Dev

在C中对大型数组使用realloc的分段错误

来自分类Dev

通过指针计算c中的数组长度,答案是错误的

来自分类Dev

中止陷阱:c 中的数组有 6 个错误

来自分类Dev

在 C 中写入 char* 数组会引发段错误