I am given an exercise that I can't seem to understand. I am almost done with my assignment but I'm stuck on this function.
Limitations:
There can only be 10 unique student ID's.
There are 5 subject area of study.
An a student can only take 2 subjects.
My struct.h look like this:
typedef struct student_info{
int student_id;
int course_id[2];
}student;
In main.c
student info[10];
In func.c
Say I Prompt the user for a Student ID.
printf("Enter Student ID. ");
scanf("%d", &info->student[count_stud]->student_id;
User inputs 123
Then Prompt the user for a course ID.
printf("Enter Course ID. ");
scanf("%d", &info->student->course_id[count_cour];
User inputs 101
My problem lays with printing out a specific student_id and the course that student is taking. Also using a for loop I couldn't find a way to find a duplicate. I can find an id that was last inputted by the user but when I enter an id from 2 previous inputs it passes my if else statements.
Any help is appreciated.
student info[10];
Here, info is array of 10 students so you will have to read it with index.
for(int student_count = 0; student_count < 10; student_count ++)
{
printf("Enter Course ID 1 for student %d. ",student_count+1);
scanf("%d", &info[student_count].course_id[0]);
printf("Enter Course ID 2 for student %d. ",student_count+1);
scanf("%d", &info[student_count].course_id[1]);
}
Related
I am currently trying to create a method to populate student with data, while having it inside an array of student, in order to be able to process its data with ease later on.
I have created two structs:
typedef struct{
char email[100];
char name[100];
char age[100];
char score[100];
}student;
typedef struct{
student *array;
}student_Array;
Then, within main(), I would declare them to be populated right after, using the Student Score Data.txt:
|NAME|E-MAIL|AGE|SCORE
|James|James#gmail.com|20 Years Old|20/50
|Charlie|Charls#gmail.com|21 Years Old|23/50
|Parker|prak#gmail.com|22 Years Old|26/50
int main(){
char fileName[100];
printf("Please enter file name:\n");
scanf("%[^\n]",fileName);
char *contents = readFileRaw(fileName); //Student Score Data.txt
student_Array students;
students.array = malloc(sizeof(student) * 100);
if(contents != NULL)
Fill_Contents(contents, students); //Method to fill the structs using the text file.
else
printf("its empty\n");
free(contents)
...
...
}
However, after at some point, I have noticed that specifically one of the parameters, students.array[0].name was populated with a part of the text file when I have not.
Note that all the other parameters of the same index, email, age and score was behaving normally, and so did all of the parameters on greater indexes.
Here is what the terminal printed after it has assigned its values:
STUDENT 0 INFORMATION:
student 0's age is 20 Years Old
student 0's name is Old|23/50
|Parker|prak#gmail.com|35 Years Old|26/50/50James
student 0's email is └James#gmail.com
student 0's score is 20/50
STUDENT 1 INFORMATION:
student 1's age is 21 Years Old
student 1's name is Charlie
student 1's email is Charls#gmail.com
student 1's score is 23/50
STUDENT 2 INFORMATION:
student 2's age is 35 Years Old
student 2's name is Parker
student 2's email is prak#gmail.com
student 2's score is 26/50
At first, I assumed it had to do with the Fill_Contents() method that I had created has some error, but it became clear that it was not the case.
By removing from main() the above mentioned method, it printed this:
STUDENT 0 INFORMATION:
student 0's age is
student 0's name is Old|23/50
|Parker|prak#gmail.com|35 Years Old|26/50/50
student 0's email is └
student 0's score is
STUDENT 1 INFORMATION:
student 1's age is
student 1's name is
student 1's email is
student 1's score is
STUDENT 2 INFORMATION:
student 2's age is
student 2's name is
student 2's email is
student 2's score is
It has now suddenly appeared as if regardless of being assigned or not using the Fill_Contents(), it would have been already populated with Old|23/50|Parker|prak#gmail.com|35 Years Old|26/50/50 for unknown reason.
My only suspect now is at the students.array = malloc(sizeof(student) * 100);, where it could have been something related to the memory, but I have no idea how to move on from here.
I hope this has been clear, and I hope someone could help me with this.
I'm new to c, so apologies if I missed out on something.
This is in C.
Im trying to create a function where you enter basic information and it assigns you an account number that can be used throughout.
When running the program it always out puts the same number. I dont need it to store outside of the program but the function and program are supposed to be run multiple times. I need this portion of the code to work because there is a later part where I have to delete the information entered and the way I thought best would be to assign the information to a value like account number and then use that to delete everything.
Sample output: Welcome Test Test to Global Bank. Your account number is 0 your initial balance in your checking is 100 and your initial balance in your savings is 1000.
What im trying to accomplish is have the account number in the sample output be 1 then when I run the NewAccount function again it should output:
Welcome Test2 Test2 to Global Bank. Your account number is 2 your initial balance in your checking is 100 and your initial balance in your savings is 1000.
This is the code. I have tried giving customers, of struct Bank a value of 20 (customers[20]) but that didnt seem to help. This is where im stuck at.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Customer
{
char FirstName[100];
char LastName[100];
int CheckingBalance;
int SavingsBalance;
int MoneyMarketBalance;
unsigned AccountNumber;
};
struct Customer NewCustomer(void)
{
static unsigned TopAccountNumber = 0;
struct Customer customer;
char FirstName[100];
char LastName[100];
int CheckingBalance;
int SavingsBalance;
int MoneyMarketBalance;
unsigned AccountNumber;
TopAccountNumber++;
customer.AccountNumber = TopAccountNumber;
return customer;
}
void NewAccount()
{
struct Customer customer;
printf("\nFirst name\n");
scanf("%s", customer.FirstName);
printf("\nLast name\n");
scanf("%s", customer.LastName);
printf("\nChecking deposit\n");
scanf("%d", &customer.CheckingBalance);
printf("\nSavings deposit\n");
scanf("%d", &customer.SavingsBalance);
printf("\nWelcome %s %s to Global Bank. Your account number is %d your initial balance in your checking is %d and your initial balance in your savings is %d.", customer.FirstName, customer.LastName, customer.AccountNumber, customer.CheckingBalance, customer.SavingsBalance);
}
int main()
{
int choice;
printf("\n1 is the problem function. After you enter your inital values run 1 again and run a second set of values \n to exit press '7'.\n");
while(1){
scanf("%d", &choice);
switch(choice)
{
case 1:
NewAccount();
break;
case 7:
printf("Have a good day and thank you for banking with Global Bank!\n");
exit(0);
}
}
}
A "bank" have customers, but isn't a customer in itself. So the Bank structure is rather badly named. And if one customer can only have a single account-number, then make it a part of the "customer" structure as well.
As for giving each customer a unique account number, one simple way is to keep track of the current highest number using a variable. Then when you create a new customer you increase that variable by one, and assign it to the customers specific account number.
For example:
struct Customer
{
// Other customer data...
unsigned AccountNumber;
};
struct Customer NewCustomer(void)
{
// This is the base for all account numbers
static unsigned TopAccountNumber = 0;
struct Customer customer;
// Initialize other customer data...
++TopAccountNumber; // Increase top account number
customer.AccountNumber = TopAccountNumber;
return customer;
}
So the question should be rather simple (not to me tho for some reason).
i need to give some employees some IDs and give those IDs their own salary.and allow said employee to be stored IF their salary is above a set point. so far I've been able to (brokenly) implement this (i apologize if it seems like i'm talking a lot it's just that i can't seem to explain my problem that easily)
OLD code
#include <stdio.h>
void Employee_Data(int number_of_employees,
float Data[3][number_of_employees],float salary){
int i ;
for(i = 0 ;i < number_of_employees;i++){
printf("Employee %d\n\n",i+1);
printf("enter the salary of employee %d:",i+1);
scanf("%f",&salary);
printf("\n");
}
}
int main() {
printf("Enter the number of employees: ");
int number_of_employees = 0;
scanf("%d",&number_of_employees);
float Data[3][number_of_employees];
float salary;
Employee_Data(number_of_employees,Data,salary);
return 0;
}
what i need is a way to set an ID that the user inputs in the above function
and another function to print out the employees and their IDs if they have a salary over a number that the user also inputs and lastly (most likely the hardest) a function to give a raise to employees which are picked by their IDs and said raise is to be percentage wise of their current salary
Once again i apologize if it's too long and too confusing i have a bit of problems trying to get my point to be understood hopefully it is though.
NEW CODE
#include <stdio.h>
float employee_ID(int number_of_employees,int ID[number_of_employees]){
for (int i = 0; i<1;i++){
scanf("%d",&ID[i]);
}
return *ID;}
float Salary_of_employees(int number_of_employees,int salary[number_of_employees]){
for(int i = 0; i<1;i++){
scanf("%d",&salary[i]);
}
return *salary;}
void printing(int number_of_employees,int *salary[number_of_employees], int *ID[number_of_employees]){
for(int k = 0; k<number_of_employees;k++){
printf("Employee %d 's ID is %d and their salary is %d\n",k+1,*ID[number_of_employees],*salary[number_of_employees]);
}
}
int main() {
int number_of_employees;
int ID[number_of_employees];
int salary[number_of_employees];
printf("Please input the number of employees you need : ");
number_of_employees = 0;
scanf("%d",&number_of_employees);
int i;
for (int i = 0; i<number_of_employees;i++){
printf("please input the ID for employee %d\n",i+1);
employee_ID(number_of_employees,&ID[number_of_employees]);
printf("please input the salary of employee %d\n",i+1);
Salary_of_employees(number_of_employees,&salary[number_of_employees]);
}
printf("if you want to see the IDs and the salaries of the employees please write 1\n if you want to ");
int choice;
scanf("%d",&choice);
if (choice == 1){
printing(number_of_employees,salary,ID);
}
return 0;
}
Currently my problem with this is that i've got 2 warnings at the calling of the printing function (salary and ID are the warnings for some reason can't get them to be fixed)
And my other problem is that the storing only takes the last inputted number given by the user. for example if i give an ID of 321 and a salary of 543 the output of the printing function would be employee number(thank god the counter works)'s ID is 543 and the salary is 543 (TO EACH EMPLOYEE)
Edit1: So basically when i run the code it asks me to input the number of employees. (so far so good) when i insert the number it starts with employee number 1 and asks me to input the salary. upon inputting the salary it just gives me 2 new lines and stops basically what i need is that each time it'd -instead of saying employee 1- it'd first ask me to set the employee ID first and then say "Employee -insert ID-'s salary: "and then asks me to insert the salary of said employee. and then the same for each employee that i set . + the other functions
Edit2: Updated the code by removing the for loop that seemingly crashed the program and updated the title + (i think) explained the stuff that i need better
Edit 3: after messing around and legit deciding to restart the code i began experiencing some uhhh trouble with the storing for some reason. and decided to add the new code and keep the old one as well
You have an inner and outer loop over the number of employees. Not only will this behave strangely, but Its likely that if you enter more than 3 employees the code will crash. The J loop doesn't look right.
I'm learning Structures in C, and trying to write the solution for this problem-
Create a structure to specify data on students given below: Roll
number, Name, Department, Course, Year of joining .
Assume that there are not more than 5 students in the class. (a) Write a function to
print names of all students who joined in a particular year. (b) Write
a function to print the data of a student whose roll number is given.
My code is given next, with a brief description of various components in the program, and then the source of the problem -
#include<stdio.h>
struct student
{
int roll_no;
char name[20];
char department[30];
char course[20];
int year_joined;
};
void main()
{
struct student s[5];
int i=0,choice;
printf("\n Enter the following information for each student: \n");
printf("\n Roll No., Name, Department, Course, Year of Joining\n");
for(i=0;i<=4;i++)
scanf("%d %s %s %s %d", &s[i].roll_no, &s[i].name, &s[i].department, &s[i].course, &s[i].year_joined);
printf("\n Please choose from the following options :\n");
printf("\n 1. Print names of students who joined in a given year.");
printf("\n 2. Print data of all students whose roll.no is given.");
scanf("%d",&choice);
switch(choice)
{
case 1: display_names(s);
break;
case 2: student_data(s);
break;
default: printf("\n Incorrect choice, please try again.");
}
}
void display_names(struct student p[])
{
int i,count=0;
int year;
printf("\n Enter the year you wish to search student info. for : \n");
scanf("%d",&year);
for(i=0;i<=4;i++)
{
if(p[i].year_joined==year)
{
//printf("%d %s %s %s %d", p[i].roll_no, p[i].name, p[i].department, p[i].course, p[i]. year_joined);
count++;
printf("\n Student Name: %s\n", p[i].name);
}
}
printf("\n Total Number of students who joined in the year %d is %d\n", year,count);
if(count==0)
printf("\n No match found.");
}
void student_data(struct student st[])
{
int i,count=0;
int roll;
printf("\n Enter roll number of the student: \n");
scanf("%d",&roll);
for(i=0;i<=4;i++)
{
if(st[i].roll_no=roll)
{
count++;
printf("\n Student Data: Roll No. Name Department Course Year of Joining \n");
printf("\n %d %s %s %s %d", st[i].roll_no, st[i].name, st[i].department, st[i].course,st[i].year_joined);
break;
}
}
if(count==0)
printf("\n No matching Roll Numbers found\n");
}
Here's a brief description of this program-
Since I'm required to store information about multiple students in the structure, I have created a structure array. The structure struct student is defined at the very beginning, and the array is created inside main- struct student s[5]
The structure is initialized by accepting user input through scanf() inside main(). There are two switch cases for the two functions, asked in the question- display_names(s) and student_data(s)
The display_names(s) function is called in main, and is defined outside it. The function accepts a structure array and asks the user to enter a year. It loops through the initialized array and searches for the input(year of joining) entered by the user. When there's a match, it prints out the names of all students who've joined in that year. Here's a sample output for this function-
*
**Enter the following information for each student:
Roll No., Name, Department, Course, Year of Joining
10 Tim CS MS 2013
20 Shane CS MS 2013
30 John EE ME 2013
40 Mark MECH MS 2013
50 Matt EE ME 2010
Please choose from the following options :
1. Print names of students who joined in a given year.
2. Print data of all students whose roll.no is given.1
Enter the year you wish to search student info. for :
2013
Student Name: Tim
Student Name: Shane
Student Name: John
Student Name: Mark
Total Number of students who joined in the year 2013 is 4**
*
This function works fine.
The problem is in the next function student_data(). Similar to the first function, it accepts a structure array as input. The way I've designed the function- the idea is to loop through the array until it finds a roll number that's equal to the one entered by the user. When there's a match, it would print out the details of that particular student itself. However, as it stands, this function always prints the first entry in the array, and I can't seem to understand why. I've added the break in the if statement, but it doesn't seem to make any difference.
Here's a sample output-
Enter the following information for each student:
Roll No., Name, Department, Course, Year of Joining
10 Tim CS MS 2013
20 Shane CS MS 2013
30 John EE ME 2013
40 Mark MECH MS 2013
50 Matt EE ME 2010
Please choose from the following options :
1. Print names of students who joined in a given year.
2. Print data of all students whose roll.no is given.2
Enter roll number of the student:
40
Student Data: Roll No. Name Department Course Year of Joining
40 Tim CS MS 2013
Can you please say where I'm going wrong in this second function?
Your if statement within student_data() is assigning rather than comparing.
Consider compiling with the -Wall ( or specifically -Wparentheses ) flag:
#include <stdio.h>
int main( void )
{
int a = 10;
if ( a = 11 ) printf( "a equals 11\n" );
return 0;
}
Using -Wall:
$ gcc main.c -Wall
warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if ( a = 11 ) printf( "a equals 11\n" );
$
I am not allowed to use arrays. I need to list all of the course codes, days, and times for as many courses as the user has specified in the first scanf_s call. I have no idea how to proceed without using arrays. Any help hint/help would be appreciated.
printf("Please enter the number of courses you'd like to take: ");
int numOfCourses;
scanf_s("%d", &numOfCourses);
int courseCode;
int courseDay;
int courseTime;
int i = 0;
while (i < numOfCourses) {
printf("Please enter the code of the course: ");
scanf_s("%d", &courseCode);
printf("Please enter the day of the course: ");
scanf_s("%d", &courseDay);
printf("Please enter the time of the course: ");
scanf_s("%d", &courseTime);
i++;
}
Use dynamic memory allocation in a linked list. Look at malloc. Your list structure could look like:
typedef struct COURSES {
int courseCode;
int courseDay;
int courseTime;
struct COURSES *next;
} t_Courses;
You allocate a list element as follows:
t_Courses *pCourse= malloc(sizeof(t_Courses));
and then read the data as you do now, e.g.:
scanf_s("%d", &pCourse->courseCode);
Managing a linked list is not simple. I leave it to you as part of the homework. There are numerous examples on the internet and on Stack Exchange.