Reputation: 29
I have to make program which requires user to:
enter surname (17 letters max)
enter name (11 letters max)
enter subject (20 letters max)
enter number of points (number between 0 and 50)
Entering need to repeat and ends afer you write END as a surname, and then close txt file
After that, I have to write function that opens txt file, reads it and print all data from it in this format:
surname|name|subject|points
-----------------------------
-----------------------------
doe|jane|math|36
doe|john|programming|45
In same function I have to find which entry have lowest number of points, and then print it (if there are two with same number of points then print last one that is found). After that, in the same function I have to calculate how big file is and print it in bytes.
I managed to write code to enter name and write it into the txt file, but I can't make the rest of the program, anyone have an idea how to?
This is my code:
#include <stdio.h>
int main()
{
FILE *inputfile = NULL;
FILE *outfile = NULL;
struct imenik
{
char surname[17 + 1];
char name[11 + 1];
char subject[20 + 1];
int points;
} imen;
outfile = fopen("new.txt", "w");
printf("Enter surname: ");
scanf("%s ", imen.surname);
printf("Enter name: ");
scanf("%s ", imen.name);
printf("Enter course: ");
scanf("%s ", imen.subject);
printf("Enter points (0 - 50): ");
scanf("%d ", &imen.points);
printf("\n %s|%s|%s|%d", imen.name, imen.surname, imen.subject, imen.points);
fprintf(outfile, "\n %s|%s|%s|%d", imen.name, imen.surname, imen.subject, imen.points);
fclose(outfile);
return 0;
}
Upvotes: 0
Views: 106
Reputation: 3142
fopen
. If it couldn't open the file, you should stop the function there.scanf_s
or fgets
instead.int
s,.
//the array
int arr[] = {3,1,4,2,3,0};
//accumulators
int min_value = INT_MAX; /* INT_MAX defined in "limits.h" */
int min_index = -1;
//steps
for( int i = 0; i < 6; ++i ) {
if( arr[i] < min_value ) {
min_value = arr[i];
min_index = i;
}
}
do{...}while(...);
loop, and use strncmp
to compare C strings (it returns 0
if they are the same).Upvotes: 1