CLion C++:为什么输入光标和当前输出之间有间隙?

q3aa0525  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(190)

why is this happening, there shouldn't be any space between 10 and the input
The input is 'Potato' and the output is showing after 10.
这是真正的代码-

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int a = 5;
  6. int b = 6;
  7. cout << endl <<a+b;
  8. for (int i = 0; i <= 10; ++i) {
  9. cout << endl << " Number " << i;
  10. }
  11. string name;
  12. cin >> name;
  13. cout << endl << endl << name;
  14. return 0;
  15. }
fruv7luv

fruv7luv1#

我不知道你读什么来做这些事情,因为:
1.没有人这样做:cout << endl << " Number " << i;。你应该学会像这样使用它:cout << " Number " << i<<endl;,如果你特别想在第一行加一个换行符,你可以只在第一行加cout<<endl;cout<<"\n";
1.根据我对你问题的理解,我认为你没有抓住重点。在" Number "之间有一个空格,您可能应该将其更改为"Number "
1.您明确地、明确地接受了输入,并在最后打印了'potato'的输入。本部分:

  1. string name;
  2. cin >> name;
  3. cout << endl << endl << name;

在这部分之后:

  1. for (int i = 0; i <= 10; ++i) {
  2. cout << endl << " Number " << i;
  3. }

很明显,您输入的文本“potato”在10之后打印。
这段代码可以帮助你:

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. string name;
  6. for (int i = 0; i <= 10; ++i)
  7. {
  8. cout << "Number: " << i << endl;
  9. }
  10. cin >> name;
  11. cout << name;
  12. return 0;
  13. }

这段代码显示了如下输出:
我希望这对你有帮助!!!

展开查看全部

相关问题