The following program is supposed to ask the user how many students he wants to grade then have the user input the grades and get the average.
#include <stdio.h>
#include <stdlib.h>
struct person {
char Name[100];
int ID;
int Age;
double Score;
};
int main(void) {
setvbuf(stdout, NULL, _IONBF, 0);
int size;
printf("How many students are you grading?(Not more than 100)-->");
scanf("%d",&size);
struct person *student;
double total=0,i=0, average;
student= malloc(sizeof(struct person));
//struct person student;
printf("Enter the following information:\n");
while (i<size) {
printf("%lf\n%lf\n",total,i);
printf("\n");
printf("Name:");
scanf("%s",student->Name);
printf("ID:");
scanf("%d",&student->ID);
printf("Age:");
scanf("%d",&student->Age);
printf("Score:");
scanf("%lf",&student->Score);
total = total + student->Score;
i++;
}
The code works fine until it needs to print out the average based on user input, at which point the program terminates without giving the average nor an error message.
if(size==1){
average = total / size;
printf("Class average is %0.2lf\n", average);
}
else
{
average=total/i;
printf("Class average is %0.2lf\n", average);
}
return 0;
}
Most console programs are intended to be run from some kind of console host like windows cmd, where the output stays there and it would be annoying for user to do some additional work to end the program so they can run another command, but since you are just testing it, you should put some code at the end to wait for some user input, now there are many ways to do that like: getch(), getchar(), scanf("%*c"), system("pause") so you can try those, but some compilers do this automatically in debug mode, sometimes you just need to specify the behaviour in your IDE settings.
I think the problem you are having is that the console is closing before you can read the average.
You could add:
getchar();
Before you return 0; in int main.
Related
i wanna make a fuction to print all date in struct array after the user press certain key(1 in that case) and stop the loop, and if he press 2 the loop continue until the array get full or the user press 1
#include <stdio.h>
#include <string.h >
struct dat {
int age;
char name[50];
int score;
int trab[2];
};
int main(void)
{
int x = 0;
struct dat people[20];
for(int i = 0; i < 20; i++)
{
gets(people[i].name);
scanf("%d", &people[i]age);
scanf("%d", &people[i].score );
scanf("%d", &people[i].trab[0]);
scanf("%d", &people[i].trab[1]);
scanf("%d", x);
switch(x)
{
case 1:
break;
case 2:
continue;
}
}
imp(people[i]);
return 0;
}
int imp(struct dat people[i])
{
int i;
printf("%s", people[0].name);
printf("%d", &people[0].age);
printf("%d", &people[0].score );
printf("%d", &people[0].trab[0]);
printf("%d", &people[0].trab[1]);
return 0;
}
Your code cannot compile in this state.
Your compiler should tell you why some line do not compile, first try to correct errors.
Once errors are corrected, turn on compiler warning, and handle them.
The line
#include <string.h >
Will raise this error: fatal error: string.h : No such file or directory
Why a space between h and > ?
The function gets should not be used: from man gets
Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.
So
gets(people[i].name);
should be
fgets(stdin, people[i].name, sizeof people[i].name);
The following line is missing a dot .
scanf("%d", &people[i]age);
Since x is 0, this next line dereference the NULL pointer (which you don't want):
scanf("%d", x);
You should write:
scanf("%d", &x);
Then you call imp function on people[i], but imp is not declared, and i is not defined (it's a variable local to for loop)
imp(people[i]);
The imp definition is not valid:
int imp(struct dat people[i])
Should be something like:
/* function to display ONE person */
int imp(struct dat people)
or
/* function to display ALL peopel */
int imp(struct dat *people, int number_of_people)
I have a question that is needed to ask. Hope someone can help me out.
I am writing a function that prompts users for names, hours worked, and hourly rate using parameter passing, and pass by reference. If the user input "-1" in any field, break out of the function.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct userdata
{
char name[20];
float hours;
float rate;
float gross;
float base;
float overtime;
float taxes;
float net;
}data;
int LoadEmployeeFromKey(data *d)
{
char name[20];
float rate, hours;
printf("Enter an employee name: ");
scanf_s("%s", name, 20);
if (strcmp(name, "-1") == 0)
return;
printf("Worked Hours: ");
scanf_s("%f", &hours);
if (hours == -1.0f)
return 1;
printf("Hourly rate: ");
scanf_s("%f", &rate);
if (rate == -1.0f)
return 1;
strcpy_s(d->name, 20, name);
d->hours = hours;
d->rate = rate;
return 1;
}
There is nothing wrong in here, this function works well. But the real problem when I work in main().
void main()
{
data employee[10];
int count = 0;
int option;
int stop = 0;
int i = 0;
FILE EmployeeFromFile[20];
FILE *File;
while (!stop)
{
puts("---Main Menu---\n");
puts("CHOOSE A FOLLOWING OPTION:");
puts("1. Add an employee info from keyboard.");
puts("2. Add employees info from a text file.");
puts("3. Print all employees info.");
puts("4. Edit an employee info.");
puts("5. Print an employee and salary.");
puts("6. Print all employees and salary.");
puts("7. Save and Quit the program.\n");
scanf_s("%d", &option);
switch (option)
{
case 1:
LoadEmployeeFromKey(&employee[count]);
Calculation(&employee[count]);
count++;
break;
Please ignore other things, when I debug, if I input "-1" in any field of (name, hours, or rate), it breaks and goes back to "Main Menu"; however, "count" is still counting (it still plus 1) leading to error output when I choose 3 -> 6. It may be a stupid question but to be honest I cannot find out where or what should I type to search to solve this problem. I am okay to see downvote but please help.
Thank you.
Your function LoadEmployeeFromKey is declared to return a value, and most of your return statements do return a value.
The problem is two-fold: First is that the function (except in one case) always return the same value, making it impossible to distinguish between the cases; The second problem is that you don't check what the function return, so it's not possible to add conditions based on that.
First of all make sure all return statements actually return a value (otherwise you will have undefined behavior). Then return one value if the input was "-1", and some other value if you fully initialized the structure. Lastly, use the returned value to check if you should increase the counter or not.
First of all, you must (and probably already) have a proper function prototype:
int LoadEmployeeFromKey(data *d);
Secondly, you have used function which returns a value before?
Lets say that the function return 1 on success, when all fields have been properly entered and initialized, and 0 otherwise, then you could use it directly in a condition:
if (LoadEmployeeFromKey(&employee[count]) == 1)
{
// All data valid
Calculation(&employee[count]);
count++;
}
If the function doesn't return 1 then just don't do anything special.
I am having a lot of trouble wrapping my head around calling functions and using them in the main program again later. I have not found an answer in depth to explain why this doesn't run. I understand that parameters belong inside of the called function parentheses, but I want the user input to begin in the called program. Is this even possible? Theoretically, the function would ask the user for a year, check that it is within certain parameters, then return it to the main function where I would like to eventually be able to store it in an array. For now, can someone please show me how I would make that work in this elementary program? Thank you in advance!
#include <stdio.h>
char year_info();
int main(void)
{
int menu_selection;
char year;
printf("Please choose from the following menu: \n1. Insert a new movie\n2. Show movie\n3. List all\n4. Exit\n");
scanf("%i", &menu_selection);
switch (menu_selection)
{
case 1: year = year_info();
printf("%c", year);
break;
}
}
char year_info()
{
int year_input;
printf("\nYear: ");
scanf("%i", &year_input);
if (year_input > 2016 || year_input < 1920)
{
printf("Sorry, I do not recognize this command. Please try again.\n");
}
else
{
int year = year_input;
return year;
}
}
It doesn't run because you're passing scanf the variable, but you should pass the address of the variable, i.e. use:
scanf("%i", &something);
instead of scanf("%i", something);
Also, as others pointed out, you're mixing char and int too liberally, so it won't work as expected.
year and year_imput can't be chars because they won't hold values large enough, you'll need at least a short.
You had 2 errors.
scanf("%i", &menu_selection);
scanf("%i", &year_imput);
You need to use the & to pass the address of the variables to scanf().
Edit: However, I would have used an integer for that, because a scanf("%c", &something) will only recognize the first char you enter, and not the whole string, even if that happened you can't do if (year_imput > 2016 || year_imput < 1920) between strings, you can do that with chars, but again, they can only store one character, so I would have done your program like this.
#include <stdio.h>
int year_info();
int main() {
int menu_selection;
int year;
printf("Please choose from the following menu: \n1. Insert a new movie\n2. Show movie\n3. List all\n4. Exit\n");
scanf("%i", &menu_selection);
switch (menu_selection) {
case 1:
year = year_info();
printf("%i", year);
break;
default:
break;
}
return 0;
}
int year_info() {
int year_imput;
printf("\nYear: ");
scanf("%i", &year_imput);
if (year_imput > 2016 || year_imput < 1920) {
printf("Sorry, I do not recognize this command. Please try again.\n");
return 0;
}
else
return year_imput;
}
For my assignment I am to create a structure that allows the user to enter student info (ID, DOB, & Phone number). I have no problem doing this that is quite simple. Now I need to search through that enter info using the student ID to display that students corresponding DOB and phone number, this is the problem that I am having trouble working with. If you see any other problems with my program please let me know what is wrong and why I should change so I can learn from my mistakes.
Thank you.
#include <stdio.h>
#include <stdlib.h>
struct infoStruct
{
int studentID;
int year;
int month;
int day;
int phone;
int end;
};
int main (void)
{
int students = 0;
int infoArray [students];
struct infoStruct info;
int studentID;
int year;
int month;
int day;
int phone;
int end;
while (info.end != -1) {
students = students + 1;
printf("Enter student information (ID, day, month, year, phone)\n");
printf("Enter -1 following the phone number to end the process to continue enter 0\n");
scanf("%d %d %d %d %d %d", &info.studentID, &info.day, &info.month, &info.year, &info.phone, &info.end);
}
if (info.end = -1){
printf("You entered %d student(s)\n", students);
}
//Student Search
printf("Please enter the student ID of the student your looking for\n.");
scanf("%d", info.studentID);
printf(" DOB: %d %d %d, Phone: %d", info.month, info.day, info.year, info.phone);
}
info.end is not initialized before while (info.end != -1). Initiliaze all your variable (studentID...) and structure.
if (info.end = -1) is an assignment !
Use : if (info.end == -1) I prefer to use if (-1 == info.end) (if you had use : only = instead of == you would have get an error). (Yoda trick ^^)
And you have to use an array of struct in order to save every student (because you are continuously erasing the previous student information).
It's your homework, I won't do the work for you ;)
I'll leave most of the coding to you, as this is homework, but here is what you need to change to get this to work.
First of all, if you want to store multiple students info is going to need to be an array
static int MAX_STUDENTS = 50;
struct infoStruct info[MAX_STUDENTS];
and then you scan each student into a seperate part of the struct
scanf("%d %d %d %d %d %d", &info[students-1].studentID, &info[students-1].day, &info[students-1].month, &info[students-1].year, &info[students-1].phone, &info[students-1].end);
then you need to ensure that the end condition is checked properly (check the latest info[x].end)
It would also be wise to check you still have some room in the array before trying to add more.
with those done you are storing the students correctly.
as for searching, you need to scan the id to search into a seperate int.
then loop through the array (info[x]) and search each element's studentID against the search ID. When you have a match, print it out.
Edit:
its also worth considering storing the phone number as a "string" rather than an int, alot of phone numbers start with "0", but an int would remove the 0.
(so the phone number 012345, would become, 12345)
I am writing a program to create a structure named 'student'. I need to input various data about a particular student. Here is my program till now.
#include<stdio.h>
#include<stdlib.h>
struct student
{
char* name;
int id;
float marks_1;
float marks_2;
};
void main()
{
int num, var_i, var_j, var_k, var_l, duplicated_id = 0;
printf("Enter number of students\n");
scanf("%d", &num);
struct student s[num];
printf("Enter the data for the students\n");
for (var_i = 0; var_i < num; var_i++)
{
var_j = var_i + 1;
printf("Enter name of student_%d\n", var_j);
scanf(" %[^\n]%*c", &s[var_i].name);
printf("Enter id of student_%d\n", var_j);
scanf("%d", &s[var_i].id);
for (var_k = 0; var_k < var_i; var_k++)
{
if (s[var_k].id == s[var_i].id)
{
printf("Duplicate Id, program will exit");
return;
}
}
printf("Enter marks(sub_1) of student_%d\n", var_j);
scanf("%d", &s[var_i].marks_1);
printf("Enter marks(sub_2) of student_%d\n", var_j);
scanf("%d", &s[var_i].marks_2);
}
}
In the following for loop I am checking all the previously entered 'id' values to check if there is a duplicate. In case of a duplicate, the program will exit.
for(var_k=0;var_k<var_i;var_k++)
{
if(s[var_k].id==s[var_i].id)
{
printf("Duplicate Id, program will exit");
return;
}
}
Now instead of exiting the program I want to prompt the user to enter a different value. This goes on till he enters a unique value. How should I do it?
Any help appreciated.
This is wrong:
scanf(" %[^\n]%*c", &s[var_i].name);
You're passing the address of the pointer member name (i.e. you're passing a char **) to scanf() which per the format string, is expecting a char* and enough memory to hold the data it subsequently reads. This is invalid, is undefined behavior, and blindly overwrites data in the s[] array. Frankly I'm amazed this doesn't seg-fault your process.
Change this:
struct student
{
char* name;
int id;
float marks_1;
float marks_2;
};
To this:
struct student
{
char name[128]; // or some other suitable size.
int id;
float marks_1;
float marks_2;
};
And change this:
scanf(" %[^\n]%*c", &s[var_i].name);
To this:
scanf(" %[^\n]%*c", s[var_i].name);
I strongly suggest a size-limiter on that scanf() call as well, but I leave that to you to discover. Read about the API here.
Just use a loop.
here is some psudocode
bool isDuplicate = false
do
{
GetInput()
isDuplicate = CheckForDuplicate()
}while(isDuplicate);