简体   繁体   English

当我在 C++ 中运行我的函数时,为什么我的代码显示数字 53

[英]Why is my code displaying a number 53 when I'm running my functions in c++

My friend created a program that prints a triforce to the console, however when he ran his code he realized that it printed weird numbers instead of what was supposed to run.我的朋友创建了一个向控制台打印三角力的程序,但是当他运行他的代码时,他意识到它打印了奇怪的数字而不是应该运行的数字。

The code looks like this:代码如下所示:

using namespace std;

#include <iostream>

void ZeldaTriangle(int Size){
    for (int  row = 0; row < Size; row++)
    {
        for (int spa = 0; spa < (Size-row-1); spa++)
        {
            cout << (" ");
        }
        for (int col = 0; col < (row +1); col++)
        {
            cout << ("@");
        }
        cout << "" << endl;
    }

}
void triforce_up (int Size, int character){
    for (int row = 0; row < (Size); row++)
    {
        for (int spa = 0; spa < (Size*2-row-1); spa++)
        {
            cout << (" ") << flush;
        }
        for (int col = 0; col < (row +1); col++)
        {
            cout << character << flush;
        }
        cout << "" << endl;
    }
}
void triforce_down (int Size, int character){
    for (int row = 0; row < (Size); row++)
    {
        for (int spa = 0; spa < (Size-row-1); spa++)
        {
            cout << (" ");
        }
        for (int col = 0; col < (row +1); col++)
        {
            cout << character;
        }
        for (int spa = 0; spa < ((Size-row-1)*2); spa++)
        {
            cout << (" ");
        }
        for (int col = 0; col < (row +1); col++)
        {
            cout << character;
        }
        cout << "" << endl;
    }
}
void print_ZeldaTriangle (int Size, int character){
    triforce_up (Size, character);
    triforce_down (Size, character);
}
int main() {
    int Size = 6;
    print_ZeldaTriangle(Size, '5');
}

It should display a triforce created with the number "5", but instead it shows this with the number 53.它应该显示用数字“5”创建的三角力,但它显示的是数字 53。

           53
          5353
         535353
        53535353
       5353535353
      535353535353
     53          53
    5353        5353
   535353      535353
  53535353    53535353
 5353535353  5353535353
535353535353535353535353

I have attempted to change the "int character" to "string character" in order to prevent the error from happening but that displays a broken triforce that looks nothing like it should look like.我试图将“int character”更改为“string character”以防止错误发生,但它显示了一个损坏的三角力,看起来不像它应该看起来的样子。

           5
          55
         555
        5555
       55555
      555555
     5          5
    55        55
   555      555
  5555    5555
 55555  55555
555555555555

"void ZeldaTriangle(int Size)" “void ZeldaTriangle(int Size)”

You don't need this function

when you convert char to int当您将 char 转换为 int 时

your output is somewhat like this:你的输出有点像这样:

5                    5
55       or         55
555                555
5555              5555
55555            55555
555555          555555

then your code is correct Syntactically it is printing what it is meant to print.那么你的代码在语法上是正确的,它正在打印它要打印的内容。 but logically you yourself is lacking basic imagination.但逻辑上你自己缺乏基本的想象力。

  • either you can insert spaces in between or insert extra value to it您可以在两者之间插入空格或为其插入额外的值

if you want output either like this or that...如果你想要这样或那样的输出......

     5                              5
    5 5                            555
   5 5 5                          55555
  5 5 5 5                        5555555
 5 5 5 5 5                      555555555
5 5 5 5 5 5                    55555555555

your code is good the only thing you need is some result imagination:)你的代码很好你唯一需要的是一些结果想象力:)

i have simplified the code: https://repl.it/repls/FluffyMildMenu我简化了代码: https ://repl.it/repls/FluffyMildMenu

And about the "53" error i don't know for sure but I think it is related to some charcater converted to int inside the code..关于“53”错误,我不确定,但我认为它与代码中转换为 int 的某些字符有关。

You are passing 5 as char but your functions accept integer value.您将5作为 char 传递,但您的函数接受整数值。 When you pass a char value your functions will use ASCII value as int value.当您传递一个 char 值时,您的函数将使用 ASCII 值作为 int 值。 If you look at ASCII table '5' represents 53. Please refer link below: https://www.ascii-code.com/如果您查看 ASCII 表,“5”代表 53。请参考以下链接: https ://www.ascii-code.com/

I guess, as you are expecting in in the我想,正如您所期望的那样

print_ZeldaTriangle (int Size, int character) {

}

and inputing this并输入这个

print_ZeldaTriangle(Size, '5');

The char is converted to int, which is bad as Char 5 when converted to int is 53 See this table for reference https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html char转换成int,转换成int的时候坏的是Char 5是53 参考这个表https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html

So, to fix this, do this所以,要解决这个问题,请执行此操作

int main() {
    int Size = 6;
    print_ZeldaTriangle(Size, 55);
}

EDIT:- Do 55, instead of 5编辑:- 做 55,而不是 5

https://repl.it/repls/NoteworthyEnviousFactor https://repl.it/repls/NoteworthyEnviousFactor

53 is the ASCII value of character '5', change function signature to 53 是字符 '5' 的 ASCII 值,将函数签名更改为

 void triforce_up (int Size, char character)
 void triforce_up (int Size, char character)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我在执行 C++ 代码时出错 - why I'm getting error in executing my C++ code 为什么我的 C++ 代码仍在运行? 我看不出理由 - Why is my C++ code still running? I see no reason 我是C ++的新手,正在努力弄清楚为什么我的代码可以正常运行,但是在运行后仍然返回荒谬的数字 - I am new to C++ and struggling to figure out why my code works perfectly but still returns an absurd number after running C ++-为什么我的cout代码显示的小数不一致? - C++ - Why is my cout code displaying decimals inconsistently? 我无法在 VSCode 中调试我的 C++ 代码 - I'm Unable to debug my C++ code in VSCode 为什么我的C ++ Windows窗体中有C#代码,而C ++函数却弹出错误? - Why do I have C# code in my C++ Windows Form and C++ functions are popping errors? 我不明白为什么我的c ++代码运行得这么慢 - I don't understand why my c++ code running so slow 当我在 Ubuntu 终端上发送 SIGTERM (Ctrl + C) 时,为什么我的 C++ 代码没有终止? - Why is my C++ code not terminated when I sent SIGTERM (Ctrl + C) on Ubuntu Terminal? 我的C ++代码出现错误! &如何在c ++中找到矩阵的行列式和逆矩阵? - I'm getting error in my c++ code! & How to find determinant and inverse of matrix in c++? 当我删除cout语句时,为什么我的C ++程序会因退出代码11而崩溃? - Why does my C++ program crash with exit code 11 when I remove a cout statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM