本文介绍了我该如何纠正这个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在这里运行一个简单的程序。预期的输出应该是:
I tried to run a simple programme here.The expected output should be :
Please enter customer'name
Kent Low
Please Enter Customer's Ic number
97xxxxxxxx
Please enter customer's phone number
0113838xxxx
相反,在我运行调试后它会显示如下:
Instead it show up like this after I run the debug:
Please enter customer'name
Kent Low
Please Enter Customer's Ic number
Kent Low
Please enter customer's phone number
我尝试过:
What I have tried:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char name[20];
int IC;
int p_num;
printf("Please enter customer's name:\n");
gets(name);
printf("Please enter customer's IC number:\n");
scanf("%d",&IC);
printf("please enter customer's phone number:\n");
scanf("d",&p_num);
return 0;
}
推荐答案
char buffer[20] = {0};
buffer[0] = IC[0];//set first
buffer[1] = IC[1];//set second
for( int i = 2; i < strlen(IC);i++ ) {
buffer[i] = 'X';//set char X
}
描述
永远不要使用此功能。
DESCRIPTION
Never use this function.
,因为获取
电话可能会收到缓冲区超支。
尝试
because gets
calls may end up in buffer overruns.
Try
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20
int main()
{
char name[SIZE];
int IC;
int p_num;
printf("Please enter customer's name:\n");
if ( ! fgets(name, SIZE, stdin) )
return -1; // TODO: better error handling
// TODO: remove (if present) the trailing newline
printf("Please enter customer's IC number:\n");
if ( scanf("%d",&IC) != 1)
return -1; // TODO: better error handling
printf("please enter customer's phone number:\n");
if ( scanf("%d",&p_num) != 1)
return -1; // TODO: better error handling
printf("Values entered name='%s', IC=%d, p_num=%d\n", name, IC, p_num);
return 0;
}
这篇关于我该如何纠正这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!