![]() |
In my assignment I am to construct a program that will read from a file. From this file, is the information such as student name, offense, #frequency of offense, and prior offenses. The program must take this into account and recommend a punishment. 10 students 5offenses.
The program should read its input form a text file using the fscanf function. Once the program reads in a line the data should be copied into a struct and processed. The program is to read from the file until end-of-file is reached. The program should read and process each record from the input file, on record at a time. There is no need to create an array of records. Use a while loop to drive the process. For each record, the program should consider the offense, the number of times it has been committed and the student's overall record. After considering these factors, the program should choose a sanction and print the name of the student on the screen and the recommended punishment. My psuedo code is: print & scan files into .txt fscanf & copy into a struct. then, it should read from file until EOF My question is, how do i get the info from the txt file into the struct? CODE: #include <stdio.h> #include <string.h> #include <stdlib.h> struct person { char name[50]; char offense[50]; double frequency[3]; double priors[4]; }; int main(void) { FILE *input; struct person student[10]; int i = 0; // counter for the array index int j = 0; // counter for display loop input = fopen("input.txt","r"); while(feof(input)==0) { fscanf(input," %s ", student[i].name); fscanf(input," %s ", student[i].offense); fscanf(input," %s ", student[i].frequency); fscanf(input," %s ", student[i].priors); i++; // increment for next time through the loop } for (j = 0; j < i; j++) // remember that i has the number of entries in the array { printf("%s %s %s %s\n", student[j].name, student[j].offense, student[j].frequency, student[j].priors); } fclose(input); getchar(); } |
This topic has been covered very recently in another thread. If you search the latest threads you'll find your answer.
|
unfortunately that doesnt explain how to make an input file, or fscanf from it. I tried the latter version of your code and it didnt work. The program must input from a file, not user input.
Thanks |
I don't understand what you mean by 'the program must input from a file'
|
Instead of user input with the scanf, we are to read from a .txt file with the fscanf function. Here is the assignment instructions.
Note: I am not looking for someone to do this assignment for me. I just need a little help. Thanks Your job is to write a Sentencing Guideline program for Mr. Magoo. It should read records from a file containing the students name, offense and other information. For each student, it should print to the console a recommended punishment for the offense. The program should be implemented in C and should use a user-defined datatype (i.e., a struct) along with an iteration control structure (while or for loop) and one or more conditional structures (if or switch statements). The fscanf and printf functions should be used for input/output requirements. Program Input The program should read its input from a text file using the fscanf function. One student record should appear on each line. The record should contain the student’s name, the offense, the number of times the student has committed that offense and the number of times the student has been in trouble overall. The record should be in a fixed format to make the input process easier. Once your program reads in a line the data should be copied into a struct and processed. It is acceptable for the program to hard code the number of students in the file, although you will not receive the enhancement points. A better approach is to read from the file until end-of-file is reached, and thus your program can process an arbitrary number of students. Your program should process at least ten students and handle at least five offenses. Processing the Input Your program should read and process each record from the input file, one record at a time. There is no need to create an array of records. You may use a while loop or for loop to read the records and drive the process. For each record, your program should consider the offense, the number of times it has been committed and the student’s overall record. After considering these factors, your program should choose a sanction and print the name of the student on the screen and the recommended punishment. This process repeats for each student. To consider the offense and the other factors, you may use if statements or switch statements in any combinations you wish. Your program should be able to choose from at least three punishments. Not all punishments should be available for all offenses. However, at least two punishments should be available for most of the offenses. You may have one serious offense that qualifies automatically for the most severe punishment, but most should consider the number of times the offense has been committed and the student’s overall behavior record. Make sure you close the input file before ending your program. |
I still don't get what you mean...You already are using fscanf to read from the text file
|
what i have isnt working, I need to first create a file with data on it, then read from the file.
It brings up the console with garbage. I cant figure it out. |
Your program works fine when I compile it
|
Im compiling it alright, it just doesnt seem to work, the console is printing garbage. Im useing Visual C++ 6.0
Oh well, itll all be overwith tonight |
I compiled it with gcc
|
| All times are GMT -5. The time now is 2:16 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC