Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

C++
#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;
}
Posted
Updated 12-Mar-18 22:58pm
v2
Comments
Mohibur Rashid 12-Mar-18 22:10pm    
Have you tried anything? Like, learning the language?

By the way, your second scanf missing %
Dominic Burford 13-Mar-18 2:50am    
Use the debugger and step through the code and find the problem yourself. Learning to debug and diagnose errors in code is part of the role of a software developer. You might actually learn something.

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:
C++
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
}
 
Share this answer
 
excerpt of 'man gets' output:


DESCRIPTION
Never use this function.



because gets calls may end up in buffer overruns.

Try
C
#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;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900