不懂错在哪里

题目:声明两个int型变量或赋值,判断两数之和,,如果大于等于50,输出hello,world
运行输入30 50,再按一次回车会直接跳出去,不会显示hello,world
#include<iostream>
using namespace std;
int main()
{
int n = 0, i = 0;
printf("请输入两个数:");
scanf_s("%d/t%d", n, i);
if ( n + i >= 50)
printf("/nhello,world");
getchar();
getchar();
}
and the English is?
There is no question there either.

The program looks to do what the prompt asks. The only obvious problems are:

  • that it includes a C++ header but uses C I/O
  • not passing the addresses of the arguments to scanf

scanf_s("%d%d", &n, &i);

?
我竟然在这看见了国人,我来回答吧。
你用scanf_s函数输入变量值的时候, n 和 i 的前面要加上&,因为它需要变量的地址。这是这个C函数的要求
还有你的两个%d之间不知道为什么要加上/t,要去掉。否则不能正常输出,或者你改成\t也行。还有,你的printf里面,你用/n可能是想换行,但是换行要用 \n , 不要用/n, 还有最后那两个getchar()是可能是用来阻止程序运行完后直接跳出去的?
如果你想让程序停一下,那就用system("pause");
Last edited on
As translated:

I actually saw Chinese people here, let me answer.
When you use scanf_s to enter variable values, n and i are preceded by & because it requires the address of the variable. This is the requirement of this C function
Also, I don't know why you have to add /t between your two %ds, you need to remove them. Otherwise, it cannot be output normally, or you can change it to \t. Also, in your printf, you may want to use /n to wrap a line, but use \n instead of /n, and the last two getchar() may be used to prevent the program from jumping directly after running out?
If you want the program to pause, use system("pause");
RE: getchar and system pause

Better yet, use neither. Run your program directly from the command prompt.

Once you have compiled, use File Explorer to find the executable. Click on the Location Bar at the top and type “cmd” and press Enter.

A Windows Console or Windows Terminal (whichever you have configured) will pop up, showing you a prompt something like:

    
C:/Users/linaya/source/HW1/x64/Release/>


Type the name of your program’s executable, including any required command-line arguments, and watch it run.

C:/Users/linaya/source/HW1/x64/Release/> HW1.exe
30 50
hello,world
C:/Users/linaya/source/HW1/x64/Release/> _
 

Good luck!
I'll chime in and say it might be helpful to use code tags:
Please learn to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
using namespace std;
int main()
{
   int n = 0, i = 0;
   printf("请输入两个数:");
   scanf_s("%d/t%d", n, i);
   if (n + i >= 50)
      printf("/nhello,world");
   getchar();
   getchar();
}
This is using C I/O functions exclusively, including <iostream> is a bit odd. Odd, not wrong. <stdio.h> or <cstdio> is a more reasonable header to include, whether the code is compiled as C or C++.
It is wrong to not include the required header and expect the compiler to magically fix or include it for you.
Registered users can post here. Sign in or register to post.