Problem with funcion scanf jump to the next scanf in C - c

I have a problem idk why jump from scanf name to scanf age, why dont let me put the address?
I already try only with %s instead of %[^\n]s
#include <stdio.h>
struct contact
{
char name[40];
char address[80];
int age;
long phone;
};
typedef struct contact contact_L;
int main(void)
{
contact_L c1;
printf("Enter Name:");
fflush(stdin);
scanf("%[^\n]s",c1.name);
printf("Enter Address:");
fflush(stdin);
scanf("%[^\n]s",c1.address);
printf("Enter Age:");
fflush(stdin);
scanf("%d",&c1.age);
printf("Enter Phone:");
fflush(stdin);
scanf("%ld",&c1.phone);
printf("Name: %s \n",c1.name);
printf("Address: %s \n",c1.address);
printf("Age: %d\n",c1.age);
printf("Phone: %ld\n",c1.phone);
return (0);
}
The output look like this ( don't let me enter the address and jump to the age )
Enter Name:jose herrera
Enter Address:Enter Age:31
Enter Phone:4567890
//printf
Name: jose herrera
Address:
Age: 31
Phone: 4567890

try
scanf(" %s",c1.address);
While Taking address input. With a preceding space

Related

Program get stuck after scanf() function

I am required to create a program which requires user to input the new born baby by implementing structure. However, after printing the message of "father age" for asking user to input the age, the program straight away terminated itself. Why is it happened and how to solve it?
#include <stdio.h>
#include <stdlib.h>
struct father{
char dad_name[50];
int dad_age;
};
struct mother{
char mom_name[50];
int mom_age;
};
struct baby{
char baby_name[50];
char sex[6];
char birthday[10];
struct father father1;
struct mother mother1;
};
struct baby *b1;
void display(int);
int main(){
int baby_num;
printf("Enter number of baby that is/are going to input: ");
scanf("%d", &baby_num);
display(baby_num);
printf("\nNEW BORN IN KUANTAN HOSPITAL\n");
for(int i=0; i<baby_num; i++){
printf("\nBaby %d\n", i+1);
printf("Baby Name: %s\n", b1[i].baby_name);
printf("Sex: %s\n", b1[i].sex);
printf("Birthday: %s\n", b1[i].birthday);
printf("\nFather name: %s\n", b1[i].father1.dad_name);
printf("Father age: %d", b1[i].father1.dad_age);
printf("\nMother name: %s\n", b1[i].mother1.mom_name);
printf("Mother age: %s\n", b1[i].mother1.mom_age);
};
return 0;
};
void display(int baby_num){
int temp;
b1 = (struct baby*) malloc(baby_num * sizeof(struct baby));
for(int i=0; i<baby_num;i++){
printf("Baby Name: ");
scanf("%d", &temp);
scanf("%[^\n]", b1[i].baby_name);
printf("Sex: ");
scanf("%s", b1[i].sex);
printf("Birthday: ");
scanf(" %s", b1[i].birthday);
printf("Father name: ");
scanf("%d", &temp);
scanf("%[^\n]", b1[i].father1.dad_name);
printf("Father age: ");
scanf("%d", b1[i].father1.dad_age);
printf("Mother name: ");
scanf("%[^\n]", b1[i].mother1.mom_name);
printf("Mother age: ");
scanf(" %d", b1[i].mother1.mom_age);
};
}
in display function, for father's age and mother's age, you need to send scanf function wrong value.
scanf("%d", &(b1[i]).father1.dad_age);
This solved the problem for me.

Program producing a bus error when reading in using scanf - C Program

I'm writing a program for an employee database and I'm writing the function to add an employee. I'm getting a bus error after my final prompt to scan in info. I'm pretty sure its to do with my scanf statement as I have a print statement right after that is not printing. Why would I be getting this error?
The prompt in question is for reading in job title.
void addEmployee(void)
{
char *name;
char gender;
int age;
char *title;
printf("Enter name: \n");
scanf(" %100s", name);
scanf("%*[^\n]%*c");
printf("Enter gender: \n");
scanf(" %1c", &gender);
scanf("%*[^\n]%*c");
printf("Enter age: \n");
scanf(" %d", &age);
scanf("%*[^\n]%*c");
printf("Enter job title: \n");
scanf(" %100s", title);
scanf("%*[^\n]%*c");
printf("Test");
printf("The employee you've entered is: %s %c %d %s \n", name, gender, age, title);
Employee newEmp = {*name, gender, age, *title};
if(employeeList[0] == NULL)
{
employeeList[0] = &newEmp;
nodeCount++;
}
}
Code is passing in an uninitialized pointer.
char *name; // Pointer 'name' not initialize yet.
printf("Enter name: \n");
// 'name' passed to scanf() is garbage.
scanf(" %100s", name);
Instead, pass a pointer to an existing array
char name[100 + 1];
printf("Enter name: \n");
// Here the array 'name' coverts to the address of the first element of the array.
// scanf receives a valid pointer.
scanf("%100s", name);

Struct and Pointer in C (Assigning string into struct)

I'm new to C and i currently studying about pointer and struct. But it seems like i have a problem when assigning value into my struct.
This is my code:
#include <stdio.h>
typedef struct
{
char name[30];
int age;
int birth;
}
student;
void record(student *sp);
int main(void)
{
student std1;
record(&std1);
printf("%i, %i %s\n", std1.birth, std1.age, std1.name);
}
void record(student *sp)
{
printf("Name: ");
scanf("%s", sp -> name);
printf("Birth: ");
scanf("%i", &sp -> birth);
printf("Age: ");
scanf("%i", &sp -> age);
}
Run program:
./struct
Name: David Kohler
result:
Birth: Age: 0, 0 David
What i don't understand is when i'm going to assign name into sp->name it immediatly print an unexpected result like that. It's doesn't prompt to enter age and birth.
But when I ran like this, it works:
./struct
Name: Kohler
Birth: 1997
Age: 22
1997, 22 Kohler
So, what do you guys think happen? It seems like it doesn't took very well when i'm entering a full-long name like "David Kohler" instead just "Kohler".
What's the solution if i want to enter a full name? Do i need to use malloc? Thank you.
Format specifier %s skips spaces. You can use fgets() or modify your scanf() format specifier, as Jabberwocky pointed in the comments.
fgets:
void record(student *sp)
{
printf("Name: ");
fgets(sp->name,30,stdin);
strtok(sp->name,"\n"); /* Removing newline character,include string.h */
printf("Birth: ");
scanf("%i", &sp -> birth);
printf("Age: ");
scanf("%i", &sp -> age);
}
Note that with fgets you also get a newline character in your buffer.
Scanf:
void record(student *sp)
{
printf("Name: ");
scanf("%29[^\n]", sp -> name); /* Added a characters limit so you dont overflow */
printf("Birth: ");
scanf("%i", &sp -> birth);
printf("Age: ");
scanf("%i", &sp -> age);
}

I cannot perform some input in C

I'm very new at computer program, C is my first programming language. I learn the code from book and now I'm at the input lesson. I try to write this code in C:
#include <stdio.h>
int main()
{
char name[99], web_address[99], address[99];
int age;
printf("Insert your name: ");
fgets(name, sizeof(name), stdin);
printf("Your web address: ");
fgets(web_address, sizeof(web_address), stdin);
printf("Insert your age: ");
scanf("%i", &age);
printf("Insert your address for more information: ");
fgets(address, sizeof(address), stdin);
printf("\n-----------------------------------------------------------\n");
printf("my name is %s", name);
printf("My web address are %s", web_address);
printf("my age is %i\n", age);
printf("and my home address are %s", address);
return 0;
}
There is no error or warning at build log, but when I try to run this code I can't input my address in it.
What I did was create an ageChar[10] array that would store the age. Then convert it to an integer using sscanf and stored it inside of the age variable (Suggested by user3386109).
#include <stdio.h>
int main() {
char name[99], web_address[99], address[99], ageChar[10];
int age;
printf("Insert your name: ");
fgets(name, sizeof(name), stdin);
printf("Your web address: ");
fgets(web_address, sizeof(web_address), stdin);
printf("Insert your age: ");
fgets(ageChar, sizeof(age), stdin);
sscanf(ageChar, "%d", &age);
printf("Insert your address for more information: ");
fgets(address, sizeof(address), stdin);
printf("\n-----------------------------------------------------------\n");
printf("my name is %s", name);
printf("My web address are %s", web_address);
printf("my age is %d\n", age);
printf("and my home address are %s\n", address);
return 0;
}
Try cleaning the buffer, do this:
#include <stdio.h>
int main()
{
char name[99], web_address[99], address[99];
int age;
char ch; //CREATE NEW VARIABLE
printf("Insert your name: ");
fgets(name, sizeof(name), stdin);
printf("Your web address: ");
fgets(web_address, sizeof(web_address), stdin);
printf("Insert your age: ");
scanf("%i", &age);
while ((getchar()) != '\n');
ch = getchar(); //CLEAN THE BUFFER
printf("Insert your address for more information: ");
fgets(address, sizeof(address), stdin);
printf("\n-----------------------------------------------------------\n");
printf("my name is %s", name);
printf("My web address are %s", web_address);
printf("my age is %i\n", age);
printf("and my home address are %s", address);
return 0;
}

Assignment to expression with array type error, char array value unable to be set to a variable in structure

I am taking a very basic C course, and I have run into a problem. My code is supposed to take someone's info, create a profile, and then print the information at the end. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
typedef struct record
{
char lastname[30];
char firstname[30];
int id;
char gender;
int monthOfBirth;
int dayOfBirth;
int yearOfBirth;
} HealthProfile;
void setID(HealthProfile *HPP){
int id;
printf("please enter your ID: ");
scanf("%d", &id);
HPP->id=id;
}
void setGender(HealthProfile *HPP){
char gender;
printf("please enter yomeur M or F for your gender: ");
scanf("%c",&gender);
HPP->gender=gender;
}
void setFirstName(HealthProfile *HPP){
char firstname[30];
printf("please enter your first name: ");
scanf("%s",&firstname);
HPP->firstname=firstname;
}
void setLastName(HealthProfile *HPP){
char lastname[30];
printf("please enter your last name: ");
scanf("%s",&lastname);
HPP->lastname=lastname;
}
void setDoB(HealthProfile *HPP){
int dayOfBirth;
printf("please enter your DoB: ");
scanf("%d", &dayOfBirth);
HPP->dayOfBirth=dayOfBirth;
}
void setMoB(HealthProfile *HPP){
int monthOfBirth;
printf("please enter your MoB: ");
scanf("%d", &monthOfBirth);
HPP->monthOfBirth=monthOfBirth;
}
void setYoB(HealthProfile *HPP){
int yearOfBirth;
printf("please enter your YoB: ");
scanf("%d", &yearOfBirth);
HPP->yearOfBirth=yearOfBirth;
}
int main()
{
HealthProfile *HPP;
HPP=(HealthProfile*) malloc(sizeof(HealthProfile));
setID(HPP);
setGender(HPP);
setLastName(HPP);
setFirstName(HPP);
setDoB(HPP);
setMoB(HPP);
setYoB(HPP);
printf("\n Profile information.....");
printf("ID number: %d\n", HPP->id);
printf("Gender: %c\n", HPP->gender);
printf("Name: %s/n",HPP->firstname);
printf(" %s", HPP->lastname);
printf("Month of birth: %d\n", HPP->monthOfBirth);
printf("Day od birth: %d\n", HPP->dayOfBirth);
printf("Year of birth: %d\n", HPP->yearOfBirth);
}
The part that is giving me the error is these two lines:
**HPP->lastname=lastname;**
and
**HPP->firstname=firstname;**
Whenever I try to run it the equal sign is highlighted red and my code gives me the "assignment to expression with array type" error. Even after looking it up and trying things for almost two hours I couldn't figure it out, so can someone help me, please?
The fields HPP->lastname and HPP->firstname are both arrays, and as the error message states you cannot assign directly to an array.
The way you copy one string to another is to use the strcpy function:
strcpy(HPP->firstname, firstname);
Of course, you can get rid of the copy altogether and read directly into the target array instead of a temporary.
scanf("%s", HPP->firstname);

Resources