abrupt end of program while fflush in c - fflush

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

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.

How can I search for a string value and print the result in linked-list?

I was doing a mini-project system for my course and had a problem searching the username from what I inserted into the system. Every time I search for the username it returns no username is found. Is the structure that I have created correctly to store the user input because I'm the first time creating this complex structure. Where is the problem and how can I fix it?
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
#define TRUE 1
#define FALSE 0
struct fitnessTracking {
char activity[50], unit[50];
int calories;
float total, weight, height, bmi;
};
struct recordDate {
int dd, mm, yyyy;
int duration;
};
struct user {
char username[50];
struct fitnessTracking tracker;
struct recordDate record;
};
struct Number {
struct Number *ptrnext;
struct user member;
};
struct Number *newptr, *currentptr;
int main(void) {
int selection;
int choice=TRUE;
char searchValue[20];
struct Number* head = NULL;
while(choice==TRUE) {
printf("\n\n1 - Enter new user");
printf("\n2 - Search by username");
printf("\n3 - Display user list");
printf("\n4 - Delete user from list");
printf("\n5 - Exit\n");
printf("\nEnter choice: ");
scanf("%d",&selection);
switch(selection) {
case 1: addUser(&head);
break;
case 2: printf("\nSearch username: ");
scanf(" %[^\n]%*c", &searchValue);
searchUser(head, searchValue);
break;
case 3: listUser(head);
break;
case 4: deleteUser();
break;
case 5: choice=FALSE;
break;
default: printf("\nEnter available choice from above");
}
}
return 0;
}
void addUser(struct Number** head_ref){
newptr=(struct Number *)malloc(sizeof(struct Number));
printf("\nEnter name: ");
scanf(" %[^\n]%*c", &newptr->member.username);
printf("\nEnter weight: ");
scanf("%f", &newptr->member.tracker.weight);
printf("\nEnter height: ");
scanf("%f", &newptr->member.tracker.height);
printf("\nEnter activity: ");
scanf(" %[^\n]%*c", &newptr->member.tracker.activity);
printf("\nEnter activity duration (in minutes): ");
scanf("%d", &newptr->member.record.duration);
printf("\nEnter total: ");
scanf("%f", &newptr->member.tracker.total);
printf("\nEnter unit: ");
scanf(" %[^\n]%*c", &newptr->member.tracker.unit);
printf("\nEnter calories: ");
scanf("%d", &newptr->member.tracker.calories);
printf("\nEnter activity date (dd/mm/yyyy): ");
scanf("%d/%d/%d", &newptr->member.record.dd, &newptr->member.record.mm, &newptr->member.record.yyyy);
float bmi = 0;
float weight = newptr->member.tracker.weight;
float height = newptr->member.tracker.height;
bmi = (weight) / (height*height);
newptr->member.tracker.bmi = bmi;
//link the old list to the new node
newptr->ptrnext = (*head_ref);
//move head to point to the new node
(*head_ref) = newptr;
}
void searchUser(struct Number* head, char searchValue){
char a[20]="Name",b[20]="Weight(kg)", c[20]="Height(m)", d[20]="BMI", e[20]="Activity";
char f[20]="Duration(minutes)", g[20]="Total", h[20]="Unit", i[20]="Calories(kal)", j[20]="Date";
struct Number* temp=head;
printf("%-15s %-10s %-10s %-10s %-15s %-20s %-15s %-15s %-15s %-15s\n", a,b,c,d,e,f,g,h,i,j);
while(temp != NULL){
if(temp->member.username == searchValue)
printf("%-15s %-10.2f %-10.2f %-10.2f %-15s %-20d %-15.2f %-15s %-15d %d/%d/%-15d\n\n", temp->member.username, temp->member.tracker.weight, temp->member.tracker.height, temp->member.tracker.bmi, temp->member.tracker.activity, temp->member.record.duration, temp->member.tracker.total, temp->member.tracker.unit, temp->member.tracker.calories, temp->member.record.dd, temp->member.record.mm, temp->member.record.yyyy);
temp=temp->ptrnext;
}
printf("Sorry, no match found\n");
}
Here you are comparing pointers:
if(temp->member.username == searchValue)
What you do want to do is comparing what they are pointing to, ideally using a ready made function for the purpose.
See https://en.cppreference.com/w/cpp/string/byte/strcmp

Problem with funcion scanf jump to the next scanf in 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

File handling problem, generate automatically other type data in c

Here this code whenever i want to input for student struct data then also a random courseData created with haphazard number, also when give input for courseData some random data(most of the 2 sets) sets created for student struct... erase the struct student grade and cut connection between two structure but the result remain same.
typedef struct student
{
int ID;
float Homework;
float Classtest;
float midterm1;
float midterm2;
float Final;
}gradelist;
struct courseData{
char courseID[10];
int semester;
int year;
int enrolllment;
struct student grade;
};
FILE *file;
int main(){
int choice;
do{
printf("\nChoice ---->>>>> ");
scanf("%d",&choice);
switch(choice){
case 1:
Read();
break;
case 2:
Insert();
break;
case 6:
display_all();
student_display();
break;
case 8:
printf("Thank you");
exit(1);
break;
}
}
while(choice!=8);
}
//insert function
void Insert(){
struct student info;
file = fopen("file.txt","a");
printf("\nEnter details \n\n");
printf("\nEnter the ID : ");
scanf("%d",&info.ID);
printf("\nEnter the Marks of the homework : ");
scanf("%f",&info.Homework);
printf("\nEnter the Marks of the classtest : ");
scanf("%f",&info.Classtest);
printf("\nEnter the Marks of the midterm1 : ");
scanf("%f",&info.midterm1);
printf("\nEnter the Marks of the midterm2 : ");
scanf("%f",&info.midterm2);
printf("\nEnter the Marks of the final : ");
scanf("%f",&info.Final);
fwrite(&info, sizeof(info), 1, file);
fclose(file);
}
//Read the data
void Read(){
struct courseData cinfo;
file = fopen("file.txt","w");
printf("\nEnter the courseID : ");
scanf("%s",cinfo.courseID);
printf("\nEnter the semeter : ");
scanf("%d",&cinfo.semester);
printf("\nEnter the year : ");
scanf("%d",&cinfo.year);
printf("\nEnter the enrollment : ");
scanf("%d",&cinfo.enrolllment);
printf("\nEnter the grades : ");
scanf("%s",&cinfo.grade);
fwrite(&cinfo, sizeof(cinfo), 1, file);
fclose(file);
printf("\nThe year is coint : %d",cinfo.year);
}
Also where i asking for txt file, but the files data not as like text.

Filing Structure / C

I am getting error after fill information.I dont know where did i make mistake.I want to get information via structure and then save in biodata.txt file.This is just for starting for my program then i will add "delete edit search" options.
#include <stdio.h>
#include <conio.h>
struct biodata{
int recno,age;
char name[20],sex;
float salary;
}obj;
int main(){
int addData();
int showRecord();
char choice;
while(1){
printf("\n\n*****CHOOSE YOUR CHOICE*****\n");
printf("1) ADD DATA\n");
printf("2) SHOW RECORD\n");
printf("Enter your choice : ");
choice = getche();
switch(choice){
case '1' :
addData();
break;
}
}
}
int addData(){
FILE *fp;
fp = fopen("biodata.txt","w+");
printf("\n*****ADDING DATA*****\n");
printf("\nEnter Record No : ");
scanf("%d",&obj.recno);
printf("Enter Name : ");
scanf("%s",obj.name);
printf("Enter age : ");
scanf("%d",&obj.age);
printf("Enter Sex : ");
scanf("%s",obj.sex);
printf("Enter Salary : ");
scanf("%f",&obj.salary);
fwrite(&obj,sizeof(obj),1,fp);
fclose(fp);
}
You are doing:
printf("Enter Sex : ");
scanf("%s",obj.sex);
But obj.sex is of char type:
struct biodata{
int recno,age;
char name[20],sex;
float salary;
}obj;
Change the scanf to:
scanf("%c",&obj.sex);

Resources