I am trying to make a menu where you can add employee info and print it out. However I run into an issue where I am unable to carry down the AddEmployeeNumber down to the print function so I can define the size of the array. Anything would be appreciated! I am unsure if I did the parameters correctly too.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
typedef struct Employee
{
char name[5][10];
float hourswork[10];
float hourspay[10];
}EmployeeNumber;
float AddEmployee()
{
int AddEmployeeNumber;
printf("How Many Employees Do You Want To Add: ");
scanf("%d", &AddEmployeeNumber);
struct Employee EmployeeNumber[AddEmployeeNumber];
for (int i = 0; i < AddEmployeeNumber; i++)
{
char name[5][10];
printf("Name: \n");
scanf("%s", &EmployeeNumber[i].name[5][10]);
float hourswork;
printf("Hours Worked: \n");
scanf("%f", &EmployeeNumber[i].hourswork[10]);
float hourspay;
printf("Hourly Rate:\n");
scanf("%f", &EmployeeNumber[i].hourspay[10]);
}
return AddEmployeeNumber;
}
float PrintingAllEmployees(struct Employee EmployeeNumber[])
{
float AddEmployee();
struct Employee EmployeeNumber[AddEmployeeNumber];
for (int i = 0; i < AddEmployeeNumber; i++)
{
printf("Name: %s", EmployeeNumber[i].name[10]);
printf("Hours Worked: %f", EmployeeNumber[i].hourswork[10]);
printf("Hourly Rate: %f", EmployeeNumber[i].hourspay[10]);
}
}
float PrintingAllEmployees(struct Employee EmployeeNumber[]);
int main()
{
int choice;
int x = 1;
while (x = 1)
{
printf("Main Menu\n");
printf("1: Add Employee\n");
printf("2: Edit Employee\n");
printf("3: Print A Employee\n");
printf("4: Print All Employees\n");
printf("5: Quit\n");
printf("Enter Which Section You Want To Access: \n");
scanf("%d", &choice);
switch (choice)
{
case 1:
AddEmployee();
break;
case 2:
break;
case 3:
break;
case 4:
PrintingAllEmployees(struct Employee EmployeeNumber[]);
break;
case 5:
return 0;
default:
printf("Sorry, Not Valid");
break;
}
}
return 0;
}
#include <stdio.h>
#include <string.h>
struct birthdate {
int month, day, year;
};
typedef struct {
char fatherName[30];
char motherName[30];
} parentage;
typedef struct demigod {
char firstName[50];
char lastName[30];
int age;
parentage parents;
struct birthdate birthday;
} halfblood;
void addDemigod(struct demigod *camphalfblood, int i) {
printf("Enter name: ");
scanf("%s %s", camphalfblood[i].firstName, camphalfblood[i].lastName);
printf("Enter birth month: ");
scanf("%d", &camphalfblood[i].birthday.month);
printf("Enter birth day: ");
scanf("%d", &camphalfblood[i].birthday.day);
printf("Enter birth year: ");
scanf("%d", &camphalfblood[i].birthday.year);
camphalfblood[i].age = 2020 - camphalfblood[i].birthday.year;
printf("Enter mother's name: ");
scanf("%s", camphalfblood[i].parents.motherName);
printf("Enter father's name: ");
scanf("%s", camphalfblood[i].parents.fatherName);
}
void printAll (struct demigod *camphalfblood, int index) {
for (int i = 0; i < index; i++) {
printf("\nNAME : %s %s\n", camphalfblood[i].firstName, camphalfblood[i].lastName);
printf("BIRTHDAY: %d-%d-%d\n", camphalfblood[i].birthday.month, camphalfblood[i].birthday.day, camphalfblood[i].birthday.year);
printf("AGE: %d\n", camphalfblood[i].age);
printf("PARENTS: %s and %s\n\n", camphalfblood[i].parents.motherName, camphalfblood[i].parents.fatherName);
}
}
void editDemigod(struct demigod *camphalfblood, int index) {
int i;
for (i = 0; i < index; i++) {
printf("[%d] %s %s\n", i, camphalfblood[i].firstName, camphalfblood[i].lastName);
}
int choice;
printf("Enter index: ");
scanf("%d", &choice);
printf("Enter new first name: ");
scanf("%s", camphalfblood[choice].firstName);
printf("Enter new last name: ");
scanf("%s", camphalfblood[choice].lastName);
}
int main() {
struct demigod camphalfblood[20];
int choice;
int index = 0;
while (1) {
printf("[1] Add Demigod\n");
printf("[2] Print All Demigods\n");
printf("[3] Edit Demigod\n");
printf("[4] Exit\n");
printf("Enter choice: ");
scanf("%d", choice);
if (choice == 1) {
addDemigod(camphalfblood, index);
index++;
printf("Successfully added!\n");
} else if (choice == 2) {
printAll(camphalfblood, index);
} else if (choice == 3) {
editDemigod(camphalfblood, index);
} else if (choice == 4) {
break;
}
}
return 0;
}
Here's my code. There's no error when I compile it but the program somehow stops when after I entered the choice. I don't know where the error is. It just don't run and I don't know why. please help me, I really dont know what happened.
this is the output that I keep seeing, the program just stops there suddenly.
EDIT: I already got it, ampersands made me dumb wtf HAAAHAHAH
The error is here: scanf("%d", choice); has undefined behavior because you pass the value of choice instead of its address. This probably causes an invalid memory access resulting in program termination.
The solution is: scanf("%d", &choice)
Note that you should also check the return value of scanf() to detect invalid or missing input.
You should also pass the maximum number of characters for %s to avoid undefined behavior on user input longer than expected.
Here is a modified version of addDemigod() using helper functions:
int flushInput(void) {
int c;
while ((c = getchar()) != EOF && c != '\n')
continue;
return c;
}
int invalidInput(void) {
int res = 1;
printf("invalid input\n");
if (flushInput() == EOF) {
printf("unexpected end of file\n");
return 2;
} else {
return 1;
}
}
int addDemigod(struct demigod *camphalfblood, int i) {
printf("Enter name: ");
if (scanf("%49s %29s", camphalfblood[i].firstName, camphalfblood[i].lastName) != 2)
return invalidInput();
flushInput();
printf("Enter birth month: ");
if (scanf("%d", &camphalfblood[i].birthday.month) != 1)
return invalidInput();
flushInput();
printf("Enter birth day: ");
if (scanf("%d", &camphalfblood[i].birthday.day) != 1)
return invalidInput();
flushInput();
printf("Enter birth year: ");
if (scanf("%d", &camphalfblood[i].birthday.year) != 1)
return invalidInput();
camphalfblood[i].age = 2020 - camphalfblood[i].birthday.year;
flushInput();
printf("Enter mother's name: ");
if (scanf("%29s", camphalfblood[i].parents.motherName) != 1)
return invalidInput();
flushInput();
printf("Enter father's name: ");
if (scanf("%29s", camphalfblood[i].parents.fatherName) != 1)
return invalidInput();
flushInput();
return 0;
}
Just as the title said, I tried to make a function that finds max/min grades from a list of grade structs by creating a new variables that stores highest/lowest grades and then compare it with each instance in an array of struct to find the desired result. However, my function does not work as intended and it returns both max grade for both function min and max
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
struct student{
char name[50], studentID[50];
float grade;
} std[30];
int n;
void add(){
for (int i=0; i<n; i++){
printf("\nAdd the students details no%d", i+1);
printf("\n**********************************");
printf("\nStudent's name: ");
scanf(" %s", &std[i].name);
getchar();
printf("\nStudent's ID: ");
scanf(" %s", &std[i].studentID);
getchar();
printf("\nStudent's grade: ");
scanf(" %f", &std[i].grade);
getchar();
system("cls");
}
}
void displayRecords(){
for (int i=0; i<n; ++i){
printf("\n**************************");
printf("\nStudent's name: %s", std[i].name);
printf("\nStudent's ID: %s", std[i].studentID);
printf("\nStudent's grade: %.2f", std[i].grade);
printf("\n**************************\n");
}
getch();
}
float max(){
float maxGrade = std[0].grade;
for (int i=0; i<n; i++){
if(std[i].grade > maxGrade);
maxGrade = std[i].grade;
}
printf("Max grade: %.2f", maxGrade);
getch();
return maxGrade;
}
float min(){
float minGrade = std[0].grade;
for (int i=0; i<n; i++){
if(std[i].grade < minGrade);
minGrade = std[i].grade;
}
printf("Min grade: %.2f", minGrade);
getch();
return minGrade;
}
int main() {
while(1){
int a;
system("cls");
printf("\t\t\t\t----------Welcome to SMS----------");
printf("\n\t\t\t\t\tPlease choose your action");
printf("\n\t\t\t\t1.Create new record");
printf("\n\t\t\t\t2.View a record");
printf("\n\t\t\t\t3.Find max grade");
printf("\n\t\t\t\t4.Find min grade");
printf("\n\t\t\t\t5.Exit");
printf("\n\n\n\t\t\t\tYour choice: ");
scanf("%d", &a);
switch(a){
case 1:
system("cls");
printf("Please enter the amount of records you want to add <1-30>: ");
scanf("%d", &n);
add(n);
break;
case 2:
system("cls");
displayRecords(n);
break;
case 3:
system("cls");
max(n);
break;
case 4:
system("cls");
min(n);
break;
case 5:
system("cls");
printf("\n\t\t\t\t\t\tSee you again next time!");
getch();
return 0;
default:
system("cls");
printf("Error! Please enter again");
}
}
}
OP's if() ends too early for min and max.
// Bad
if(std[i].grade < minGrade);
minGrade = std[i].grade;
// Should be
if(std[i].grade < minGrade)
minGrade = std[i].grade;
Look carefully.
Best to enable all compiler warnings to save you and us time.
This project is the skyline project for a school work, and at the moment I'm getting an error when I'm putting in the coordinates being that this error is 0xc0000005.
Everything else is running smoothly as for now if I'm not wrong.
If someone could explain to me why this is happening I would be grateful.
#include <stdlib.h>
typedef struct {
int registerednumber; // It registers the number of buildings used in the array of buildings;
} registernumber;
typedef struct
{
int li;//POINT MOST TO THE LEFT
int hi;//HEIGHT OF THE BUILDING
int ri;//POINT MOST TO THE RIGHT
} buildings;
void exitprogram(){
exit(0);
}
int menu() {
int optioninmenu=0;
do {
system("cls");
fflush(stdin);
printf("-------------SKYLINES------------- \n");
printf(" 1-REGISTER NUMBER OF BUILDINGS \n");
printf(" 2-REGISTER BUILDING COORDINATES \n");
printf(" 3-EXECUTE SKYLINE \n");
printf(" 4-CREDITS \n");
printf(" 5-EXIT \n \n");
printf(" TYPE YOUR OPTION: ");
scanf("%d", &optioninmenu);
} while(optioninmenu!= 1 && optioninmenu!= 2 && optioninmenu!= 3 && optioninmenu!= 4 && optioninmenu!= 5 && optioninmenu!=0);
return(optioninmenu);
}
int credits() {
int returntomenu = 0;
system("cls");
printf(" ################################ \n");
printf(" #This program was done by: #\n");
printf(" #Joao Moura A93099 #\n");
printf(" #Eva Castro A93097 # \n");
printf(" ################################ \n \n");
printf("\n TYPE ENTER TO RETURN TO THE MAIN MENU");
getchar();
return menu();
}
int countbuildings(buildings *building, int *regnumberofbuildings) {
int returntomenu = 0;
int i=0;
int numberofbuildings=0;
system("cls");
printf(" TYPE IN THE NUMBER OF BUILDINGS: ");
scanf("%d", &numberofbuildings);
*regnumberofbuildings=numberofbuildings;
printf("%d",*regnumberofbuildings);
printf("\n TYPE ENTER TO RETURN TO THE MAIN MENU");
getchar();
return menu();
}
int coordinates(buildings *building,int regnumberofbuildings) {
int i=0;
building=(buildings *)malloc(regnumberofbuildings*sizeof(building));
system("cls");
printf("%d",regnumberofbuildings);
for(i=0;i!=regnumberofbuildings;i++){
system("cls");
printf("Building number %d \n \n", i+1);
printf(" TYPE THE LEFTMOST COORDINATE: ");
scanf("%d", &building[i].li);
printf("\n TYPE IN THE HEIGHT OF THE BUILDING: ");
scanf("%d", &building[i].hi);
printf("\n TYPE THE RIGHTMOST COORDINATE: ");
scanf("%d", &building[i].ri);
}
printf("\n TYPE ENTER TO RETURN TO THE MAIN MENU");
getchar();
return menu();
}
int main()
{
int numberofbuildings=0;
int registerednumber=0;
buildings *building;
int option=0;
int i=0;
do{
option=menu();
if(option==1){
countbuildings(building,&numberofbuildings);
}
if(option==2){
coordinates(building,numberofbuildings);
getchar();
}
if(option==4){
credits();
}
}while(option!=5);
if(option==5){
printf("Thanks for using our program!");
printf("Press enter to leave the program");
getchar();
exitprogram();
}
return 0;
}
I've run this code countless times and I'm not sure what's wrong with it. It won't display the 'subj' variable whenever I run the code and choose the '1. Display' option. All I get is the error 'divide error' 'abnormal program termination '→ '
Here's my code:
#include <stdio.h>
#include <conio.h>
char subj[20], name[20], course[20], studentid[20];
float grade[20], hold, gu[20], guadd, gpa, temp, median, mode, max, a[20];
int z, i, h, n, tu, c, unit[20], count;
void disp(int n, int unit[], float grade[], char subj[], char name[], char studentid[], char course[])
{
printf("Name: %s\n", name);
printf("Course: %s\n", course);
printf("Student ID: %s\n", studentid);
printf("\n\nSubject\t\t\tUnits\t\t\tFinal Grade\n");
for(i=0; i<n; i++)
{
printf("%s", subj[i]);
printf("\t\t\t%d\t\t\t", unit[i]);
printf("\%.2f\n", grade[i]);
}
tu=0;
guadd=0;
for(i=0; i<n; i++)
{
gu[i]=grade[i]*unit[i];
tu+=unit[i];
guadd+=gu[i];
}
gpa=guadd/tu;
printf("\n\nSemester Grade Point Average:\t\t\t%.2f\n", gpa);
}
int main()
{
clrscr();
printf("Enter Student Name: ");
scanf ("%s", &name);
printf("Enter Student ID: ");
scanf ("%s", &studentid);
printf("Enter Course: ");
scanf ("%s", &course);
printf("Please Enter the Amount of Subjects Enrolled: ");
scanf ("%d", &n);
for(i=0; i<n; i++)
{
printf("\nPlease Enter the Subject Name: ");
scanf("%s", &subj[i]);
printf("Please Enter the Final Grade for this Subject: ");
scanf ("%f", &grade[i]);
printf("Please Enter Number of Units for this Subject: ");
scanf ("%d", &unit[i]);
}
clrscr();
do
{
printf("\nPlease Choose an Option:\n\n1. Display Grades with GPA\n2. Ascending Order of Grades\n3. Descending Order of Grades\n4. Median and Mode\n5.EXIT\n\nChoice: ");
scanf ("%d", &z);
clrscr();
switch(z)
{
case 1: disp(n, unit, grade, subj, name, studentid, course);
break;
case 2: ascending(n, grade);
break;
case 3: descending(n, grade);
break;
case 4: medmod(n, grade);
break;
}
}
while (z!=5);
getch();
return 0;
}
P.S. I'm new to this so I don't really know what the issue is. Thanks for the help!
Some changes I made to your code and it works.
Replace char subj[20] by char *subj[20] since you need an array of strings, not an array of characters which would be a single string. Then, allocate memory to each of them dynamically (used malloc from C's stdlib.h), say at-least for 100 chars.
Also, the gcc compiler generates warnings good enough to know what's going on, so it becomes quite clear to fix your arguments to printf and scanf functions.
PS : Since conio.h is non-portable, I removed it and its functions from the code.
Modified code :
#include <stdio.h>
#include <stdlib.h>
char *subj[20];
char name[20], course[20], studentid[20];
float grade[20], hold, gu[20], guadd, gpa, temp, median, mode, max, a[20];
int z, i, h, n, tu, c, unit[20], count;
void disp(int n, int unit[], float grade[], char *subj[], char name[],
char studentid[], char course[]) {
printf("Name: %s\n", name);
printf("Course: %s\n", course);
printf("Student ID: %s\n", studentid);
printf("\n\nSubject\t\t\tUnits\t\t\tFinal Grade\n");
for (i = 0; i < n; i++) {
printf("%s", subj[i]);
printf("\t\t\t%d\t\t\t", unit[i]);
printf("\%.2f\n", grade[i]);
}
tu = 0;
guadd = 0;
for (i = 0; i < n; i++) {
gu[i] = grade[i] * unit[i];
tu += unit[i];
guadd += gu[i];
}
gpa = guadd / tu;
printf("\n\nSemester Grade Point Average:\t\t\t%.2f\n", gpa);
}
int main() {
for (i = 0; i < 20; i++)
subj[i] = malloc(sizeof(char) * 100);
printf("Enter Student Name: ");
scanf("%s", name);
printf("Enter Student ID: ");
scanf("%s", studentid);
printf("Enter Course: ");
scanf("%s", course);
printf("Please Enter the Amount of Subjects Enrolled: ");
scanf("%d", &n);
printf("%s %s %s %d", name, studentid, course, n);
for (i = 0; i < n; i++) {
printf("\nPlease Enter the Subject Name: ");
scanf("%s", subj[i]);
printf("%s", subj[i]);
printf("Please Enter the Final Grade for this Subject: ");
scanf("%f", &grade[i]);
printf("Please Enter Number of Units for this Subject: ");
scanf("%d", &unit[i]);
}
do {
printf("\nPlease Choose an Option:\n\n1. Display Grades with GPA\n2. "
"Ascending Order of Grades\n3. Descending Order of Grades\n4. "
"Median and Mode\n5.EXIT\n\nChoice: ");
scanf("%d", &z);
switch (z) {
case 1:
disp(n, unit, grade, subj, name, studentid, course);
break;
case 2:
break;
case 3:
descending(n, grade);
break;
case 4:
medmod(n, grade);
break;
}
} while (z != 5);
return 0;
}