What is the Correct Way to Initialize a Character in C Programming?

AI Thread Summary
The discussion revolves around a programming issue where a C program fails to prompt for multiple inputs and closes immediately after the first input. The problem stems from using `scanf("%c", &ident.name);`, which reads only a single character into a char variable. This causes confusion when subsequent `scanf` calls are made, as they do not receive the expected input. The solution involves changing the `name` variable from a char to a string (char array) and using `scanf("%s", ident.name);` instead. This adjustment allows the program to correctly handle multiple inputs, and after implementing these changes, the program compiles and runs successfully without errors.
nard
Messages
16
Reaction score
0
the foolowing program is to read inputed data and display them all at once but when i run it it asks for the first information and then runs the total program without asking for the input of other information.after that it doesn't respond and closes immediatelly.
can someone help me and tells me what's wrong with my program?
thanks for your help.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct database {
char name;
int phone_number,id_numb;
};
main()
{
struct database ident;
printf("enter a name\n");
scanf("%c",&ident.name);
printf("enter a phn numb\n");/* enter a phone number*/
scanf("%d",&ident.phone_number);
printf("enter the id_numb\n");/*enter an id number*/
scanf("%d",&ident.id_numb);
printf("the identity of %s is:\n",ident.name);/*displays the name*/
printf("phone:%d id:%d \n",ident.phone_number,ident.id_numb);/*displays the phone number and the id number*/
system("PAUSE");
}
 
Technology news on Phys.org
nard said:
scanf("%c",&ident.name);
the scanf("%c",...) is the issue. This reads a single character into the single character variable name. If you enter more than one character, scanf will attempt to handle those with the following scanf()'s.

Perhaps you'd want to use a string, which will solve the problem:

char name[128];

scanf("%s", ident.name);

printf( "... %s ...", ident.name);
 
I saw where i was making an error.
i corrected and while initialising the character i defined as an array and that way in the printf and scanf functions instead of using %c i used %s.after that the program is compiled and run without complications
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top