我该如何纠正这个? [英] How do I correct the this?

查看:81
本文介绍了我该如何纠正这个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在这里运行一个简单的程序。预期的输出应该是:



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;
}

推荐答案

您必须学习基本的字符串操作。学习一些 C ++教程并观看一些视频。



你也应该参加IC和电话号码为 string ,以便更好地处理,如下所示:
You must learn the basic string operations. Learn some C++ tutorial and watch some videos.

You should take also the IC and phone number as string for better processing like here:
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
}


摘录' man获取'输出:



excerpt of 'man gets' output:



描述

永远不要使用此功能。




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;
}


这篇关于我该如何纠正这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆