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;
}
Related
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.
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);
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
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);
}
So I was doing an assignment for a class in which I have to perform basic
array functions for an array of structures and in taking input my
program closed on its own. The program terminated after taking input of name
void input(struct record *d){
printf("\nenter name: ");
fflush(stdin);
gets(d->name);
printf("\nenter adress: ");
fflush(stdin);
gets(d->adress);
printf("\nEnter mobile no :");
scanf("%s",d->mobile);
printf("\nenter marks:");
scanf("%if",d->marks);
printf("\nenter cgpa: ");
scanf("%if",d->cgp);
}
The record structure must be initialized, and you should use
scanf("%if", &(d->marks));
printf("\nenter cgpa: ");
scanf("%if", &(d->cgp));
#include <stdio.h>
typedef struct record
{
char name[255];
char adress[2048];
char mobile[80];
int marks;
int cgp;
} record;
void input(struct record *d)
{
printf("\nenter name: ");
fflush(stdin);
gets(d->name);
printf("\nenter adress: ");
fflush(stdin);
gets(d->adress);
printf("\nEnter mobile no :");
scanf("%s", d->mobile);
printf("\nenter marks:");
scanf("%if", &(d->marks));
printf("\nenter cgpa: ");
scanf("%if", &(d->cgp));
}
void print_record(record *r)
{
printf("Name: %s\r\n", r->name);
printf("Adress: %s\r\n", r->adress);
printf("Mobile %s\r\n", r->mobile);
printf("Marks %i\r\nf", r->marks);
printf("Cgp %if\r\n", r->cgp);
}
int main()
{
record r1;
input(&r1);
print_record(&r1);
}