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
Related
I am trying to build a program which takes user input of employee details and prints it. I have separate functions for both of them.
Structure is as follows:
struct employee
{
int empId;
char name[20];
char empType[10];
int dd, mm, yyyy;
};
struct employee input()
{
struct employee e;
printf("Enter Employee ID: \n");
scanf("%d", &e.empId);
printf("Enter Employee name: \n");
scanf("%s", &e.name);
printf("Enter employee type: \n");
scanf("%s", &e.empType);
printf("Enter joining date: \n");
scanf("%d/%d/%d", &e.dd, &e.mm, &e.yyyy);
}
void display(struct employee emp[], int n)
{
printf("Employee ID \t Name \t Employee Type \t Joining date\n");
int i;
for (i = 0; i < n; i++)
{
printf("%d %s %s %d/%d/%d", emp[i].empId, emp[i].name, emp[i].empType, emp[i].dd, emp[i].mm, emp[i].yyyy);
}
}
int main()
{
int n = 5;
struct employee emp[n];
int i;
for(i = 0; i < n ;i++)
{
emp[i] = input();
}
display(emp, n);
return 0;
}
I am able to take the input properly but while printing I am getting all values as 0.
Requesting help!
Thanks!
You forgot about the return statement in the function input
//...
return e;
Also the arguments of these calls of scanf
printf("Enter Employee name: \n");
scanf("%s", &e.name);
printf("Enter employee type: \n");
scanf("%s", &e.empType);
are incorrect. You need to write
printf("Enter Employee name: \n");
scanf("%19s", e.name);
printf("Enter employee type: \n");
scanf("%9s", e.empType);
In general you should check that inputs were successful.
That for loop is unneccessary - you would end up printing the output 5 times, for some reason.
Also use typedef struct, it's convenient.
And don't type & when dealing with struct's string elements.
This code is a mess, by the way.
I am writing a program where if the user chooses "2" the program will add a new student record and if he chooses "4" then it will compare his surname with all the other surnames, it will find everyone that has the same surname and print their school information. The problems are the following:
When the user selects "2" my program actually shows all the prompts that are needed and you can input all the information however when I try to print it just gives me 0 0 0.000000 .When it should have given me 2(id) B(name) b(surname) 1(semester) 4(grade).
When the user selects "4" it reads the surname but when it goes to compare it with the others to see if it exists or not it just crashes.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.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;
int car=0;
int flag=1;
struct student *ptr = NULL;
while (x!=8)
{
printf("\n1. 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)
{
ptr=realloc(ptr,100* sizeof(student));
std=std+1;
ptr[std].id=std+1;
printf("Enter first name: ");
scanf("%s", ptr[std].name);
printf("Enter last name: ");
scanf("%s", ptr[std].surname);
printf("Enter semester: ");
scanf("%d", &ptr[std].semester);
printf("Enter grade: ");
scanf("%f", &ptr[std].grade);
flag=0;
}
else if (x==3)
{
/* code */
}
else if (x==4)
{
printf("Give the surname: ");
const char *sur;
scanf("%s", sur);
for (i = 0; i < std; i++)
{
if(strcmp(sur, ptr[i].surname)==0){
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");
car=1;
}
}
if (car=0)
{
printf("That surname does not exist");
}
}
I may have missed a "{" but this is only part of my code
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");
}
}
}
}
For when the user chooses "2" everything goes well until its time to print so i tried increasing std by 1 two times.One when the program goes to option "2" and another time when it goes back to main. It didnt do anything so i returned it to how it was in the beginning.
For when the user chooses "4" I tried going with strcmp but it would just throw errors because of sur. I had it written as char sur. After I wrote it as const char *sur it stopped throwing errors but it would just crash when it went to compare it with the other.
I want the user to enter "M" for Male or "F" for Female and the single character must be replaced by Male or Female but here the strcmp function is not working everytime time it is showing "Please enter correct value for Gender!!".
#include<stdio.h>
#include<string.h>
struct student{
char name[50];
int Roll_No;
char Gender[10];
};
int main()
{
int size;
printf("Enter the size for number of students you want to store:");
scanf("%d",&size);
struct student s[size];
int i;
for(i=0;i<size;i++)
{
printf("Enter the name of student s[%d]:",i);
fflush(stdin);
fgets(s[i].name,50,stdin);
printf("Enter the Roll Number of student s[%d]:",i);
scanf("%d",&s[i].Roll_No);
gen:printf("Enter your Gender(M-Male F-Female) for student s[%d]:",i);
fflush(stdin);
fgets(s[i].Gender,10,stdin);
if(strcmp(s[i].Gender,"M")==0)
{
strcpy(s[i].Gender,"Male");
}
if(strcmp(s[i].Gender,"F")==0)
{
strcpy(s[i].Gender,"Female");
}
else
{
printf("Please enter correct value for Gender!!\n");
goto gen;
}
}
printf("\nInformation entered of Students:\n");
for(i=0;i<size;i++)
{
printf("Name of student s[%d]:%s",i,s[i].name);
printf("Roll Number of student s[%d]:%d\n",i,s[i].Roll_No);
printf("Gender of student s[%d]:%s\n",i,s[i].Gender);
}
return 0;
}
The function fgets can append the entered string with the new line character '\n' that you should remove.
For example
fgets(s[i].Gender,10,stdin);
s[i].Gender[ strcspn( s[i].Gender, "\n" ) ] = '\0';
if(strcmp(s[i].Gender,"M")==0)
{
strcpy(s[i].Gender,"Male");
}
if(strcmp(s[i].Gender,"F")==0)
{
strcpy(s[i].Gender,"Female");
}
else
{
printf("Please enter correct value for Gender!!\n");
goto gen;
}
Pay attention to that instead of the using the goto statement you could enclose the code in the do-while statement.
Also to enter a letter there is no great sense to use the function fgets moreover when the entered string will be overwritten.
You could use an object of the type char and use a call of scanf like this
char gender;
scanf( " %c", &gender );
And then
if ( toupper( ( unsigned char )gender ) == 'M' )
{
strcpy(s[i].Gender,"Male");
}
//...
I have fixed your code. There was a number of errors.
You flush stdin
You mix scanf() and fgets()
Missing else when checking for "F"
There are other areas where to code could be enhanced. I let it as an exercise for you :-)
#include<stdio.h>
#include<string.h>
struct student {
char name[50];
int Roll_No;
char Gender[10];
};
int main()
{
int size;
struct student s[100];
int i;
char buf[100];
printf("Enter the size for number of students you want to store:");
fgets(buf, 100, stdin);
sscanf(buf, "%d", &size);
for (i = 0; i < size; i++)
{
printf("Enter the name of student s[%d]:", i);
fgets(s[i].name, 50, stdin);
printf("Enter the Roll Number of student s[%d]:", i);
fgets(buf, 10, stdin);
sscanf(buf, "%d", &s[i].Roll_No);
gen:printf("Enter your Gender(M-Male F-Female) for student s[%d]:", i);
fgets(buf, 10, stdin);
sscanf(buf, "%s", s[i].Gender);
if (strcmp(s[i].Gender, "M") == 0)
{
strcpy(s[i].Gender, "Male");
}
else if (strcmp(s[i].Gender, "F") == 0)
{
strcpy(s[i].Gender, "Female");
}
else
{
printf("Please enter correct value for Gender!!\n");
goto gen;
}
}
printf("\nInformation entered of Students:\n");
for (i = 0; i < size; i++)
{
printf("Name of student s[%d]:%s", i, s[i].name);
printf("Roll Number of student s[%d]:%d\n", i, s[i].Roll_No);
printf("Gender of student s[%d]:%s\n", i, s[i].Gender);
}
return 0;
}
I have this project where I have to create a struct array with C language for students' data, and I can't seem to get past this warning by my variable arr_student. The warning says that I have not initialized the variable, and whenever I try to debug it the IDE says that it has a memory error involving where it goes. I want to be able to declare it and use it as a way to get to the variables created in my struct array. If anyone knows what I could be missing, that would be greatly appreciated!
#include <stdio.h>
#include <string.h>
#define numero 2 //This number is the limiter for the number of students
struct Student
{ //Define struct array with student information
char id[50];
char gpa[50];
char address[50];
char phone_number[50];
char first_name[50];
char last_name[50];
};
int main(struct Student arr_student[numero])
{
int i; //Counter
char tempvalue[50]; //Temporary variable to store data in the array
char search[50]; //Search value input by user
int result = 1; //Initialized result to false
for (i = 0; i < numero; i++)
{
//Asks user for input on student information.
printf("\nEnter the information for student %d\n\n", i + 1);
printf("\nEnter first name: ");
scanf("%s", tempvalue);
printf("%s", tempvalue);
//prints value to verify if tempvalue recieved
strcpy(arr_student[i].first_name, tempvalue);
//error begins with the arr_student being underlined
printf("\nEnter last name: ");
scanf("%s", tempvalue);
strcpy(arr_student[i].last_name, tempvalue);
printf("\nEnter student id: ");
scanf("%s", tempvalue);
strcpy(arr_student[i].id, tempvalue);
printf("\nEnter student gpa: ");
scanf("%s", tempvalue);
strcpy(arr_student[i].gpa, tempvalue);
printf("\nEnter student address: ");
scanf("%s", tempvalue);
strcpy(arr_student[i].address, tempvalue);
printf("\nEnter student phone number: ");
scanf("%s", tempvalue);
strcpy(arr_student[i].phone_number, tempvalue);
}
printf("Enter the last name of the student you wish to examine data for: ");
//Asks input from the user for a name to search the data for
scanf("%s", search);
for (i = 0; i < numero; i++)
{
result = strcmp(search, arr_student[i].last_name);
}
if (result == 0) //A match is found in the array
{
printf("Here is the data on the student: %s", search);
printf("First Name\t Last Name\t ID\t GPA\t Address\t Phone Number\n");
//Prints out student information
printf("%s\t%s\t%s\t%s\t%s\t%s\n",
arr_student[i].first_name, arr_student[i].last_name, arr_student[i].id,
arr_student[i].gpa, arr_student[i].address, arr_student[i].phone_number);
}
else
{
printf("The name you have entered is not in our system, please try again");
return;
}
return 0;
}
Change the invalid main() definition from
int main(struct Student arr_student[numero])
{
to
int main(void) {
struct Student arr_student[numero] = {0};
Use width limited input.
char tempvalue[50];
// scanf("%s", tempvalue);
scanf("%49s", tempvalue);
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.