C dynamic struct error - c

#include <stdio.h>
#include <stdlib.h>
int main()
{
int size,choice;
printf("student size:\n");
scanf("%d", &size);
typedef struct
{
int age;
double gpa;
char name[];
}STUDENT;
STUDENT *array = (STUDENT *) malloc(sizeof(STUDENT) * size);
printf("\n(1) Add a student\n(2) Delete a student\n(3) Save all students\n(4)Quit\n");
scanf("%d",&choice);
while (choice != 4){
switch (choice) {
STUDENT temp;
STUDENT *tempptr = &temp;
int cellNum;
case 1:
printf("Enter age gpa and name:\n");
scanf("%d %f %s",&tempptr->age,&tempptr->gpa,&tempptr->name);
printf("Enter cell number:\n");
scanf("%d", &cellNum);
if (cellNum > (size-1)){
printf("Invalid cell number\n");
break;
}else{
*(array + cellNum) = temp;
}
case 3:
printf("stupid c syntax rules");
FILE *p = fopen("students.txt","w");
int i = 0;
for (i=0; i<size; i++){
fprintf(p, "%d, %f, %s\n",*(array+i).age,*(array+i).gpa,*(array+i).name);
}
}
printf("\n(1) Add a student\n(2) Delete a student\n(3) Save all students\n(4)Quit\n");
scanf("%d",&choice);
}
return 0;
}
getting errors:
main.c:40:56: error: request for member 'age' in something not a structure or union
fprintf(p, "%d, %f, %s\n",*(array+i).age,*(array+i).gpa,*(array+i).name);
^
main.c:40:71: error: request for member 'gpa' in something not a structure or union
fprintf(p, "%d, %f, %s\n",*(array+i).age,*(array+i).gpa,*(array+i).name);
^
main.c:40:86: error: request for member 'name' in something not a structure or union
fprintf(p, "%d, %f, %s\n",*(array+i).age,*(array+i).gpa,*(array+i).name);
why is this happening?
Thanks

The . operator is a higher precedence than unary * (refer here). So first (before dereferencing) the compiler is trying to find an age field in (array+i), which is not a structure, but a pointer.
You want to replace the *(array+i).age by either (array+i)->age or (*(array+i)).age.

Related

Program doesnt show the things I want it to show

I am currently trying to write a program in which when the user selects "1" It will ask him the number of students.It will then create a dynamic array and store all of their information in there.However I am running into the problem where when I try to print the information of the students my program just spews out numbers and letters.Here is the program:
#include <stdio.h>
#include <stdlib.h>
typedef struct student{
int id;
char name[50];
char surname[50];
int semester;
float grade;
}student;
int main()
{
int x,std,i;
x=0;
student** info;
while (x!=8)
{
printf("1. Initialize student list\n2. Add a student record\n3. Delete a student record\n4. Display a student record by student surname\n5. Display students passed\n6. Display students failed\n7. Display all student records\n8. Exit");
printf("\nYour choice: ");
scanf("%d",&x);
if (x==1)
{
printf("How many new students? ");
scanf("%d",&std);
struct student come[std];
struct student *ptr = NULL;
ptr=come;
info = (student**)malloc(std*sizeof(student*));
for(i = 0; i < std; i++){
printf("Enter detail of student #%d\n", (i + 1));
ptr->id=i+1;
printf("Enter first name: ");
scanf("%s", ptr->name);
printf("Enter last name: ");
scanf("%s", ptr->surname);
printf("Enter semester: ");
scanf("%d", &ptr->semester);
printf("Enter grade: ");
scanf("%f", &ptr->grade);
ptr++;
}
}
else if (x==2)
{
/* code */
}
else if (x==3)
{
/* code */
}
else if (x==4)
{
/* code */
}
else if (x==5)
{
/* code */
}
else if (x==6)
{
}
else if (x==7)
{
struct student come[std];
struct student *ptr = NULL;
ptr=come;
for(i = 0; i < std; i++){
printf("%d", ptr->id);
printf("%s", ptr->name);
printf("%s", ptr->surname);
printf("%d", ptr->semester);
printf("%f", ptr->grade);
printf("\nn");
ptr++;
}
}
}
At first I used come[i].grade for all of them (name,id,etc) but it would return something along the lines of 19845241814DNva1280.000000 . Then I added the "&" before it but it gave me 6422088oIv4DNva64221920.000000. The last thing i tried is going with ptr->id but no luck.It just returned to me 19845241814DNv`a1280.000000. The same thing as the first.
The problem is how you use the pointers. There is no necessary use of a pointer to pointer.
#include <stdio.h>
#include <stdlib.h>
typedef struct student{
int id;
char name[50];
char surname[50];
int semester;
float grade;
}student;
int main()
{
int x,std,i;
x=0;
struct student *ptr = NULL;
while (x!=8)
{
printf("1. Initialize student list\n2. Add a student record\n3. Delete a student record\n4. Display a student record by student surname\n5. Display students passed\n6. Display students failed\n7. Display all student records\n8. Exit");
printf("\nYour choice: ");
scanf("%d",&x);
if (x==1)
{
printf("How many new students? ");
scanf("%d",&std);
ptr = (student*)malloc(std*sizeof(student));
for(i = 0; i < std; i++){
printf("Enter detail of student #%d\n", (i + 1));
ptr[i].id=i+1;
printf("Enter first name: ");
scanf("%s", ptr[i].name);
printf("Enter last name: ");
scanf("%s", ptr[i].surname);
printf("Enter semester: ");
scanf("%d", &ptr[i].semester);
printf("Enter grade: ");
scanf("%f", &ptr[i].grade);
}
}
else if (x==2)
{
/* code */
}
else if (x==3)
{
/* code */
}
else if (x==4)
{
/* code */
}
else if (x==5)
{
/* code */
}
else if (x==6)
{
}
else if (x==7)
{
for(i = 0; i < std; i++){
printf("%d", ptr[i].id);
printf("%s", ptr[i].name);
printf("%s", ptr[i].surname);
printf("%d", ptr[i].semester);
printf("%f", ptr[i].grade);
printf("\n");
}
}
}
}
your fundamental problem is that you create a fresh student table for each command (1 and 7). So in the display command you are displaying n unitialized student records.
You also seem to have got into 'grasping at straws' mode for storing the student records, you have pointers, mallocs, dynamically sized stack arrays, 2 dimensional malloced arrays....
option 1 should malloc an array of students of the correct size. The array pointer should be declared before any of the loop code , so that yiou use the same array all the time
student *info = NULL;
int students=0;
...
while (x!=8){
.....
if(x==1){
....
info = malloc(std*sizeof(student));
}
you now have a correctly sized array . to add a student
scanf("%s", info[students].name);
etc
then `students++` to keep count of the number of active students

invalid type argument of '->' editing fields of a structure from a separate function

I'm having problems editing the fields of a structure from a seperate function, I'm trying to edit fields of my drone structure from the update droneinfofunction .basically i get the same error for all the arrows (invalid type argument of '->')
i'm sure this problem stems from my lack of understanding of pointers
any help would be greatly appreciated :)
here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DRONE_COUNT 10
typedef struct{
//define struct info and variables
int drone_number;
char drone_name[20];
int year_manufactured;
double mass;
double top_speed;
double max_distance;
double load_capacity;
} drone_info;
int updateDroneInfo(drone_info *droneinfo, int no_of_drones) {
int searchID, numdrones, i, drYrMan;
double drMass, drTopSpeed, drMaxDist, drLoadCap;
char drname[20];
numdrones = no_of_drones;
printf("what drone ID would you like to update?: ");
scanf("%d", &searchID);
printf("name: ");
scanf("%s", drname);
printf("year manufactured: ");
scanf("%d", &drYrMan);
printf("mass: ");
scanf("%lf", &drMass);
printf("top speed: ");
scanf("%lf", &drTopSpeed);
printf("max distance: ");
scanf("%lf", &drMaxDist);
printf("load capacity: ");
scanf("%lf", &drLoadCap);
droneinfo[searchID]->drone_number = searchID;
droneinfo[searchID]->drone_name = drname;
droneinfo[searchID]->year_manufactured = drYrMan;
droneinfo[searchID]->mass = drMass;
droneinfo[searchID]->top_speed = drTopSpeed;
droneinfo[searchID]->max_distance = drMaxDist;
droneinfo[searchID]->load_capacity = drLoadCap;
for(i=0; i < numdrones; i++){
}
return 0;
}
//drone search function
int searchDroneName(drone_info *droneinfo, int no_of_drones){
int i, found;
char namechoice[20];
printf("input drone name: ");
scanf("%s", namechoice);
found=0;
scanf("what drone would you like to search %s", namechoice);
for (i=0; i < no_of_drones; i++){
if (!strcmp(namechoice, droneinfo[i].drone_name)) {
printf("found a match\n\nID: %d Name: %s Year: %d Mass: %.2f Top Speed: %.2f Max Distance: %.2f Load Capacity: %.2f\n",
droneinfo[i].drone_number, droneinfo[i].drone_name, droneinfo[i].year_manufactured, droneinfo[i].mass, droneinfo[i].top_speed, droneinfo[i].max_distance, droneinfo[i].load_capacity);
found = 1;
}
}
if(found == 0){
printf("\nNo matches were found!\n");
}
return 0;
//make condition for all
}
int main(void) {
drone_info droneinfo[10];
int choice, droneID, yrman, i, no_of_drones;
double dronemass, dronemaxdist, dronetopspd, droneload;
char dronename[20];
i=0;
//open the drone.txt file where the drone info is stored
FILE* inputfile = fopen("drone.txt", "r");
if(inputfile == NULL)
{
perror("ERROR! ");
exit(-1);
}
//initialise the function that puts the struct in an array
while(fscanf(inputfile, "%d %19s %d %lf %lf %lf %lf", &droneID, dronename, &yrman, &dronemass, &dronetopspd, &dronemaxdist, &droneload)==7){
if(ferror(inputfile)){
perror("An error occurred: ");
}
droneinfo[i].drone_number = droneID;
strcpy(droneinfo[i].drone_name, dronename);
droneinfo[i].year_manufactured = yrman;
droneinfo[i].mass = dronemass;
droneinfo[i].top_speed = dronetopspd;
droneinfo[i].max_distance = dronemaxdist;
droneinfo[i].load_capacity = droneload;
i++;
}
no_of_drones = i;
fclose(inputfile);
//print the dtone info in an array
printf("Data:\n\n");
for (i=0; i < no_of_drones; i++){
printf("ID: %d Name: %s Year: %d Mass: %.2f Top Speed: %.2f Max Distance: %.2f Load Capacity: %.2f\n",
droneinfo[i].drone_number, droneinfo[i].drone_name, droneinfo[i].year_manufactured, droneinfo[i].mass, droneinfo[i].top_speed, droneinfo[i].max_distance, droneinfo[i].load_capacity);
}
do{
//program menu with appropriate menu items
printf("Please select an option:\n\n");
printf("1. Input/update drone information\n");
printf("2. Search a drone\n");
printf("3. Simulate a drone delivery scenario\n");
printf("4. Display simulation results\n");
printf("5. Save drone information\n");
printf("6. Save all results\n");
printf("7. Exit\n\n");
scanf("%d", &choice);
//switch for the 7 available menu cases
switch(choice)
{
case 1:
//input drone function
updateDroneInfo(droneinfo, no_of_drones);
break;
case 2:
//search drone function
searchDroneName(droneinfo, no_of_drones);
break;
case 3:
//simulate drone function
break;
case 4:
//display simulation results
break;
case 5:
//save drone information
break;
case 6:
//save all results function
break;
case 7:
//exit/breaks the loop
break;
default:
printf("Invalid Data Entered! please enter a number between 1 and 7\n\n");
break;
}
} while(choice != 7);
return 0;
}
re
droneinfo[searchID]
has the type drone_info and not drone_info*, so you use . instead of ->.
In statements like this:
droneinfo[searchID]->drone_number = searchID;
the expression droneinfo[searchID] is not a pointer. It has the type drone_info because the pointer droneinfo was already dereferenced by the subscript operator.
You have to write:
droneinfo[searchID].drone_number = searchID;
Also arrays do not have the assignment operator. You need to copy element elements from one array to another.
Instead of this statement:
droneinfo[searchID]->drone_name = drname;
you have to write using the standard string function strcpy:
$include <string.h>
//...
strcpy( droneinfo[searchID].drone_name, drname );

Having trouble with C pointers

First of all, im a begginer C programmer, studying in a university. I need to do a program that is kinda like a data base. Thus far i have made the function to input new data, it works, but compiler shows a few warning and notes, i tried a lot of things, but nothing helped. Here is the code:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
struct subjects{
char *subjName[100];
char *lectName[100];
char *lectSurname[100];
double credits;
double studentnum;
}subj;
char subjectname[][100];
char lecturername[][100];
char lecturersurname[][100];
int credits[100];
int students[100];
int newentry=0;
void listInput();
void listEdit();
void listDelete();
void listPrint();
int userChoice();
int main() {
int select;
int r=1;
while(r!=0){
select=userChoice();
switch(select){
case 1:
listPrint();
break;
case 2:
listInput();
break;
case 3:
listDelete();
break;
case 4:
listEdit();
break;
case 0:
r=0;
break;
}
}
return 0;
}
int userChoice(){
int choice,input=0,check;
printf("(1). View all the data\n");
printf("(2). Enter new data\n");
printf("(3). Delete data\n");
printf("(4). Edit data\n");
printf("(0). Exit\n");
printf("-----------------------------\n");
while(input!=1){
printf("Enter your choice\n");
scanf("%d", &choice);
if(choice>4 || choice<0){
printf("Invalid input \n");
}
else
input = 1;
}
return choice;
}
void listPrint(){
for(int i=0; i<newentry; i++){
printf("%d, %s, %s, %s, %d, %d\n",i+1, subjectname[i], lecturername[i], lecturersurname[i], credits[i], students[i]);
}
}
void listInput(){
int i;
char firstLetter,term;
printf("Enter the name of the subject \n");
while(scanf("%s", &subj.subjName)!=1)
printf("Error, subject name must be a text ");
printf("Enter the name of the lecturer \n");
int valid=0;
while(valid!=1){
while(scanf("%s", &subj.lectName)!=1)
printf("Error, lecturer's' name must be a text ");
firstLetter=*subj.lectName;
if(firstLetter>65 && firstLetter<90 && isalpha(firstLetter)){
valid=1;
}
else
printf("Error, lecturer's name must start with a capital letter, try again \nEnter the name of the lecturer \n");
}
valid=0;
printf("Enter the surname of the lecturer\n");
while(valid!=1){
while(scanf("%s", &subj.lectSurname)!=1)
printf("Error, lecturer's surname must be a text ");
firstLetter=*subj.lectSurname;
if(firstLetter>65 && firstLetter<90 && isalpha(firstLetter)){
valid=1;
}
else
printf("Lecturer's surname must start with a capital letter, try again \nEnter the surname of the lecturer \n");
}
printf("Enter the amount of credits in course \n");
while(1){
scanf("%lf", &subj.credits);
if(subj.credits<0 || subj.credits != (int)subj.credits)
printf("Error, amount of credits must be a positive integer, try again \n");
if(subj.credits>0 && subj.credits == (int)subj.credits)
break;
}
printf("Enter the number of students \n");
while(1){
scanf("%lf", &subj.studentnum);
if(subj.studentnum<0 || subj.studentnum != (int)subj.studentnum)
printf("Error, number of students must be a positive integer,try again \n");
if(subj.studentnum>0 && subj.studentnum == (int)subj.studentnum)
break;
}
printf("Added a new entry.\n\n");
strncpy(subjectname[newentry], subj.subjName, 99);
strncpy(lecturername[newentry], subj.lectName, 99);
strncpy(lecturersurname[newentry], subj.lectSurname, 99);
credits[newentry]=subj.credits;
students[newentry]=subj.studentnum;
newentry++;
}
void listDelete(){
printf("33333333333");
}
void listEdit(){
printf("4444444");
}
And the warnings:
In function 'listInput':
96 14 [Warning] assignment makes integer from pointer without a cast
108 14 [Warning] assignment makes integer from pointer without a cast
132 33 [Warning] passing argument 2 of 'strncpy' from incompatible pointer type
79 9 [Note] expected 'const char * restrict' but argument is of type 'char **'
133 34 [Warning] passing argument 2 of 'strncpy' from incompatible pointer type
79 9 [Note] expected 'const char * restrict' but argument is of type 'char **'
134 37 [Warning] passing argument 2 of 'strncpy' from incompatible pointer type
79 9 [Note] expected 'const char * restrict' but argument is of type 'char **
At top level:
14 6 [Warning] array 'subjectname' assumed to have one element
15 6 [Warning] array 'lecturername' assumed to have one element
16 6 [Warning] array 'lecturersurname' assumed to have one element
What can i do to fix those warnings? The program works just fine, but i cant pass if i dont fix the warnings.
Here is a better way to do things
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char *subjName;
char *lectName;
char *lectSurname;
double credits;
double num_students;
}Subject;
typedef struct{
Subject **subjs;
int num_subjs;
}Subjects;
void listInput();
void listEdit();
void listDelete();
void listPrint();
int userChoice();
int main() {
Subjects *subjects = malloc(sizeof(Subjects));
subjects->num_subjs = 0;
subjects->subjs = NULL;
int r=1;
while(r!=0){
int select=userChoice();
switch(select){
case 1:
listPrint(subjects);
break;
case 2:
listInput(&subjects);
break;
case 3:
listDelete();
break;
case 4:
listEdit();
break;
case 0:
r=0;
break;
}
}
return 0;
}
int userChoice(){
int choice,input=0,check;
printf("(1). View all the data\n");
printf("(2). Enter new data\n");
printf("(3). Delete data\n");
printf("(4). Edit data\n");
printf("(0). Exit\n");
printf("-----------------------------\n");
while(input!=1){
printf("Enter your choice\n");
scanf("%d", &choice);
if(choice>4 || choice<0){
printf("Invalid input \n");
}
else
input = 1;
}
return choice;
}
void listPrint(Subjects *subjects){
for(int i=0; i< subjects->num_subjs; i++){
printf("%d, %s, %s, %s, %d, %d\n",i+1, subjects->subjs[i]->subjName, subjects->subjs[i]->lectName, subjects->subjs[i]->lectSurname, subjects->subjs[i]->credits, subjects->subjs[i]->num_students);
}
}
char *getln()
{
char *line = NULL, *tmp = NULL;
size_t size = 0, index = 0;
int ch = 1;
while (ch) {
ch = getc(stdin);
if (ch == '\n')
ch = 0;
if (size <= index) {
size += 1;
tmp = realloc(line, size);
if (!tmp) {
free(line);
line = NULL;
break;
}
line = tmp;
}
line[index++] = ch;
}
return line;
}
int isText(char *str,char *name){
for(int i = 0; i < strlen(str);i++){
if((str[i]<'A' || str[i]>'z') && str[i]!=' '){
printf("Error, %s must be a text \n",name);
return 0;
}
}
return 1;
}
int isNegative(int n){
if(n < 0){
printf("Error, number of students must be a positive integer,try again \n");
return 1;
}
return 0;
}
void listInput(Subjects **p_subjects){
while ( getchar() != '\n' );
Subject *new_subj = malloc(sizeof(Subject));
new_subj->subjName = NULL;
new_subj->lectName = NULL;
new_subj->lectSurname = NULL;
new_subj->credits = 0;
new_subj->num_students = 0;
do{
printf("Enter the name of the subject \n");
new_subj->subjName = getln();
}while(!isText(new_subj->subjName,"Subject name"));
do{
printf("Enter the name of the lecturer \n");
new_subj->lectName = getln();
}while(!isText(new_subj->lectName,"Lecturer's name"));
do{
printf("Enter the surname of the lecturer\n");
new_subj->lectSurname = getln();
//Convert to uppercase
new_subj->lectSurname[0] &= '_';
}while(!isText(new_subj->lectSurname,"Lecturer's surname"));
do{
printf("Enter the number of credits \n");
scanf("%d", &new_subj->credits);
}while(isNegative(new_subj->num_students));
do{
printf("Enter the number of students \n");
scanf("%d", &new_subj->num_students);
}while(isNegative(new_subj->num_students));
(*p_subjects)->subjs = realloc((*p_subjects)->subjs,sizeof(Subject*)*(++(*p_subjects)->num_subjs));
(*p_subjects)->subjs[(*p_subjects)->num_subjs-1] = new_subj;
printf("Added a new entry.\n\n");
}
void listDelete(){
printf("33333333333");
}
void listEdit(){
printf("4444444");
}

error: expression must have struct or union type in c

I've been working on a code to store names and grades of students, then recall the grade when the students' names are entered.
Here is my code:
#include <stdio.h>
#define N 10
#define M 2
struct a
{
char name[50];
int grade;
};
int main()
{
int i;
int j;
struct a A[N][M];
for(i=0;i<N;i++)
{
printf("Please Enter Students' Names:/n");
scanf("%s", &A[i].name);
}
for(j=0;j<M;j++)
{
printf("Please Enter Students' Grades:/n");
scanf("%d", &A[j].grade);
}
printf("Which Student's Grades Would You Like To View?/n");
if(scanf("%s", *A[i].name))
{
printf("Their Grade Is:%d/n", *A[j].grade);
}
return 0;
}
I've been getting these errors:
hw2problem2.c(21): error: expression must have struct or union type
scanf("%s", &A[i].name);
^
hw2problem2.c(26): error: expression must have struct or union type
scanf("%d", &A[j].grade);
^
hw2problem2.c(29): error: expression must have struct or union type
if(scanf("%s", *A[i].name))
^
hw2problem2.c(31): error: expression must have struct or union type
printf("Their Grade Is:%d/n", *A[j].grade);
^
compilation aborted for hw2problem2.c (code 2)
Any help with the errors or the program in general would be appreciated.
Thanks.
You defined struct a A as a two dimensional array, and only specified
one dimension in scanf("%s", &A[i].name); and scanf("%d", &A[j].grade);.
You have a couple of other issues, like scanf("%s", &A[i].name);... where the
& is unnecessary.
Your program should be like this
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
printf("Please Enter Students' Grades:/n");
scanf("%d", &A[i][j].grade);
printf("Please Enter Students' Names:/n");
scanf("%s", &A[i][j].name);
}
}
Because, A[i] is of type struct a*, not struct a. It should be A[i][j]
Logically, your array should be 1-D. Hence, loop should like:
struct a A[N];
for(i=0;i<N;i++)
{
printf("Please Enter Students' Names:/n");
scanf("%s", &A[i].name);
}
for(j=0;j<N;j++)
{
printf("Please Enter Students' Grades:/n");
scanf("%d", &A[j].grade);
}
If it is subject wise, then it should be 2D and use nested loop as shown.
No need to use 2D array. Simply try this..
#include <stdio.h>
#define N 3
struct a
{
char name[50];
int grade;
};
int main()
{
int i;
struct a A[N];
char sname[50];
for(i=0;i<N;i++)
{
printf("Please Enter Students' Names:/n");
scanf("%s", A[i].name);
}
for(i=0;i<N;i++)
{
printf("Please Enter Students' Grades:/n");
scanf("%d",&A[i].grade);
}
printf("Which Student's Grades Would You Like To View?/n\n");
printf("Enter the name...\n");
scanf("%s",sname);
for(i=0;i<N;i++)
{
if(strcmp(A[i].name,sname)==0)
{
printf("%s grade is %d\n",A[i].name,A[i].grade);
break;
}
else
{
if(i==N-1)
printf("No such a name in your list...\n");
}
}
return 0;
}

Using pointers with structures to pass data to functions

This is a program for a class project. It is suppose to be able to create and edit structures. The CreatRec function works fine. For the ModifyRec I am trying to send it the array by pointers in order to avoid having to "copy" the data. However, I am having trouble getting it to actually change the array. ATM The line at the bottom (gr[change].lastname= *info;) is not working at all. I really have no clue what I am doing wrong here.
#include "stdafx.h"
#include <string.h>
#include <stdlib.h>
struct student{
int recordname;
char lastname[10];
char firstname[10];
float math;
float english;
float science;
};
//prototypes
int menu();
struct student CreatRec(int);
void ModifyRec(struct student*);
void main()
{
int option, j;//option will be for users menu choice, j makes for loop work for creatrec
struct student grades[10];
j = 0;
option=Menu();
if (option == 1)
for (j = 0; j<10; j++)
(grades[j + 0]) = CreatRec(j);
else if (option==2)
ModifyRec(grades);//dont need & is smart bc array
printf("%s",grades[0].lastname);//This line is checking to see if ModifyRec actaully worked
//free(grades);2
while (1);
}
int Menu()
{
int choi;
printf("Please choose one of the following options.\n 1) Create New Student Records.\n 2) Modify an Existing Student Record\n");
printf(" 3) Print a New Sutdent Record.\n 4) Quit\n");
scanf("%d", &choi);
return choi;
}
struct student CreatRec(int i)
{
struct student qr;
//qr = (struct student*)malloc(sizeof(struct student)*6);
printf("RecordNum %i\n", i);
printf("Please enter last name-->");
scanf("%s", &qr.lastname);
printf("Please enter first name-->");
scanf("%s", &qr.firstname);
printf("Please math grade-->");
scanf("%f", &qr.math);
printf("Please english grade-->");
scanf("%f", &qr.english);
printf("Please science grade-->");
scanf("%f", &qr.science);
return qr;
}
void ModifyRec(struct student gr[])
{
int change;
char feild[10], info[10];
printf("Which record would you like to change?\n");
scanf("%d", &change);
rewind(stdin);
printf("Which feild would you like to edit?\n");
scanf("%s", &feild);
rewind(stdin);
printf("Enter info\n");
scanf("%s", &info);
if (!strcmp("lastname", feild))
gr[change].lastname= *info;//NOT WORKING
}
First of all I do not see a great sense in expression grades[j + 0] of statement
for (j = 0; j<10; j++)
(grades[j + 0]) = CreatRec(j);
These statements
printf("Please enter last name-->");
scanf("%s", &qr.lastname);
printf("Please enter first name-->");
scanf("%s", &qr.firstname);
have to be substituted for
printf("Please enter last name-->");
scanf("%s", qr.lastname);
printf("Please enter first name-->");
scanf("%s", qr.firstname);
And this statement
if (!strcmp("lastname", feild))
gr[change].lastname= *info;//
has to be substituted for
if (!strcmp("lastname", feild))
strcpy( gr[change].lastname, info );
gr[change].lastname is a char array, not a pointer. You can't reassign it. In this case, you probably ought to do scanf("%s", gr[change].lastname); and skip char info[10] altogether.

Resources