Getting weird output when trying to pull info from file and then display it on screen

As the title says I am trying to pull info from a file, calculate GPA with file info, and display it on screen. I keep getting this weird output (below) and it is not showing the correct info. If anyone could help me figure out what is happening it would be greatly appreciated.
[output]
File Name: info1
Student's Name:
Student's Age: -13108
Student's SSN: -858993460
Student's Sex: ╠

Transfer Hours: -858993460 Transfer Quality Points: -858993460
MSU Hours: -858993460 MSU Quality Points: -858993460

-------------------

Transfer GPA: 1.00
MSU GPA: 1.00
Total GPA: 1.00
[correct info]
Name: Peter Ford
Age: 21
SSN: 789341256
Sex: M
Transfer hours: 10 Transfer quality points: 30
MSU hours: 48 MSU quality points: 179
[code]

#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>

using namespace std;

struct Record {
char Sex;
short Age;
long SSN;
int hours, quality;
float GPA;
string Fname, Lname;
int totalhours, totalquality;
float totalGPA;
int MSUhours, MSUquality;
float MSUGPA;
};
void displayArecord(ofstream &myFileOut, Record Fname, Record Lname, Record age, Record SSN, Record Sex, Record hours, Record quality, Record GPA, Record MSUhours, Record MSUquality, Record MSUGPA, Record totalhours, Record totalquality, Record totalGPA)
{
Record record;

record.GPA = (float)record.quality / record.hours;
record.MSUGPA = (float)record.MSUquality / record.MSUhours;
record.totalhours = (record.MSUhours + record.hours);
record.totalquality = (record.quality + record.MSUquality);
record.totalGPA = (float)record.totalquality / record.totalhours;

std::cout << left << setw(20) << "Student's Name: " << record.Fname << ' ' << record.Lname << endl;
std::cout << setw(20) << "Student's Age: " << record.Age << endl;
std::cout << setw(20) << "Student's SSN: " << record.SSN << endl;
std::cout << setw(20) << "Student's Sex: " << record.Sex << "\n\n";

std::cout << setw(20) << "Transfer Hours: " << setw(15) << record.hours << setw(30) << "Transfer Quality Points: " << record.quality << endl;
std::cout << setw(20) << "MSU Hours: " << setw(15) << record.MSUhours << setw(30) << "MSU Quality Points: " << record.MSUquality << "\n\n";

std::cout << "-------------------" << endl;
std::cout << setprecision(2) << fixed << endl;
std::cout << setw(20) << "Transfer GPA: " << record.GPA << endl;
std::cout << setw(20) << "MSU GPA: " << record.MSUGPA << endl;
std::cout << setw(20) << "Total GPA: " << record.totalGPA << endl;

myFileOut << left << setw(20) << "Student's Name: " << record.Fname << ' ' << record.Lname << endl;
myFileOut << setw(20) << "Student's Age: " << record.Age << endl;
myFileOut << setw(20) << "Student's SSN: " << record.SSN << endl;
myFileOut << setw(20) << "Student's Sex: " << record.Sex << "\n\n";

myFileOut << setw(20) << "Transfer Hours: " << setw(15) << record.hours << setw(30) << "Transfer Quality Points: " << record.quality << endl;
myFileOut << setw(20) << "MSU Hours: " << setw(15) << record.MSUhours << setw(30) << "MSU Quality Points: " << record.MSUquality << "\n\n";

myFileOut << "-------------------" << endl;
myFileOut << setprecision(2) << fixed << endl;
myFileOut << setw(20) << "Transfer GPA: " << record.GPA << endl;
myFileOut << setw(20) << "MSU GPA: " << record.MSUGPA << endl;
myFileOut << setw(20) << "Total GPA: " << record.totalGPA << endl;
}
void getinfo(ifstream &myFileIn, Record &Fname, Record &Lname, Record &age, Record &SSN, Record &Sex, Record &hours, Record &quality, Record &MSUhours, Record &MSUquality) {

Record record;
string Label;

myFileIn >> Label >> record.Fname >> record.Lname;
myFileIn >> Label >> record.Age;
myFileIn >> Label >> record.SSN;
myFileIn >> Label >> record.Sex;
myFileIn >> Label >> Label >> record.hours;
myFileIn >> Label >> Label >> Label >> record.quality;

myFileIn >> Label >> Label >> record.MSUhours;
myFileIn >> Label >> Label >> Label >> record.MSUquality;
}

int main() {

Record Fname, Lname, age, SSN, Sex, hours, quality, GPA, MSUhours, MSUquality, MSUGPA, totalhours, totalquality, totalGPA;

string FileName, Filelocation = "C:\\Temp\\";
ofstream myFileOut;
ifstream myFileIn;
Record record;

cout << "File Name: ";
cin >> FileName;
myFileIn.open(Filelocation + FileName);
myFileOut.open("Record of " + FileName);

getinfo(myFileIn, Fname, Lname, age, SSN, Sex, hours, quality, MSUhours, MSUquality);
displayArecord(myFileOut, Fname, Lname, age, SSN, Sex, hours, quality, GPA, MSUhours, MSUquality, MSUGPA, totalhours, totalquality, totalGPA);

myFileOut.close();
myFileIn.close();
}
Last edited on
Please fix the [code][/code] tags.
This kind of output mostly hints to a not initialized variable(s). This:
1
2
3
4
5
void displayArecord(ofstream &myFileOut, Record Fname, Record Lname, Record age, Record SSN, Record Sex, Record hours, Record quality, Record GPA, Record MSUhours, Record MSUquality, Record MSUGPA, Record totalhours, Record totalquality, Record totalGPA)
{
Record record;

record.GPA = (float)record.quality / record.hours;
So record is not initialized and there's no way for a useful result.

I guess what you want is this:
1
2
3
4
5
void displayArecord(ofstream &myFileOut, Record &record) // Note: Record &record as parameter
{
Record record;

record.GPA = (float)record.quality / record.hours;


You haven't provided a sample file input so I can't easily test, but consider something like (not tried):

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using std::left;
using std::setw;
using std::setprecision;
using std::fixed;

struct Record {
	char Sex {};
	short Age {};
	long SSN {};
	int hours {}, quality {};
	float GPA {};
	std::string Fname, Lname;
	int totalhours {}, totalquality {};
	float totalGPA {};
	int MSUhours {}, MSUquality {};
	float MSUGPA {};
};

void displayArecord(std::ostream& myFileOut, const Record& record) {
	myFileOut << left << setw(20) << "Student's Name: " << record.Fname << ' ' << record.Lname << '\n';
	myFileOut << setw(20) << "Student's Age: " << record.Age << '\n';
	myFileOut << setw(20) << "Student's SSN: " << record.SSN << '\n';
	myFileOut << setw(20) << "Student's Sex: " << record.Sex << "\n\n";

	myFileOut << setw(20) << "Transfer Hours: " << setw(15) << record.hours << setw(30) << "Transfer Quality Points: " << record.quality << '\n';
	myFileOut << setw(20) << "MSU Hours: " << setw(15) << record.MSUhours << setw(30) << "MSU Quality Points: " << record.MSUquality << "\n\n";

	myFileOut << "-------------------\n";
	myFileOut << setprecision(2) << fixed << '\n';
	myFileOut << setw(20) << "Transfer GPA: " << record.GPA << '\n';
	myFileOut << setw(20) << "MSU GPA: " << record.MSUGPA << '\n';
	myFileOut << setw(20) << "Total GPA: " << record.totalGPA << '\n';
}

std::istream& getinfo(std::istream& myFileIn, Record& record) {
	std::string Label;

	myFileIn >> Label >> record.Fname >> record.Lname;
	myFileIn >> Label >> record.Age;
	myFileIn >> Label >> record.SSN;
	myFileIn >> Label >> record.Sex;
	myFileIn >> Label >> Label >> record.hours;
	myFileIn >> Label >> Label >> Label >> record.quality;
	myFileIn >> Label >> Label >> record.MSUhours;
	myFileIn >> Label >> Label >> Label >> record.MSUquality;

	if (myFileIn) {
		record.GPA = (float)record.quality / record.hours;
		record.MSUGPA = (float)record.MSUquality / record.MSUhours;
		record.totalhours = (record.MSUhours + record.hours);
		record.totalquality = (record.quality + record.MSUquality);
		record.totalGPA = (float)record.totalquality / record.totalhours;
	}

	return myFileIn;
}

int main() {
	const std::string Filelocation { "C:\\Temp\\" };
	std::string FileName;

	std::cout << "File Name: ";
	std::cin >> FileName;

	std::ifstream myFileIn(Filelocation + FileName);
	std::ofstream myFileOut(Filelocation + "Record of " + FileName);

	if (!myFileIn || !myFileOut)
		return (std::cout << "Cannot open files\n"), 1;

	for (Record record; getinfo(myFileIn, record); ) {
		displayArecord(std::cout, record);
		displayArecord(myFileOut, record);
	}
}

Topic archived. No new replies allowed.