struct subscriber
{
char phonenumber[20];
char name[50];
float amount;
}s;
void modifyrecords()
FILE *f;
char phonenumber[20];
long int size=sizeof(s);
if((f=fopen("c:/file.ojs","rb+"))==NULL)
exit(0);
system("cls");
printf("Enter phone number of the subscriber to modify:");
scanf("%[^\n]",phonenumber);
fflush(stdin);
while(fread(&s,sizeof(s),1,f)==1)
{
if(strcmp(s.phonenumber,phonenumber)==0)
{
system("cls");
printf("\n Enter phone number:");
scanf("%s",&s.phonenumber);
printf("\n Enter name: ");
fflush(stdin);
scanf("%[^\n]",&s.name);
printf("\n Enter amount: ");
scanf("%f",&s.amount);
fseek(f,-size,SEEK_CUR);
fwrite(&s,sizeof(s),1,f);
break;
}
}
fclose(f);
}
C:\Users***\Desktop\a.c|394|error: parameter 'size' is initialized|
I am getting parameter 'size' is initialized error in this code. After googling I found out I may have to pass certain arguments to suppress these kind of errors. Is this the only solution? Actually I am using Code::Blocks so if is the only way how do I pass command line arguments in it?
You have a simple syntax error here:
void modifyrecords()
should be:
void modifyrecords() {
Also, this:
scanf("%s",&s.phonenumber);
should be
scanf("%s",s.phonenumber);
And that applies to s.name as well.
Related
[Warning] passing argument 1 of 'gets' from incompatible pointer type
I want to create a program that allows me to input the student full name that's why I used the function "gets" however when I run my program it will only read the input on name, age and telephone it won't read the address why is this happening ? can someone help me? I still new to this programming thing
#include<stdio.h>
#include<string.h>
#include<conio.h>
struct data
{
char name[10][40];
int age[10];
char address[10][40];
char telephone[10][40];
}d;
main()
{
char ans='y';
int i;
i=1;
while(ans=='y'||ans=='Y')
{
printf("Enter name: ");
gets(d.name[i]); //this is the warning goes
printf("Enter age: ");
scanf("%d",&d.age);
printf("Enter address: ");
gets(d.address[i]); //this is where the warning goes
printf("telephone: ");
scanf("%s",&d.telephone[i]);//this is where the warning goes
i++;
printf("Enter another [Y/N]? ");
scanf("%c",&ans);
}
getch();
}
Could I please have an explanation of what I am doing wrong.The program is for inputting and displaying student details. It must use a structure and have 2 functions; one to capture(which is to be passed by reference) and another to display (which is to be passed by value.
Here is my code:
#include <stdio.h>
struct Students{
int ID;
char name[50];
int age;
char address[100];
char course[30];
} aStudent[5];
void capture(char *name , int *age , char *address, char *course){
int i;
for(i=0; i<5; i++){
aStudent[i].ID = i+1;
printf("\nFor Student number %d:\n",aStudent[i].ID);
printf("Enter Student Name: ");
scanf ("%s", &aStudent[i].name);
printf("Enter Student Age: ");
scanf ("%d", &aStudent[i].age);
printf("Enter Student Address: ");
scanf ("%s", &aStudent[i].address);
printf("Enter Course: ");
scanf ("%s", &aStudent[i].course);
}
}
void display(char name, int age , char address, char course){
int i;
for(i=0; i<5; i++){
printf("\nStudent %d:\n",aStudent[i].ID);
printf("Name: %s\t\tAge: %d\t\tAddress: %s\t\tCourse:
%s",aStudent[i].name, aStudent[i].age, aStudent[i].address,
aStudent[i].course);
printf("\n");
}
}
void main()
{
int option, age;
char name, address, course;
printf("\t...Welcome to the Student Data System...\n\n");
printf("\nPlease Select An Option: \n1. Input Student Data\n2. View
Student Data\n3. Exit Syatem\n\n");
scanf("%d",&option);
switch(option){
case 1:
printf("Enter Student Details:\n");
capture(name, age , address, course);
break;
case 2:
printf("\nDisplaying Information:\n");
display(name, age , address, course);
break;
case 3:
close();
break;
default:
printf("\nSorry, your option is not valid.");
}
}
I have tested a number of times and it's working, but I'm getting these error messages:
Errors are shown for every argumety I've used
Also, is there a way or a line(s) of code i can use to return to the start of the switch when I am done with one of the cases - A "Return to Main Menu"?
First of all, variables you have tried to pass (either by value or by reference) when you are calling the two functions capture() and display(), haven't used anywhere because you are dealing directly with the members of the structure Students when you are capturing or displaying the results.
And the reasons you are getting syntax errors are because the capture() is expecting the addresses of the variables (&name,&age,&address,&course) and you are passing variables (name,age,address,course) themselves. And also you are using,
scanf ("%s", &aStudent[i].name);
instead of
scanf ("%s", aStudent[i].name);
In my opinion instead of making the Structure array global, declaring it inside main function and passing the whole structure array to the capture() as a reference and passing it by value to the display() is better to your objective as you need to use both call by value and reference in your code.
I have edited your code a bit and added the return to main menu option. It works for me and I apologize if my answer is long and hard to understand because this is my first answer in stackoverflow. Thanks!
#include <stdio.h>
struct Students
{
int ID;
char name[50];
int age;
char address[100];
char course[30];
};
void capture(struct Students *aStudent)
{
int i;
for(i=0; i<2; i++)
{
aStudent[i].ID = i+1;
printf("\nFor Student number %d:\n",aStudent[i].ID);
printf("Enter Student Name: ");
scanf ("%s", aStudent[i].name);
printf("Enter Student Age: ");
scanf ("%d", &aStudent[i].age);
printf("Enter Student Address: ");
scanf ("%s", aStudent[i].address);
printf("Enter Course: ");
scanf ("%s", aStudent[i].course);
}
}
void display(struct Students aStudent[])
{
int i;
for(i=0; i<2; i++)
{
printf("\nStudent %d:\n",aStudent[i].ID);
printf("Name: %s\t\tAge: %d\t\tAddress: %s\t\tCourse: %s",aStudent[i].name, aStudent[i].age, aStudent[i].address,aStudent[i].course);
printf("\n");
}
}
void main()
{
struct Students aStudent[2];
int option;
char choice = 'Y';
printf("\t...Welcome to the Student Data System...\n\n");
while(choice == 'Y' || choice == 'y')
{
printf("\nPlease Select An Option: \n1. Input Student Data\n2. View Student Data\n3. Exit Syatem\n\n");
scanf("%d",&option);
switch(option)
{
case 1:
printf("Enter Student Details:\n");
capture(aStudent);
printf("Return to main menu? (Y/N) :");
scanf(" %c",&choice);
break;
case 2:
printf("\nDisplaying Information:\n");
display(aStudent);
printf("Return to main menu? (Y/N) :");
scanf(" %c",&choice);
break;
case 3:
close();
break;
default:
printf("\nSorry, your option is not valid.");
}
}
}
You have not initialized your string instead you have initialized a character! char name, address, course; If you have used 'char *name, *address, *course;' or char name[100], address[100], course[100]; you might have got the answer ! If you use the above case you should scan using scanf("%s",name); ! Hope I answered your question !
I keep getting this error when I try to run a piece of code say that the variable is being used which isn't initialized, despite I've declared it.
{
FILE *fptr;
int length;
int number_search;
struct student
{
char surname[15];
char initials[6];
char title[4];
int student_number;
char module_name[25];
char module_code[7];
int assesment_mark;
int exam_mark;
int tuition_fee;
};
struct student record_student;
struct student *student_ptr;
student_ptr=&record_student;
length=sizeof(struct student);
printf("2 has been called\n");
printf("Enter module code: \n");
scanf("%s", module_code);
clear_buffer(module_code);
printf("%s\n",module_code); /*Test the string entered is 6 charaters, AB1234 format*/
if (! modcheck(module_code)) /*Change this fucntion to a differnt one to check correct format*/
{
printf("Invalid input\n");
}
else
{
printf("input ok\n");
printf("Enter Student Number: \n");
scanf("%d",number_search);
}
it's saying that the int number_search isn't being initialized despite it being in the code above.
Change:
scanf("%d",number_search);
to
scanf("%d", &number_search);
//^See here the address operator
Indeed, number_search is not initialized.
And your call to scanf(3) is wrong. It should be
scanf("%d", &number_search);
and even with that correction, number_search is still uninitialized: scanf can fail (e.g. if your user types hello or Ctrl D on Linux) and you should test the result of scanf (number of successfully read items), at least:
if (scanf("%d", &number_search) != 1) {
perror("number_search input failure"); exit(EXIT_FAILURE);
}
I believe that you should always explicitly initialize local variables (if that initialization happens to become useless, the compiler would optimize it out), like
int number_search = 0;
PS. You should compile with all warnings and debug info, e.g. gcc -Wall -Wextra -g; once you are sure of not having bugs, add -O2 to get optimizations.
printf("Enter module code: \n");
scanf("%s", module_code);
This should be
printf("Enter module code: \n");
scanf("%s", student_ptr->module_code);
and
scanf("%d", &number_search);
Scan to the address of the variable which is given by &number_search
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);
}
}
#include<stdio.h>
struct student{
char name[80];
char subject
char country;
};
int main(){
struct student s[10];
int i;
printf("Enter the information of the students:\n");
for(i=0;i<4;++i)
{
printf("\nEnter name of the student: ");
scanf("%s",&s[i].name);
printf("\nEnter the subject of the student: ");
scanf("%s",&s[i].subject);
printf("\nEnter name of the student country: ");
scanf("%s",&s[i].country);
}
printf("\n showing the input of student information: \n");
for(i=0;i<10;++i)
{
printf("\nName: \n");
puts(s[i].name);
printf("\nMajor: \n",s[i].subject);
printf("\nCountry: \n",s[i].country);
}
return 0;
}
***while I tried to display the result it is not showing the subject and country.Can u tell me what problem is in my coding?
Is it not showing the subject and the country or is it displaying the first letter only?
I'm not familiar with C but i would suggest you to change
char variableName
to
char variableName[size]
As you have in name but you do not have in country and subject. I'm not sure if it is your problem but it might be, I believe just char variableName would store only one character of the users input.
You need to provide a conversion pattern, for char it is %c
printf("\nMajor: %c\n",s[i].subject);
printf("\nCountry: %c\n",s[i].country);
Also
scanf("%s",&s[i].name);
is not correct, it should be
scanf("%s", s[i].name); // s[i].name is already an array
for reading a char you need to pass the correct conversion pattern too
scanf("%c", &s[i].subject);
scanf("%c", &s[i].country);
Your struct should be something like:
struct student{
char name[80];
char subject[80];
char country[80];
};