This code is about 'struct' in C..
I created a struct with the properties name,roll etc.. By using the for-loop I let the user to create the struct objects. they are named as student,employee, faculty
the problem is the objects are created. But I can use them only inside the for-loop. If I want to get the value of all names in main function, it doesn't work. How can I solve it?How do i print all names in the code in only one loop
what will be the logic
#include<stdio.h>
#include<conio.h>
struct student
{
int std;
char fee[90]; //Collect Data of students
int rollno;
char name[15];
char sub[100];
};
main()
{
int x;
printf("******Enter the total number of Student from HOD*******:\n");
scanf("%d",&x);
struct student a[x];
for(int i=0;i<x;i++)
{
printf("\nEnter Rollno:\t");
scanf("%d",&a[x].rollno);
printf("\nEnter name:\t");
scanf("%s",&a[x].name);
printf("\nIs Fee Submitted:\t");
scanf("%s",&a[x].fee);
printf("\nEnter Subject name:\t");
scanf("%s",a[x].sub);
}
printf("\n****Display All Student names****");
for(int i=0;i<x;i++)
{
printf("\n%s",a[x].name);
}
//Faculty
struct faculty
{
char Fname[100];
char Sname[100];
};
int y;
printf("\n\n********Please HOD enter the total faculty members********\n");
scanf("%d",&y);
struct faculty b[y];
for(int j=0;j<y;j++)
{
printf("\nEnter Faculty Member Name:\t");
scanf("%s",&b[y].Fname);
printf("\nEnter their Subjects:\t");
scanf("%s",&b[y].Sname);
}
printf("\n****Display all Faculty Member Name****");
for(int j=0;j<y;j++)
{
printf("\n%s",b[y].Fname);
}
// Employes
struct employes
{
char ename[100];
char rank[100];
};
int z;
printf("\n\n********please HOD enter the total no of Employes*******:\n");
scanf("%s",&z);
struct employes c[z];
for(int j=0;j<y;j++)
{
printf("\nEnter the Employe name:\t");
scanf("%s",&c[y].ename);
printf("\n and enter their ranks:\t");
scanf("%s",&c[y].rank);
}
printf("\n****Display all Employe names****");
for(int j=0;j<y;j++)
{
printf("%s\n",c[y].ename);
}
}
#include<stdio.h>
#include<conio.h>
struct student
{
int std;
char fee[90];
int rollno;
char name[15];
char sub[100];
};
main()
{
int x;
printf("******Enter the total number of Student from HOD*******:\n");
scanf("%d",&x);
struct student a[x];
for(int i=0;i<x;i++)
{
printf("\nEnter Rollno:\t");
scanf("%d",&a[x].rollno);
printf("\nEnter name:\t");
scanf("%s",&a[x].name);
printf("\nIs Fee Submitted:\t");
scanf("%s",&a[x].fee);
printf("\nEnter Subject name:\t");
scanf("%s",a[x].sub);
}
printf("\n****Display All Student names****");
for(int i=0;i<x;i++)
{
printf("\n%s",a[x].name);
}
struct faculty
{
char Fname[100];
char Sname[100];
};
int y;
printf("\n\n********Please HOD enter the total faculty members********:\n");
scanf("%d",&y);
struct faculty b[y];
for(int j=0;j<y;j++)
{
printf("\nEnter Faculty Member Name:\t");
scanf("%s",&b[y].Fname);
printf("\nEnter their Subjects:\t");
scanf("%s",&b[y].Sname);
}
printf("\n****Display all Faculty Member Name****");
for(int j=0;j<y;j++)
{
printf("\n%s",b[y].Fname);
}
struct employes
{
char ename[100];
char rank[100];
};
int z;
printf("\n\n********Please HOD enter the total no of Employes*******:\n");
scanf("%d",&z); //You used %s instead of %d
struct employes c[z];
for(int j=0;j<z;j++) //You used wrong variable 'y' here
{
printf("\nEnter the Employe name:\t");
scanf("%s",&c[z].ename); //You used wrong variable 'y' here
printf("\nEnter their ranks:\t");
scanf("%s",&c[z].rank); //You used wrong variable 'y' here
}
printf("\n****Display all Employe names****");
for(int j=0;j<z;j++) //You used wrong variable 'y' here
{
printf("\n%s",c[z].ename); //You used wrong variable 'y' here
}
}
Errors:
Wrong format specifier used in taking integer input in number of Employees.
Wrong loop variable used in all loops of Employee.
Now this code is working. I've pointed out the errors in the comments also.
The loop increases i from 0 to some other value x.
(lets say x is 5 for this example)
So i has values 0, 1, 2, 3....
But regardless, you only put data into one element of the array: 5
And you keep overwriting that same element over and over again.
And 5 isn't even a valid index!! The valid indicies are 0, 1, 2, 3, 4.
5 is one-past the end of the array!
Related
#include <stdio.h>
int i, n;
struct add_stock
{
char fullname[30];
int stocks;
char com_name[30];
int shares;
float price;
float total;
int totalmoney;
} add;
int main()
{
printf("Enter full name : ");
scanf(" %[^\n]s", add.fullname);
printf("Enter the no. of stocks you want to purchased : ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter the name of the company : ");
scanf(" %[^\n]s", add[i].com_name);
printf("Enter the no. of shares you want to purchased : ");
scanf("%d", &add[i].shares);
printf("Enter the price of each share : ");
scanf("%f", &add[i].price);
add.total = add.shares * add.price;
printf("Toatl money invested in this stock : ");
scanf("%f", &add[i].total);
}
printf("Total money invested : ");
scanf("%d", add.totalmoney);
return 0;
}
In question it's add_stock , not add stock , i write like this becoz it's not accepting question.
So, i get error for "add" saying subscripted value is neither array nor pointer nor vector.
To declare an array of strucuture, you need to define the variable as below,
struct add_stock
{
char fullname[30];
int stocks;
char com_name[30];
int shares;
float price;
float total;
int totalmoney;
} add[20];
this makes the add as an array of add_stock elements.
then you can access it as, add[0].total, add[1].price and so on.
If you want to declare an array that need to be having dynamic number of elements, you can do so as below.
struct add_stock* arStock;
//allocate memory to hold 10 elements
arStock = (add_stock*)malloc( 10 * sizeof(struct add_stock));
arStock[0]->total=10; //access it with ->, instead of .
the question is to calculate the average of each student separately and then show the id and the average of rank one student. how to compare members of structure
#include<stdio.h>
typedef struct student
{
int id;
float math,physics;
float av;
}std;
int main()
{
int i;
std p[5];
for(i=0;i<5;i++)
{
printf("enter student(%d) id: ",i+1);
scanf("%d",&p[i].id);
printf("grade of math over 100: ");
scanf("%f",&p[i].math);
printf("grade of physics over 100: ");
scanf("%f",&p[i].physics);
printf("average=%.3f\n\n",(p[i].math+p[i].physics)/2);
}
}
To compare on element with another you would take either
p[index]->av {comparaision} p[index]->av
Example p[0]->av > p[1]->av
Don't forget printf("average=%.3f\n\n",(p[i].math+p[i].physics)/2); will print you the result of av but will not save it in p. Save it in p[index].av to be able to do comparaisons.
Then afterward just do a loop where you compare each of them and save their id in an array. The array should be based on a decreasing average with the matching id. Then just print the array and you will be good to go.
#include<stdio.h>
typedef struct studentd
{ int id;
float math,physics;
float av;
} std;
int main()
{
int i,a;
std p[5];
for(i=0; i<5; i++)
{
printf("student(%d) id: ",i+1);
scanf("%d",&p[i].id);
printf("grade of math over 100: ");
scanf("%f",&p[i].math);
printf("grade of physics over 100: ");
scanf("%f",&p[i].physics);
printf("average=%.3f\n\n",p[i].av=(p[i].math+p[i].physics)/2);
}
int max=p[0].av;
for(i=0; i<5; i++)
{
if(p[i].av<=p[i+1].av)
{
max=p[i].av;
}
}
printf("the highest average is for student: %d ",p[i].id);
return 0;
}
/*END*/
/now this is the updated code my problem is how to link the id of student to the max to show the id of student whi got the highest mark/
Well one of two things,
Either add an integer to track the index of the structure containing the max,
Or instead of
max =p[i].avg
Use max = p[i]
Of course let max be a struct not float or int
i've created a struct "Employee"
#define MAX_SIZE 20
typedef struct Employee{
char name[MAX_SIZE];
int s;
int e;
} employee_s;
and i need to create an array of 2 employees and ask the user to initialize them, nothing i try seem to work,
void main()
{
int i, s, e;
char name[10];
Employee* e[3];
*e = (Employee*)malloc(sizeof(Employee)*ARR_SIZE);
for(i=0; i < 3; i++)
{
fflush(stdin);
puts("Please enter Employee's name:");
scanf("%c",&name);
*e[i]->name = name;
puts("Please enter Employee's salary:");
scanf("%d",&s);
*e[i]->s= s;
puts("Please enter Employee's experience:");
scanf("%d",&e);
*e[i]->e=e;
}
}
p.s: i dont have to use dynamic allocation,
what do i do wrong?
thank you!
There are several errors here:
Employee is not a valid type. struct Employee is, and so is employee_s.
e is defined in multiple places
When reading in name, use %s (for a string), not %c (for a char)
Your employee array is defined as an array of pointers. That's probably not what you want. You just need an array. No call to malloc either.
Never fflush(stdin). It's undefined behavior.
In your scanf calls, put a space as the first character in the string. That will allow any newlines to be passed over.
The result:
int main()
{
int i;
employee_s e[3];
for(i=0; i < 3; i++)
{
puts("Please enter Employee's name:");
scanf(" %s",&e[i].name);
puts("Please enter Employee's salary:");
scanf(" %d",&e[i].s);
puts("Please enter Employee's experience:");
scanf(" %d",&e[i].e);
}
for(i=0; i < 3; i++) {
printf("emp %d: name=%s, sal=%d, exp=%d\n", i, e[i].name, e[i].s, e[i].e);
}
}
You've got your declaration backward. This:
typedef struct Employee{
char name[MAX_SIZE];
int s;
int e;
} employee_s;
declares a type named employee_s to be equivalent to struct Employee, and furthermore declares struct Employee. You appear to want this, instead:
typedef struct employee_s {
char name[MAX_SIZE];
int s;
int e;
} Employee;
In this case you can omit employee_s from that if you wish; perhaps that would be less confusing.
Moreover, you are going about your allocation in a very strange way, especially since you don't require dynamic allocation. Why not just do this:
Employee e[3];
? Then you can (and should) skip malloc() altogether. You will then refer to the members of the array elements via the form e[0].name, etc..
You can do this easily without dynamic memory allocation as follows.
#include <stdio.h>
#define MAX_SIZE 20
typedef struct Employee{
char name[MAX_SIZE];
int s;
int e;
} employee_alias;
int main(void) {
int i;
employee_alias e[3];
for(i=0; i < 3; i++)
{
puts("Please enter Employee's name:");
scanf("%s",e[i].name);
puts("Please enter Employee's salary:");
scanf("%d",&e[i].s);
puts("Please enter Employee's experience:");
scanf("%d",&e[i].e);
printf("Entered Data\n");
printf("Name : %s\n",e[i].name);
printf("Salary : %d\n",e[i].s);
printf("Experience : %d\n",e[i].e);
}
return 0;
}
I am having trouble making an array of structures. I want to store data regarding multiple students and take input using the execution of the program. Then output the memory address of persons name with highest marks.
I tried getting to know the answers on similar issues on site, to no help.
The code I have written is
the struct
struct student{
char name;
int marks;
};
and the function is
int num,max = 0,i;
printf("Number of students?\n");
scanf("%d", &num);
struct student* students = (struct student*)malloc (num* (sizeof(struct student)));
for (int x = 0; x < num; x++)
{
printf("Enter Name\n");
scanf("%c" , &students[x].name);
printf("Enter Marks\n");
scanf ("%d" , &students[x].marks);
}
for (i = 1; i < num; i++)
{
if (students[max].marks<students[i].marks)
{
max = i;
}
}
printf("The Memory Add for %c is %p\n", students[max].name , &students[max].name );
It is a part of menu driven program. It goes into a weird loop after line 5 in function code. And i can't create a doubly linked list for the same.
Edit 1
These are the changes i made to struct and func
struct student{
char name[20];
int marks;
};
void memadd(){
int num,max = 0,i, mark;
printf("Number of students?\n");
scanf("%d", &num);
struct student* students = (struct student*)malloc (num* (sizeof(struct student)));
for (int x = 0; x < num; x++)
{
printf("Enter Name\n");
scanf("%s" , &students[x].name);
printf("Enter Marks\n");
scanf ("%d" , &mark);
students[x].marks = mark;
}
for (i = 1; i < num; i++)
{
if (students[max].marks<students[i].marks)
{
max = i;
}
}
printf("The Memory Add for %s is %p\n", students[max].name , &students[max].name );
It is giving an array at input of the name. "format specifies type 'char ' but the argument has
type 'char ()[20]'"
How do I make the format to be char * [20]?
Edit 2
The final working code after correcting the char name to being a character array
And issue with scanning of names.
struct student{
char name[20];
int marks;
};
int num,max = 0,i, mark;
printf("Number of students?\n");
scanf("%d", &num);
struct student* students = (struct student*)malloc (num* (sizeof(struct student)));
for (int x = 0; x < num; x++)
{
printf("Enter Name\n");
scanf("%19s" , students[x].name);
printf("Enter Marks\n");
scanf ("%d" , &mark);
students[x].marks = mark;
}
for (i = 1; i < num; i++)
{
if (students[max].marks<students[i].marks)
{
max = i;
}
}
printf("The Memory Add for %s is %p\n", students[max].name , &students[max].name );
However, what if I wish to print the address in Hex numbers? or the one that am getting is hex already.?
Because I got an output of The Memory Add for XYZ is 0x7fb340c03928
The problem is with the student name being a char and
scanf("%c" , &students[x].name);
What happens is, you read in the number of students with
scanf("%d", &num);
Now you have read num, but there is still a newline in the input buffer.
Then you try to read a character with %c, but this reads the remaining newline.
When you change the student's name to a character array, e.g.
struct student{
char name[20];
int marks;
};
and read the name with
scanf("%19s" , students[x].name);
it will skip the remaining whitespace/newline and read the name as it should.
My program reads the following data: Date, Distance and Time.
So for example an input would look like:
Date: 10.10.2013
Distance (m): 500
Time (HH:MM:SS): 01:20:05
I want to store all this information in an array. The problem is that I want the user to input date, distance and time several times and the program has to save all the data in an array.
I can't store it like this, because then how would I know which index is the date index? And how should I store the time? I can't store it with :.
arr[0] = 10.10.2013
arr[1] = 500
arr[2] = 012005
arr[3] = 22.10.2013
arr[4] = 200
arr[5] = 000510
You can make a struct:
struct Data{
Date date;
Distance distance;
Time time;
}
Then declare an array of Data and use it like that:
Data arr[5];
arr[0].date = //some date;
arr[0].distane =//some distance
arr[0].time=//some time
Because the type of data you will need to store, it needs type char, so it can be stored in strings. (i.e. "10.10.2013")
First, Define a struct PARAM:
typedef struct {
char date[20];
char distance[20];
char time[20];
} PARAM;
use PARAM to create an array of your struct:
PARAM param[20];
Now, you can use it like this for example:
int main(void)
{
strcpy(param[0].date, "10.10.2013");
strcpy(param[0].time, "05:02:10");
strcpy(param[0].distance, "500");
//and so on for all the struct array elements, 0 through 20
return 0;
}
Or, better yet, using printf() and scanf() statements as necessary, you can prompt the user for input in a loop, and store in your struct array:
int main(void)
{
int i;
for(i=0;i<20;i++)
{
printf("enter date %d:", i+1);
scanf("%s", param[i].date);
printf("enter distance %d:", i+1);
scanf("%s", param[i].distance);
printf("enter time %d:", i+1);
scanf("%s", param[i].time);
}
return 0;
}
EDIT Regarding question in comment Therefore I guess its best to store them in date.day, date.month and date.year. Right? That approach would work, and I include it below, but it is a little more tedious to enter data that way. I included a second example below that might improve both, entering data, and storing data.
So, Per your comment, two ways jump to mind:
ONE, create struct members that contain the discrete members of time and date as integers: i.e.
typedef struct {
int day;
int month;
int year;
int hour;
int minute;
int second;
}TIMEDATE;
Use this struct as a member of the PARAM struct;
typedef struct {
int distance;
TIMEDATE timedate;
}PARAM;
PARAM param[20];
Now, just modify and expand the example of the last main() function to include scanning in values for the new struct members. This will be more tedious for the person using your program, but will allow you to keep all the input values as numbers as you have indicated.
//Note the changes in scanf format specifiers for int, "%d":
// in all the statements
int main(void)
{
int i;
for(i=0;i<20;i++)
{
printf("enter date %d:", i+1);
scanf("%d", ¶m[i].timedate.day);
printf("enter time %d:", i+1);
scanf("%d", ¶m[i].timedate.month);
printf("enter time %d:", i+1);
scanf("%d", ¶m[i].timedate.year);
printf("enter time %d:", i+1);
scanf("%d", ¶m[i].timedate.hour);
printf("enter time %d:", i+1);
scanf("%d", ¶m[i].timedate.minute);
printf("enter time %d:", i+1);
scanf("%d", ¶m[i].timedate.second);
printf("enter time %d:", i+1);
scanf("%d", ¶m[i].distance);
}
return 0;
}
Two, modify the first approach to include using strings AND integers, all in the same struct. This will make it easier for the user user to enter time and date information, and possible for you to manipulate the data easier. And a bonus, it will demonstrate how to parse the user input string data into integer data.
typedef struct {
char date[20];//keep as char
char time[20];//keep as char
int distance; //changed to int
TIMEDATE timedate;//container for in data
} PARAM;
//use PARAM to create an array of your struct:
PARAM param[20], *pParam; //create a pointer to pass
int GetIntData(PARAM *p, int index);//prototype for new function
//Note the changes in scanf format specifiers for int, "%d":
// in all the statements
int main(void)
{
int i, loops;
pParam = ¶m[0]; //initialize pointer to struct
printf("How many sets of data would you like to enter? :");
scanf("%d", &loops);
for(i=0;i<loops;i++)
{
printf("enter date (eg:MM.DD.YYYY): %d:", i+1);
scanf("%s", pParam[i].date);
printf("enter time (eg HH:MM:SS): %d:", i+1);
scanf("%s", pParam[i].time);
printf("enter distance %d:", i+1);
scanf("%d", &pParam[i].distance);
GetIntData(pParam, i);
}
return 0;
}
//reads string members into integer members
int GetIntData(PARAM *p, int index)
{
char *buf=0;
if(strstr(p[index].date, ".")==NULL) return -1;
p[index].timedate.month = atoi(strtok(p[index].date, "."));
p[index].timedate.day = atoi(strtok(NULL, "."));
p[index].timedate.year = atoi(strtok(NULL, "."));
if(strstr(p[index].time, ":")==NULL) return -1;
buf=0;
p[index].timedate.hour = atoi(strtok(p[index].time, ":"));
p[index].timedate.minute = atoi(strtok(NULL, ":"));
p[index].timedate.second = atoi(strtok(NULL, ":"));
return 0;
}