[英]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.
但逻辑上你自己缺乏基本的想象力。
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.