Attempting to run this code will display blank inputs for most of the fields and will be mixed up, for example the street number would be in place of first name. Not sure what's going on. Was hesitant about using stackoverflow at first but now it seems I have no choice
header file:
// Structure type Name declaration
struct Name {
char firstName[31];
char middleInitial[7];
char lastName[36];
};
// Structure type Address declaration
struct Address {
int streetNumber;
char street[41];
int apartmentNumber;
char postalCode[8];
char city[41];
};
// Structure type Numbers declaration
struct Numbers {
char cell[11];
char home[11];
char business[11];
};
// Structure type Contact declaration
struct Contact {
struct Name name;
struct Address address;
struct Numbers numbers;
};
//------------------------------------------------------
// Function Prototypes
//------------------------------------------------------
// Get and store from standard input the values for Name
void getName(struct Name* name);
// Get and store from standard input the values for Address
void getAddress(struct Address* address);
// Get and store from standard input the values for Numbers
void getNumbers(struct Numbers* numbers);
input file
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "contacts.h"
//This function will clear the input buffer after every input
void clear() {
while (getchar() != '\n');
}
// Get and store from standard input the values for Name
void getName(struct Name* name) {
char option = 0;
printf("Please enter the contact's first name: ");
scanf("%31s", name[0].firstName);
clear();
printf("Do you want to enter a middle initial(s)? (y or n): ");
scanf("%c", &option);
clear();
if (option == 'y' || option == 'Y') {
printf("Please enter the contact's middle initial(s): ");
scanf("%7[^\n]s", name[0].middleInitial);
clear();
}
printf("Please enter the contact's last name: ");
scanf("%36[^\n]s", name[0].lastName);
clear();
}
// Get and store from standard input the values for Address
void getAddress(struct Address* address) {
char option = 0;
printf("Please enter the contact's street number: ");
scanf("%d", &address[0].streetNumber);
clear();
printf("Please enter the contact's street name: ");
scanf("%40[^\n]s", address[0].street);
clear();
printf("Do you want to enter an apartment number? (y or n): ");
scanf("%c", &option);
clear();
if (option == 'y' || option == 'Y') {
printf("Please enter the contact's apartment number: ");
scanf("%d", &address[0].apartmentNumber);
clear();
}
printf("Please enter the contact's postal code: ");
scanf("%7[^\n]s", address[0].postalCode);
clear();
printf("Please enter the contact's city: ");
scanf("%40[^\n]s", address[0].city);
clear();
}
// Get and store from standard input the values for Numbers
void getNumbers(struct Numbers* numbers) {
char option = 0;
printf("Do you want to enter a cell phone number? (y or n): ");
scanf("%c", &option);
clear();
if (option == 'y' || option == 'Y') {
printf("Please enter the contact's cell phone number: ");
scanf("%11s", numbers[0].cell);
clear();
}
printf("Do you want to enter a home phone number? (y or n): ");
scanf("%c", &option);
clear();
if (option == 'y' || option == 'Y') {
printf("Please enter the contact's home phone number: ");
scanf("%11s", numbers[0].home);
clear();
}
printf("Do you want to enter a business phone number? (y or n): ");
scanf("%c", &option);
clear();
if (option == 'y' || option == 'Y') {
printf("Please enter the contact's business phone number: ");
scanf("%11s", numbers[0].business);
clear();
}
}
my main program:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "contacts.h"
int main(void)
{
// Declare variables:
struct Contact contact[] = {{0}};
// Display the title
printf("Contact Management System\n");
printf("-------------------------\n");
// Call the Contact function getName to store the values for the Name member
getName(contact);
// Call the Contact function getAddress to store the values for the Address member
getAddress(contact);
// Call the Contact function getNumbers to store the values for the Numbers member
getNumbers(contact);
// Display Contact summary details
printf("\n");
printf("Contact Details\n");
printf("===============\n");
printf("Name Details\n");
printf("------------\n");
printf("First name: %s\n", contact[0].name.firstName);
printf("Middle initial(s): %s\n", contact[0].name.middleInitial);
printf("Last name: %s\n", contact[0].name.lastName);
printf("\n");
printf("Address Details\n");
printf("--------------\n");
printf("Street number: %d\n", contact[0].address.streetNumber);
printf("Street name: %s\n", contact[0].address.street);
printf("Apartment: %d\n", contact[0].address.apartmentNumber);
printf("Postal code: %s\n", contact[0].address.postalCode);
printf("City: %s\n", contact[0].address.city);
printf("\n");
printf("Phone Numbers\n");
printf("-------------\n");
printf("Cell phone number: %s\n", contact[0].numbers.cell);
printf("Home phone number: %s\n", contact[0].numbers.home);
printf("Business phone number: %s\n", contact[0].numbers.business);
printf("\n");
// Display Completion Message
printf("Structure test for Contact using functions done!\n");
return 0;
}
any help would be greatly appreciated, thanks
you have an error in main when calling your functions
getName(contact);
getAddress(contact);
getNumbers(contact);
your passing a contact struct while they wait for other structure types.
void getName(struct Name* name);
void getAddress(struct Address* address);
void getNumbers(struct Numbers* numbers);
try to correct this with
getName(&contact->name);
getAddress(&contact->address);
getNumbers(&contact->numbers);
Related
So I'm trying to write a program in which I need to enter informations like name, surname, student id, birthday for multiple students . The thing is I can't get it to print the info for all the students. This version of the code I wrote just prints the variables without anything stored in them or with some weird characters. In an earlier version of the script the info I typed in would just overwrite the previous info and it would print just one student's info. I think I need to make some changes in the for loop if I'm not mistaken. If someone could give me a hand, I'd appreciate it.
Here's the code:
#include <stdio.h>
#include <string.h>
#define students 200
typedef struct {
char name[20];
char surname[20];
int studentid[5];
int day[5];
int month[5];
int year[5];
}student;
int main(){
student a[students];
int j;
int n;
int i;
int choice;
for(i=0;i<=students;i++){
printf("\n===========================================================\n");
printf("\n1 Enter info for a student");
printf("\n2 Print all the students");
printf("\n3 End\n");
printf("\n===========================================================\n");
printf("\nChoose something ---> ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter name: \n");
scanf("%s", a[students].name);
printf("Enter surname: \n");
scanf("%s", a[students].surname);
printf("Enter student ID: \n");
scanf("%s", a[students].studentid);
printf("Enter day: \n");
scanf("%s", a[students].day);
printf("Enter month: \n");
scanf("%s", a[students].month);
printf("Enter year: \n");
scanf("%s", a[students].year);
break;
case 2:
for(i=0;i<students;i++)
{
printf("\nNome student: %s\nSurname student: %s\nStudent id: %s\nStudent Birthday: %s.%s.%s\n", a[i].name, a[i].surname, a[i].studentid, a[i].day, a[i].month, a[i].year);
}
break;
case 3:
break;
default:
printf("Choose again!\n");
}
}
return 0;
}
Thanks!
Did you see the warning when you compile:
warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int *’ [-Wformat=]
For string, you should use the character array, so the parameters of the struct student should change to:
char studentid[10];
char day[10]; // 5 is too short, for example monday need at lest 7 characters (1 for null character at the end of string)
char month[10];
char year[10];
You declare the array with length = students but you try to access the (students+1)th (a[students])
The for loop should change to:
for(i=0;i < students;i++){} // i from 0 to students-1 not to students.
You should use the counter (you can declare the variable count for example) to count the number of students that you enter the info. It will be useful when you print the info of these students.
printf("Enter name: \n");
scanf(" %19s", a[count].name);
printf("Enter surname: \n");
scanf(" %19s", a[count].surname);
printf("Enter student ID: \n");
scanf(" %9s", a[count].studentid);
printf("Enter day: \n");
scanf(" %9s", a[count].day);
printf("Enter month: \n");
scanf(" %9s", a[count].month);
printf("Enter year: \n");
scanf(" %9s", a[count].year);
count++; // increase count after each student
break;
case 2 changes to:
for(int j=0;j<count;j++) // just print the students that you set the info in case 1.
{
printf("\nNome student: %s\nSurname student: %s\nStudent id: %s\nStudent Birthday: %s.%s.%s\n", a[j].name, a[j].surname, a[j].studentid, a[j].day, a[j].month, a[j].year);
}
break;
This line: #define students 200 is not wrong, but you should use the difference name and use the uppercase, it's easier to understand the constant value, for example:
#define MAX_NUM_STUDENTS 200
The complete code:
#include <stdio.h>
#include <string.h>
#define MAX_NUM_STUDENTS 200
typedef struct {
char name[20];
char surname[20];
char studentid[10];
char day[10];
char month[10];
char year[10];
}student;
int main(){
student a[MAX_NUM_STUDENTS];
int i, n, choice, count = 0;
for(i=0;i<MAX_NUM_STUDENTS;i++){
printf("\n===========================================================\n");
printf("\n1 Enter info for a student");
printf("\n2 Print all the students");
printf("\n3 End\n");
printf("\n===========================================================\n");
printf("\nChoose something ---> ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter name: \n");
scanf(" %19s", a[count].name);
printf("Enter surname: \n");
scanf(" %19s", a[count].surname);
printf("Enter student ID: \n");
scanf(" %9s", a[count].studentid);
printf("Enter day: \n");
scanf(" %9s", a[count].day);
printf("Enter month: \n");
scanf(" %9s", a[count].month);
printf("Enter year: \n");
scanf(" %9s", a[count].year);
count++;
break;
case 2:
for(int j=0;j<count;j++) // just print the students that you set the info in case 1.
{
printf("\nNome student: %s\nSurname student: %s\nStudent id: %s\nStudent Birthday: %s.%s.%s\n", a[j].name, a[j].surname, a[j].studentid, a[j].day, a[j].month, a[j].year);
}
break;
case 3:
break;
default:
printf("Choose again!\n");
}
}
return 0;
}
You're storing the user input at a[students]. However, the array a (by the way, you should be descriptive with your variable names) is only students-elements long. Therefore, the students-th element is past the end of the array.
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);
My code works fine, except for one line which keeps printing out the data that supposed to be for the next line only.
The output is supposed to look something like this:
Address Details
Street number: 100
Street name: Bedrock
Apartment: 14
Postal code: Z8Z 7R7
City: Markham
Instead my output is this:
Address Details
Street number: 100
Street name: Bedrock
Apartment: 14
Postal code: Z8Z 7R7Markham
City: Markham
As you can see Markham is printed along the same line of postal code.
Below are the potential files which might have caused this error. All help is much appreciated!
a1ms4.c file:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
// This source file needs to "know about" the structures you declared
// in the header file before referring to those new types:
// HINT: put the header file name in double quotes so the compiler knows
// to look for it in the same directory/folder as this source file
// #include your contacts header file on the next line:
#include "contacts.h"
int main(void)
{
// Declare variables here:
// Create a variable of type Contact and call it something self-describing like "contact"
// - HINT: Be sure to initialize the values to 0 and empty C strings
struct Contact contact = {
{ "", "", "" },
{0," ", 0, " ", " "},
{ "", "", "" }
};
// Display the title
printf("Contact Management System\n");
printf("-------------------------\n");
// Call the Contact function getName to store the values for the Name member
getName(&contact.name);
// Call the Contact function getAddress to store the values for the Address member
getAddress(&contact.address);
// Call the Contact function getNumbers to store the values for the Numbers member
getNumbers(&contact.numbers);
// Display Contact summary details
printf("\nContact Details\n");
printf("---------------\n");
printf("Name Details\n");
printf("First name: %s", contact.name.firstName);
printf("\n");
printf("Middle initial(s): %s", contact.name.middleInitial);
printf("\n");
printf("Last name: %s", contact.name.lastName);
printf("\n");
printf("\n");
printf("Address Details\n");
printf("Street number: %d", contact.address.streetNumber);
printf("\n");
printf("Street name: %s", contact.address.street);
printf("\n");
printf("Apartment: %d", contact.address.apartmentNumber);
printf("\n");
printf("Postal code: %s", contact.address.postalCode);
printf("\n");
printf("City: %s", contact.address.city);
printf("\n");
printf("\n");
printf("Phone Numbers:");
printf("\n");
printf("Cell phone number: %s", contact.numbers.cell);
printf("\n");
printf("Home phone number: %s", contact.numbers.home);
printf("\n");
printf("Business phone number: %s", contact.numbers.business);
printf("\n");
printf("\n");
// Display Completion Message
printf("Structure test for Contact using functions done!\n");
return 0;
}
contacts.c file
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
// This source file needs to "know about" the structures you declared
// in the header file before referring to those new types:
// HINT: put the header file name in double quotes so the compiler knows
// to look for it in the same directory/folder as this source file
// #include your contacts header file on the next line:
#include "contacts.h"
// Get and store from standard input the values for Name
// Put your code here that defines the Contact getName function:
void getName(struct Name * name) {
//Variable/Struct declaration:
char notify;
// Contact Name Input:
printf("Please enter the contact's first name: ");
scanf("%30s", name->firstName);
//Prompts the user to see if they want to enter a middle name using y, yes or n, no.
printf("Do you want to enter a middle initial(s)? (y or n): ");
scanf("%s", ¬ify);
if (notify == 'y' || notify == 'Y')
{
printf("Please enter the contact's middle initial(s): ");
scanf("%6s", name->middleInitial);
}
printf("Please enter the contact's last name: ");
scanf("%30s", name->lastName);
}
// Get and store from standard input the values for Address
// Put your code here that defines the Contact getAddress function:
void getAddress(struct Address * address) {
//Variable/Structure declaration:
char notify;
// Contact Address Input:
printf("Please enter the contact's street number: ");
scanf("%d",& (address->streetNumber));
printf("Please enter the contact's street name: ");
scanf("%40s", address->street);
//Prompts the user to see if they want to enter an apartment number using yes, or no.
printf("Do you want to enter an apartment number? (y or n): ");
scanf("%s", ¬ify);
if (notify == 'y' || notify == 'Y')
{
printf("Please enter the contact's apartment number: ");
scanf("%d",& (address->apartmentNumber));
}
printf("Please enter the contact's postal code: ");
scanf(" %[^\n]", address->postalCode);
printf("Please enter the contact's city: ");
scanf(" %40s", address->city);
}
// Get and store from standard input the values for Numbers
// Put your code here that defines the Contact getNumbers function:
void getNumbers(struct Numbers *numbers) {
//Variable/Structure declaration:
char notify;
// Contact Numbers Input:
printf("Do you want to enter a cell phone number? (y or n): "); //Prompt the user to see if they want to enter a cellphone.
scanf(" %s", ¬ify);
if (notify == 'y' || notify == 'Y')
{
printf("Please enter the contact's cell phone number: ");
scanf("%20s", numbers->cell);
}
printf("Do you want to enter a home phone number? (y or n): ");
scanf(" %s", ¬ify);
if (notify == 'y' || notify == 'Y')
{
printf("Please enter the contact's home phone number: ");
scanf("%20s", numbers->home);
}
printf("Do you want to enter a business phone number? (y or n): ");
scanf(" %s", ¬ify);
if (notify == 'y' || notify == 'Y')
{
printf("Please enter the contact's business phone number: ");
scanf("%20s", numbers->business);
}
}
contacts.h
// Structure type Name declaration (Milestone 1)
struct Name {
char firstName[31];
char middleInitial[7];
char lastName[36];
};
// Structure type Address declaration
// Place your code here... (from Milestone 1)
struct Address {
int streetNumber;
char street[40];
int apartmentNumber;
char postalCode[7];
char city[40];
};
// Structure type Numbers declaration
// Place your code here... (from Milestone 1)
struct Numbers {
char cell[20], home[20], business[20];
};
you should have a null terminator on the end of the postal code:
struct Address {
int streetNumber;
char street[40];
int apartmentNumber;
char postalCode[8];// add space for the null terminator
char city[40];
};
printf("Please enter the contact's postal code: ");
scanf(" %[^\n]", address->postalCode);
address->postalCode[7] = '\0'; // add a null terminator
this should be enough to solve the issue
I've got a program for taking input and storing it into structs. The structs are for contact details, Name, Address, and Phone numbers. My program works just fine, I can enter all my information into the program however when I try to printf the results the program crashes halfway through. I think they may be an issue with memory or corruption, or something. The reason I think it may be with corruption is because if I cut off some of my program and compile it, instead of the program crashing I get a 'Run-Time check failure #2 Stack around the variable 'optionAddress' was corrupted' error. Here's my program
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "contacts.h"
int main(void)
{
// Declare variables here:
struct Name names;
char optionName;
struct Address addresses;
char optionAddress;
struct Numbers number;
char optionCell;
char optionHome;
char optionBusiness;
// Display the title
printf("Contact Management System\n");
printf("-------------------------\n");
// Contact Name Input:
printf("Please enter the contact's first name: ");
scanf("%s", &names.firstName);
printf("Do you want to enter a middle initial(s)? (y or n): ");
scanf("%s", &optionName);
while (optionName == 'y' || optionName == 'Y') {
printf("Please enter the contact's middle initial(s): ");
scanf("%s", &names.middleInitial);
break;
}
printf("Please enter the contact's last name: ");
scanf("%s", &names.lastName);
// Contact Address Input:
printf("Please enter the contact's street number: ");
scanf("%s", &addresses.streetNumber);
printf("Please enter the contact's street name: ");
scanf("%s", &addresses.street);
printf("Do you want to enter an apartment number? (y or n): ");
scanf("%s", &optionAddress);
while (optionAddress == 'y' || optionAddress == 'Y') {
printf("Please enter the contact's apartment number: ");
scanf(" %c", &addresses.apartmentNumber);
break;
}
printf("Please enter the contact's postal code: ");
scanf("%s", &addresses.postalCode);
printf("Please enter the contact's city: ");
scanf("%s", &addresses.city);
// Contact Numbers Input:
printf("Do you want to enter a cell phone number? (y or no): ");
scanf("%s", &optionCell);
while (optionCell == 'y' || optionCell == 'Y') {
printf("Please enter the contact's cell phone number: ");
scanf(" %c", number.cell);
break;
}
printf("Do you want to enter a home phone number? (y or n): ");
scanf("%s", &optionHome);
while (optionHome == 'y' || optionHome == 'Y') {
printf("Please enter the contact's home phone number: ");
scanf(" %c", &number.home);
break;
}
printf("Do you want to enter a business phone number? (y or n): ");
scanf("%s", &optionBusiness);
while (optionBusiness == 'y' || optionBusiness == 'Y') {
printf("Please enter the contact's business phone number: ");
scanf(" %c", number.business);
break;
}
// Display Contact Summary Details
printf("Contact Details\n");
printf("---------------\n");
printf("Name Details\n");
printf("First name: ");
printf("%s", names.firstName);
printf("\nMiddle initials(s): ");
printf("%s", names.middleInitial);
printf("\nLast name: ");
printf("%s", names.lastName);
printf("\n\nAddress Details\n");
printf("Street number: ");
printf("%s", addresses.streetNumber);
printf("\nStreet name: ");
printf("%s", addresses.street);
printf("\nApartment: ");
printf("%s", addresses.apartmentNumber);
printf("\nPostal code: ");
printf("%s", addresses.postalCode);
printf("\nCity: ");
printf("%s", addresses.city);
printf("\n\nPhone Numbers: ");
printf("\nCell phone number: ");
printf("%s", number.cell);
printf("\nHome phone number: ");
printf("%s", number.home);
printf("\nBusiness phone number: ");
printf("%s", number.business);
// Display Completion Message
printf("\n\nStructure test for Name, Address, and Numbers Done!");
return 0;
}
And the structs in the header file:
// Structure type Name declaration
struct Name {
char firstName[31];
char middleInitial[7];
char lastName[36];
};
// Structure type Address declaration
// Place your code here...
struct Address {
char streetNumber;
char street[41];
char apartmentNumber;
char postalCode[8];
char city[41];
};
// Structure type Numbers declaration
// Place your code here...
struct Numbers {
char cell[21];
char home[21];
char business[21];
};
My program reaches the point of printing out "Street number: " then stops working. It is a windows error window that shows up, not a compiler window.
My program reaches the point of printing out "Street number: " then
stops working.
Take a look at:
struct Address {
char streetNumber; // declared as character!
char street[41];
char apartmentNumber; // character
char postalCode[8];
char city[41];
};
char optionName;
char optionAddress;
char optionCell;
char optionHome;
char optionBusiness;
Later on you try to read the values to the characters:
scanf("%s", &optionName);
scanf("%s", &optionAddress);
scanf("%s", &optionCell);
scanf("%s", &optionHome);
scanf("%s", &optionBusiness);
scanf("%s", &addresses.streetNumber); // <-------------- string read
using string format %s. That invokes UB since you are reading at least 2 bytes. "y" is a string with string null terminator '\0'. That extra byte overwrites the memory location.
Change declarations of variables to strings or reading format to character read as you already do here:
scanf(" %c", &addresses.apartmentNumber);
scanf("%s", &optionName);
This invites undefined behavior which could lead to run time error you are getting as optionName is char one byte and scanf() with %s will try to write '\0' which will surely go beyond bounds
I just ended up writing these codes. It is a structure of data which is supposed to be written in the FILE. I want all the strings to be entered by the user with spaces in between.
I used fgets(), but fgets() produces a newline in the time of reading the file. Any way to do it? And please explain if any buffer overflow occurs. Thank you.
#include <stdio.h>
FILE *student_records;
struct studentdetails{
char name[20];
int age;
int class_room;
char section[10];
char fathername[20];
char mothername[20];
}totalstudentdetails;
struct studentmarks{
int physics;
int chemistry;
int maths;
int biology;
int english;
}totalstudentmarks;
int main()
{
student_records=fopen("studentrec.dat","a+");
printf("\n ADD STUDENT RECORDS \n\n\n");
printf(" Enter Student Name > ");
scanf("%s",totalstudentdetails.name);
printf(" Enter Student Age > ");
scanf("%d", &totalstudentdetails.age);
getchar();
printf(" Enter Student Class > ");
scanf("%d", &totalstudentdetails.class_room);
getchar();
printf(" Enter Student Section > ");
scanf("%s", totalstudentdetails.section);
printf("Enter Student Father's Name > ");
scanf("%s",totalstudentdetails.fathername);
getchar();
printf("Enter Student mother's Name > ");
scanf("%s",totalstudentdetails.mothername);
getchar();
fwrite(&totalstudentdetails, sizeof(totalstudentdetails), 1, student_records);
/* Student marks details */
printf("\n\n\n\n");
printf(" Marks Obtained(Out Of 100) \n\n");
printf(" Enter Mark In Physics > ");
scanf("%d", &totalstudentmarks.physics);
getchar();
printf(" Enter Mark In Chemistry > ");
scanf("%d", &totalstudentmarks.chemistry);
getchar();
printf(" Enter Mark In Maths > ");
scanf("%d", &totalstudentmarks.maths);
getchar();
printf(" Enter Mark In Biology > ");
scanf("%d", &totalstudentmarks.biology);
getchar();
printf(" Enter Mark In English > ");
scanf("%d", &totalstudentmarks.english);
getchar();
fwrite(&totalstudentmarks, sizeof(totalstudentmarks), 1, student_records);
/* closing file */
fclose(student_records);
}