C (Basic stuff) - Write User Input to a File

user6904864

Basically, I want to have the user input four names and four grades into a text file (2 columns); just for example:

Mary 98

Mike 60

John 78

Beth 89

I want to be able to read the file (presumably with fscanf) and display the highest/lowest grades, and the average of them. So I would need to create variables for all the inputs, right? In class today, we learned about structures, so I'm assuming one might need to be implemented. I have no actually useful code, but here's the start of it:

.
.
.

int main()
{

int i;

FILE *Fpointer;
char input[100];
int input2[4];


Fpointer = fopen("file.txt", "w");

for(i = 0; i < 4; i++)
{
    printf("Enter a name>");
    scanf("%s", input);
    printf("Enter a grade>");
    // User Josh Bressers told me to change the below %d to %s
    scanf("%s", input2);
    // And the second %d to %s here
    // The program prints 4 names and 4 numbers perfectly now
    fprintf(Fpointer, "%s %s", input, input2);
    fprintf(Fpointer, "\n");

}

fclose(Fpointer);

return 0;


}

Now that the program takes the user's input of four names and numbers, how would I read the numbers and do the calculations listed in the first paragraph? Any help is appreciated!

Thanks,

TP

EDIT: GOT IT MYSELF (UGLY, BUT IT WORKS)

user6904864

I got it myself...

#include <stdio.h>
#include <stdlib.h>


// Name of file to be created and data saved in
#define FILENAME "students_and_grades.txt"
// Size of arrays for names
#define NAME_ARRAY_SIZE 25


// Declare arrays
char arrcName1[NAME_ARRAY_SIZE];
char arrcName2[NAME_ARRAY_SIZE];
char arrcName3[NAME_ARRAY_SIZE];
char arrcName4[NAME_ARRAY_SIZE];
char arrcHighestName[NAME_ARRAY_SIZE];
char arrcLowestName[NAME_ARRAY_SIZE];


int main()
{
printf("----Tyler Procko----\n\n\n");

//Declare
int iScore1, iScore2, iScore3, iScore4;
int iHighestScore, iLowestScore;
double dScoreAverage;

// Declare file pointer
FILE *Fpointer;

// Open file for writing
Fpointer = fopen(FILENAME, "w");


// Ask user for first student name and score
printf("What is the first student's name?   ");
scanf("%s", arrcName1);

// Check range (0-100)
do
{
    printf("What is the first student's grade?   ");
    scanf("%d", &iScore1);

} while ((iScore1 < 0) || (iScore1 > 100));


// Ask user for second student name and score
printf("What is the second student's name?   ");
scanf("%s", arrcName2);

// Check range (0-100)
do
{
    printf("What is the second student's grade?   ");
    scanf("%d", &iScore2);

} while ((iScore2 < 0) || (iScore2 > 100));


// Ask user for third student name and score
printf("What is the third student's name?   ");
scanf("%s", arrcName3);

// Check range (0-100)
do
{
    printf("What is the third student's grade?   ");
    scanf("%d", &iScore3);

} while ((iScore3 < 0) || (iScore3 > 100));


// Ask user for fourth student name and score
printf("What is the fourth student's name?   ");
scanf("%s", arrcName4);

// Check range (0-100)
do
{
    printf("What is the fourth student's grade?   ");
    scanf("%d", &iScore4);

} while ((iScore4 < 0) || (iScore4 > 100));


// Print student name's and grades to file
fprintf(Fpointer, "%s %d\n", arrcName1, iScore1);
fprintf(Fpointer, "%s %d\n", arrcName2, iScore2);
fprintf(Fpointer, "%s %d\n", arrcName3, iScore3);
fprintf(Fpointer, "%s %d\n", arrcName4, iScore4);

// Close file
fclose(Fpointer);


// Open file for reading
Fpointer = fopen(FILENAME, "r");


// If file is there and working properly, scan it
if (Fpointer != NULL)
{
// Scan file for data
fscanf(Fpointer, "%s %d", arrcName1, &iScore1);
fscanf(Fpointer, "%s %d", arrcName2, &iScore2);
fscanf(Fpointer, "%s %d", arrcName3, &iScore3);
fscanf(Fpointer, "%s %d", arrcName4, &iScore4);

}

// Otherwise, ERROR
else
{
    printf("ERROR: File not found!");

}

// Display it all, just for the visual
printf("\nStudent one, %s, scored a %d.\n", arrcName1, iScore1);
printf("Student two, %s, scored a %d.\n", arrcName2, iScore2);
printf("Student three, %s, scored a %d.\n", arrcName3, iScore3);
printf("Student four, %s, scored a %d.\n\n", arrcName4, iScore4);


// Assume the first is the highest
iHighestScore = iScore1;
strcpy(arrcHighestName, arrcName1);

// Assume the first is the lowest
iLowestScore = iScore1;
strcpy(arrcLowestName, arrcName1);

// Compare score 2 to score 1
// Highest
if (iScore2 > iScore1)
{
    iHighestScore = iScore2;
    strcpy(arrcHighestName, arrcName2);

}

// Lowest
if (iScore2 < iScore1)
{
    iLowestScore = iScore2;
    strcpy(arrcLowestName, arrcName2);

}

// Compare score 3 to score 2 and 1
// Highest
if ((iScore3 > iScore2) && (iScore3 > iScore1))
{
    iHighestScore = iScore3;
    strcpy(arrcHighestName, arrcName3);

}

// Lowest
 if ((iScore3 < iScore2) && (iScore3 < iScore1))
{
    iLowestScore = iScore3;
    strcpy(arrcLowestName, arrcName3);

}

// Compare score 4 to score 3, 2 and 1
// Highest
if ((iScore4 > iScore3) && (iScore4 > iScore2) && (iScore4 > iScore1))
{
    iHighestScore = iScore4;
    strcpy(arrcHighestName, arrcName4);

}

// Lowest
 if ((iScore4 < iScore3) && (iScore4 < iScore2) && (iScore4 < iScore1))
{
    iLowestScore = iScore4;
    strcpy(arrcLowestName, arrcName4);

}

// Print highest and lowest scores and their respective students
printf("\nThe highest score was a %d from %s.\n", iHighestScore,               
arrcHighestName);
printf("The lowest score was a %d from %s.\n", iLowestScore, 
arrcLowestName);


// Perform average calculation and print result
dScoreAverage = (iScore1 + iScore2 + iScore3 + iScore4) / 4.0;
printf("\n\nThe test average is %.2f\n\n", dScoreAverage);


// Close file
fclose(Fpointer);

return 0;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Write user input string to file in C

From Dev

write user input to a file

From Dev

File path as user input in C

From Dev

Get basic file permissions to compare with input in C

From Dev

Python- how do I write user input to an Excel file?

From Dev

Python- how do I write user input to an Excel file?

From Dev

Can't get PHP script to write user input to text file

From Dev

Java Using the Scanner to write user input to a .txt file

From Dev

Write a program that takes a user's input and compares it to words in a file

From Dev

C++ cin with file instead of user input

From Dev

C Program file-io user input

From Dev

C++ cin with file instead of user input

From Dev

C Program file-io user input

From Dev

Save user input to a text file c++

From Dev

opening a file based on user input c++

From Dev

(Very Basic) If user types then write

From Dev

Write file input for subprocess

From Dev

Strange Error when writing basic stuff in VS Community - C++

From Dev

C# visual studio user input for file path and file name

From Dev

User input into file path

From Dev

How to read a .txt file from user input in C

From Dev

python 3.x : How to write/append text to a existing file as per user input in new line?

From Dev

user input in c

From Dev

Processing user input in C

From Dev

user input in c

From Dev

How do I write a shell script called myedit which asks the user to input a file name, and then starts up an editor to edit that file

From Dev

How to Read and Write from/to the same XML file in Visual Basic/C# using Data Set?

From Dev

Write a menu program in C++ that lets the user select from a list of options, and if the input is not one of the options, reprint the list

From Dev

PHP-CLI How to write in user input?

Related Related

  1. 1

    Write user input string to file in C

  2. 2

    write user input to a file

  3. 3

    File path as user input in C

  4. 4

    Get basic file permissions to compare with input in C

  5. 5

    Python- how do I write user input to an Excel file?

  6. 6

    Python- how do I write user input to an Excel file?

  7. 7

    Can't get PHP script to write user input to text file

  8. 8

    Java Using the Scanner to write user input to a .txt file

  9. 9

    Write a program that takes a user's input and compares it to words in a file

  10. 10

    C++ cin with file instead of user input

  11. 11

    C Program file-io user input

  12. 12

    C++ cin with file instead of user input

  13. 13

    C Program file-io user input

  14. 14

    Save user input to a text file c++

  15. 15

    opening a file based on user input c++

  16. 16

    (Very Basic) If user types then write

  17. 17

    Write file input for subprocess

  18. 18

    Strange Error when writing basic stuff in VS Community - C++

  19. 19

    C# visual studio user input for file path and file name

  20. 20

    User input into file path

  21. 21

    How to read a .txt file from user input in C

  22. 22

    python 3.x : How to write/append text to a existing file as per user input in new line?

  23. 23

    user input in c

  24. 24

    Processing user input in C

  25. 25

    user input in c

  26. 26

    How do I write a shell script called myedit which asks the user to input a file name, and then starts up an editor to edit that file

  27. 27

    How to Read and Write from/to the same XML file in Visual Basic/C# using Data Set?

  28. 28

    Write a menu program in C++ that lets the user select from a list of options, and if the input is not one of the options, reprint the list

  29. 29

    PHP-CLI How to write in user input?

HotTag

Archive