C++ assignment using input/output file :(

Honestly, I dont understand a thing in my C++ class T_T but this course is the core requirement so i cannot avoid taking it. Plz help me T_T I will appreciate it much!!!

This is the prompt:

ASSIGNMENT 2
Write a program that reads from a formatted file a list of 4 students and their grades for 3 tests, computes the average test score for each student and the class and the overall average for each test and for all tests and outputs them in a file
(in a special format).

The formatted input file contains on each line the name, student ID, and the grades for the 3 tests for a student (one
line per student). The format of the input file is the following:

Student1FirstName Student1LastName Student1ID Student1Test1 Student1Test2 Student1Test3
Student2FirstName Student2LastName Student2ID Student2Test1 Student2Test2 Student2Test3
Student3FirstName Student3LastName Student3ID Student3Test1 Student3Test2 Student3Test3
Student4FirstName Student4LastName Student4ID Student4Test1 Student4Test2 Student4Test3

The output file should be formatted as following:

STUDENTS
Student1LastName, Student1FirstNameInitial. Student1ID Student1TestAverage
Student2LastName, Student1FirstNameInitial. Student2ID Student2TestAverage
Student3LastName, Student1FirstNameInitial. Student3ID Student3TestAverage
Student4LastName, Student1FirstNameInitial. Student4ID Student4TestAverage
Class ClassAverage
TESTS
Test 1 Test1Average
Test 2 Test2Average
Test 3 Test3Average
Tests TestsAverage

where:
 StudentXLastName is the last name of student X as read from the input file.

 StudentXFirstNameInitial is the initial (first letter) of the first name of student X that you read from the input file.

 StudentXID is the student X’s ID as read from the input file

 StudentXTestAverage is the average between the student X's grades for Test1, Test2, and Test3.

 ClassAverage is the average of the student test averages.

 TestXAverage is the average of the student X's grades for Test1, Test2, and Test3.

 TestsAverage is the average of the test averages.

 The reminder values are constants and they should be outputted as they are.

The students and tests averages and overall totals should have exactly 2 decimals and should be aligned to the right. The
rest of the values should be aligned to the left. Make sure you are not missing any of the digits of the student ID. If you
are unable to determine the initial of the first name, you should use the first name instead (you will get partial credit
rather than 0). File1.txt and File2.txt are examples of input files that you can use to test your program.
For 5 points extra credit, change your program to read an optional middle name. Some students might have a middle
name while others might not have a middle name. Your program has to read names with both 2 words and 3 words (e.g.
Mary Smith, Mary Jane Smith). Your program should be able to distinguish between the 2 cases and read the data
appropriately (without confusing the last name for middle name, the last name for student ID, the student ID for test 1
grade, test 1 grade for test 2 grade, and so on). Do not output the middle name in the output file. You can use File3.txt
to test your extra credit part of the assignment.

You should use stream methods and manipulators rather than C++ features that we did not study in class like userdefined
functions, loops, arrays, string manipulation, etc.
Here is my code, but it's not working T_T

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Assignment 2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <fstream>
#include <iomanip>
#include <string> 

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	//declaring the variable
	ifstream inFile; //input file stream variable
    ofstream outFile; //output file stream variable

	string studentfirstName;
	string studentlastName;
	
	double studentTest1, studentTest2, studentTest3;
	double average;

	inFile.open("test.txt");                         
    outFile.open("testavg.out");                     

    outFile << fixed << showpoint;                  
    outFile << setprecision(2);                     

	inFile >> studentfirstName >> studentlastName;
	outFile << "Student name: " << studentfirstName << " " << studentlastName << endl;

	inFile >> studentTest1 >> studentTest2 >> studentTest3;
	outFile << "Student's Test Scores: " << setw(6) << studentTest1 << setw(6) 
		    << studentTest2 << setw(6) << studentTest3 << setw(6) << endl;
	average = (studentTest1 + studentTest2 + studentTest3) / 3.0;

	outFile << "Average test score: " << setw(6) << average << endl;


    inFile.close();                                 
    outFile.close(); 
	
	return 0;
}
What is the error you got? From what I could tell, when you read the data from inFile, right after studentFirstName and studentLastname; the next thing should be the 'studentID', and not studentTest1.. and so on...

NOT:
inFile >> studentfirstName >> studentlastName;
outFile << "Student name: " << studentfirstName << " " << studentlastName << endl;

inFile >> studentTest1 >> studentTest2 >> studentTest3;

IT SHOULD BE:

inFile >> studentfirstName >> studentlastName;
outFile << "Student name: " << studentfirstName << " " << studentlastName << endl;

inFile >> studentID; // you should declare this variable on top

inFile >> studentTest1 >> studentTest2 >> studentTest3;

Give it a try and see if it works.
Never ever use normal cin-calls for string inputting, use getline() instead.
Topic archived. No new replies allowed.