为什么 char 在 C++ 中不被当作数字?

Why char is not taken as numeral in C++?

我用 C 写了一个代码,运行 非常完美,我 运行 将它转换为 C++。它给出了错误的输出。我有一个迭代,我将输入和迭代器变量都用作 char 来保存 space。但并没有像预期的那样表现。

unsigned char repeat, i;  
cin >> repeat;
for(i= 0; i < repeat; i++)

的等效行是什么
scanf("%hhi", &repeat)

?

Why char is not taken as numeral in C++?

因为C和C++是不同的编程语言。

例如参见 [​​=14=] 项目。

编程语言是规范,通常写在一些文档中。它的semantics matters as much as its syntax

如果您使用 GCC, I suggest to invoke 的某些最新变体编译 C++ 源代码,它会作为 g++ -Wall -Wextra -g 启用大量警告和一些调试信息。

考虑使用一些 static program analysis tools, e.g. Frama-C or Clang static analyzer or Coverity 等....

另读How to debug small programs and about Undefined behavior

另请参阅this site about C and C++ and look into RefPerSys (some "interesting" C++ free software project to which I actively contribute, AGI相关)。

研究现有 C++ 的源代码,至少是为了获得灵感 open source projects. Qt or the fish shell comes immediately to mind (but thousands of others exist on github or on gitlab or elsewhere, e.g. Ghudi, PPL, Fox toolkit, FLTK). You'll learn a lot by actively contributing to one of them (perhaps even RefPerSys)

顺便说一句,我强烈建议 显式 初始化 C 或 C++ 中的每个数字变量(所以代码 unsigned char repeat=0, i=0; 在你的情况下)。当它的行为变得更加确定时,任何足够好的 optimizing compiler would remove them when they are useless, and debugging your program (e.g. with gdb, notably on GNU/Linux systems such as Ubuntu or Debian) 都会变得更容易。