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);
Related
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.
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);
}
Here is my code. I want it to ask me questions in a sequence. But whenever I enter my choice and put my name it didn't allow me to ask further. How to deal with that?
#include <stdio.h>
#include <stdlib.h>
int new_acc();
int main(){
int one=1, two=2, three=3, four=4, five=5, six=6, seven=7, new_account;
printf("-----WELCOME TO THE MAIN MENU-----\n\n");
printf("%d. Create new account\n",one);
printf("Enter you choice: ");
if (scanf("%d",&one)){
new_account = new_acc(); // calling a function
}
return 0;
}
int new_acc(){
int id; char name;
printf("Enter your name: ");
scanf("%c\n",&name);
printf("Enter your ID card number: ");
scanf("%d\n",&id);
return 0;
}
If you typed Enter after typing nubmer for the MAIN MENU, the newline character remains in the buffer.
Then, is is read as the name via %c.
After that, if you typed, for example, alphabet as name, it will prevent it from reading the number id.
To avoid this, you can put a space before %c to have it skip the newline character.
Also you won't be have to skip after reading name and id, so you should remove \n in scanf() after %c and %d for them.
int new_acc(){
int id; char name;
printf("Enter your name: ");
scanf(" %c",&name); /* add space and remove \n */
printf("Enter your ID card number: ");
scanf("%d",&id); /* remove \n */
return 0;
}
By the way, the above code will allow only one alphabet as name.
To support multi-character name (without space character), you should use an array of char and %s with length specified.
int new_acc(){
int id; char name[1024];
printf("Enter your name: ");
scanf(" %1023s",name); /* don't use & here, and size limit is buffer size - 1 (-1 for terminating null character) */
printf("Enter your ID card number: ");
scanf("%d",&id);
return 0;
}
If you want to support name with space characters, you can use %[\n] (read until newline character) instead of %s.
int new_acc(){
int id; char name[1024];
printf("Enter your name: ");
scanf(" %1023[^\n]",name);
printf("Enter your ID card number: ");
scanf("%d",&id);
return 0;
}
Seems like you want to use an object oriented programming paradigm in this. To so do, you should define an "object" with struct and save the new account with that:
#define MAX 50
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Account {
int id;
char name[MAX];
};
struct Account new_acc();
int main(){
int choice;
struct Account new_account;
printf("-----WELCOME TO THE MAIN MENU-----\n\n");
printf("1. Create new account\n");
printf("Enter you choice: ");
scanf("%d",&choice);
switch(choice) {
case 1:
new_account = new_acc();
break;
default:
printf("Not a valid option\n");
return 1;
}
return 0;
}
struct Account new_acc(){
char name[MAX];
int id;
struct Account new;
printf("Enter your name: ");
scanf("%c\n",name);
printf("Enter your ID card number: ");
scanf("%d\n",&id);
strcpy(new.name, name);
new.id = id;
return new;
}
Pay attention because this code is very vulnerable to buffer overflows. Plus, I edited your check for the option in main because scanf returns 1 if reads whatever value successfully.
Use This Code i have modfified a little bit
int new_acc(){
int id; char name[10];
printf("Enter your name: ");
scanf("%s",name);
printf("Enter your ID card number: ");
scanf("%d",&id);
return 0;
}
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);
I was practicing array of structure. I made the following program and there were no compiling errors.But when i try to run it(I guess these errors are called runtime errors?),it stops working just after accepting the roll number. I wonder what wrong i did.
I use Dev c++ and gcc compiler.
Here's the code:
#include<stdio.h>
struct student{
char Fname[];
char Lname[];
int reg_no;
int Class;
char sec;
};
void enterinfo(student *,int);
void Display(student *,int);
int main()
{
int i;
printf("\t\t\t Enter student's information\n\n\n\n");
printf("How many students are there in you're school: ");
scanf("%d",&i);
student ob[i],*ptr;
ptr=ob;
enterinfo(ptr,i);
Display(ptr,i);
}
void enterinfo(student *e,int y)
{
char CONT='y';
for (int j=0;j<y && (CONT=='y' || CONT=='Y');j++)
{
printf("Enter Students First Name: ");
scanf("%s",e->Fname);
printf("Enter Students Last Name: ");
scanf("%s",e->Lname);
printf("Enter Roll number: ");
scanf("%d",e->reg_no);
printf("Enter class: ");
scanf("%d",e->Class);
printf("Enter Section: ");
scanf("%d",e->sec);
printf("\n\n\n\n Do you want to enter more? : ");
scanf("%c",&CONT);
}
}
void Display(student *e,int y)
{
char CONT='y';
for (int j=0;j<y;j++)
{
printf("Students name : %s %s",e->Fname,e->Lname);
printf("Enter Roll number: %d",e->reg_no);
printf("class: %d",e->Class);
printf("Enter Section: %d",e->sec);
}
}
I've made the following changes to your code and it started working for me:
char Fname[]; --> char Fname[100];
char Lname[]; --> char Lname[100];
char sec; --> int sec; This is needed for scanf.
scanf("%d",e->reg_no); --> scanf("%d",&e->reg_no);
scanf("%d",e->Class); --> scanf("%d",&e->Class);
scanf("%d",e->sec); --> scanf("%d",&e->sec);
adding \n to the end of printf strings in Display
Please note that scanf("%s", ...) is insecure and it can cause a crash the input string is longer than the array size you're reading it to, i.e. if the user types a name of at least 100 bytes.
Please note that you should always check the return value of scanf, and abort early on error (i.e. if it doesn't return 1 in your case).
Please note that in C++ the istream methods (http://en.cppreference.com/w/cpp/header/istream) provide a safer way to read the input.
Here:
scanf("%d",e->reg_no);
you should insert a '&' symbol before e->reg_no. But I can see many other problems once you solve this...
You can use below code, it will run on both linux & windows. Your program halts because of:
scanf("%d",&e->reg_no);
printf("Enter class: ");
scanf("%d",&e->Class);
printf("Enter Section: ");
scanf("%d",&e->sec);
you did not use & which is must for int, char, float data types as it is used as reference of the variable.
#include
struct student{
char Fname[30];
char Lname[30];
int reg_no;
int Class;
char sec[5];
};
void enterinfo(student *,int);
void Display(student *,int);
int main()
{
int i;
printf("\t\t\t Enter student's information\n\n\n\n");
printf("How many students are there in you're school: ");
scanf("%d",&i);
student * ob = new student[i];
enterinfo(ob,i);
Display(ob,i);
}
void enterinfo(student *e,int y)
{
char CONT='y';
for (int j=0;jFname);
printf("Enter Students Last Name: ");
scanf("%s",e->Lname);
printf("Enter Roll number: ");
scanf("%d",&e->reg_no);
printf("Enter class: ");
scanf("%d",&e->Class);
printf("Enter Section: ");
scanf("%s",e->sec);
getchar();//to eat newline/ enter char of previous statement
printf("\n\n\n\n Do you want to enter more? : ");
scanf("%c",&CONT);
}
}
void Display(student *e,int y)
{
for (int j=0;jFname,e->Lname);
printf("\nEnter Roll number: %d",e->reg_no);
printf("\nclass: %d",e->Class);
printf("\nEnter Section: %s\n",e->sec);
}
}