Help with reading from file

I'm having problems with being able to read from a file.

This is my code.

http://pastebin.com/NEgH8XyE

I need to be able to read from a file like this.

Course Title: Computer Science
Instructor: Dr. FUN
1000 John 9.5 9.0 8.5 8.0 8.5 87.0 92.5 86.0
2000 Tom 10.0 6.7 10.0 10.0 10.0 100.0 100.0 100.0
3000 Alice 6.9 8.0 8.0 8.0 8.0 80.0 80.0 80.0

and be able to write those grades into the memory from the file.

Help is appreciated.

Thanks
What exactly is wrong? (compilation errors, crashes, wrong output?)
"problems" is a bit abstract and I'd rather not read all of that code.
I can't figure out how to get it to let me read numbers that are next to each other. fstream InFile(FileName.c_str(), ios::in);
getline(InFile, Line1);
getline(InFile, Line2);
Number = 0;
double Buffer = 0;
double TempQ1 = 0, TempQ2 = 0, TempQ3 = 0, TempQ4 = 0, TempQ5 = 0, TempM1 = 0, TempM2 = 0, TempE1 = 0;
double FakeId = 0;
string StuName;

InFile >> Buffer;
FakeId = Buffer;

InFile >> Buffer;
StuName = Buffer;

InFile >> Buffer;
TempQ1 = Buffer;

InFile >> Buffer;
TempQ2 = Buffer;

InFile >> Buffer;
TempQ3 = Buffer;

InFile >> Buffer;
TempQ4 = Buffer;

InFile >> Buffer;
TempQ5 = Buffer;

InFile >> Buffer;
TempM1 = Buffer;

InFile >> Buffer;
TempM2 = Buffer;

InFile >> Buffer;
TempE1 = Buffer;

List.push_back(Student(FakeId, StuName, TempQ1, TempQ2, TempQ3, TempQ4, TempQ5, TempM1, TempM2, TempE1) );

When Execute I get the same number for all of my temps and then the string for stuname is some weird looking circle
closed account (43RGz8AR)
Agreed ^^

But for the heck of it I copied and tried to compile it. Got some type errors first thing.

1
2
3
4
5
6
7
8
||=== r, Release ===|
C:\Users\Josephine\Desktop\r\topic.cpp||In member function 'void GradingSystem::SeeGrades()':|
C:\Users\Josephine\Desktop\r\topic.cpp|217|warning: comparison between signed and unsigned integer expressions|
C:\Users\Josephine\Desktop\r\topic.cpp||In member function 'void GradingSystem::WriteStuffToFile()':|
C:\Users\Josephine\Desktop\r\topic.cpp|275|warning: comparison between signed and unsigned integer expressions|
C:\Users\Josephine\Desktop\r\topic.cpp||In member function 'void GradingSystem::ChangeGrade()':|
C:\Users\Josephine\Desktop\r\topic.cpp|287|warning: comparison between signed and unsigned integer expressions|
||=== Build finished: 0 errors, 3 warnings ===|


I'm looking through it to see or find anything else. (But just a reminder I shouldn't have to!) I don't promise anything. Please if you will give more details I'm only doing this out of boredom.

EDIT: Sorry I forgot I formatted the code so those line numbers are useless to you...

Anyway on mine.
line 217:
for(Number = 0; Number < List.size(); Number++) { // Prints Data to screen
line 275:
for(Number = 0 ; Number < List.size() ; Number++) {
line 287:
for(Number = 0 ; Number < List.size(); Number++) {

Basically all the same thing. Although I don't think this is causing your problems, but please note that unknowly casting is to be avoided.
Last edited on
I don't know what that means, but i get those warnings everytime i compile. it still works however.

to replicate what i see

you need to make a file with what i posted

Course Title: Computer Science
Instructor: Dr. FUN
1000 John 9.5 9.0 8.5 8.0 8.5 87.0 92.5 86.0
2000 Tom 10.0 6.7 10.0 10.0 10.0 100.0 100.0 100.0
3000 Alice 6.9 8.0 8.0 8.0 8.0 80.0 80.0 80.0

in it and have it in the right directory to be able to load it. then run the program
type

set file

"file name . extension"

and then read file

and it should display what im talking about.

you definitely dont have to but the help is much appreciated. sorry for the lack of punctuation and whatnot but its really late for me haha and im getting extremely frustrated by this
closed account (43RGz8AR)
Okay I'll try here in a minute.

I'll explain what this means.
warning: comparison between signed and unsigned integer expressions

Number is declared as a int but size is declared as unsigned int.
Number < List.size() so here you are comparing to different read types. Unsigned means only positive numbers, signed, which every default type in C++ is, means the number can be negative as well as positive. Simply declaring Number as unsigned int clears this error because you are no longer casting types. Casting is changing what type a variable was declared as in order to change or read/compare with a different type. The compiler is warning of this because it maybe that you did not mean to do this as you did not explicitly tell it your casting.
Ahh I read about that it makes a lot more sense the way you explain it.

Will it really cause any problems in my program?
closed account (43RGz8AR)
Okay so a couple things you might want to fix on top of your problem.

First, you need to check if the file name the user entered is actually opened without error before you start reading data.

Second, your getting a loop back after the user sets the file name. So it gets a blank command and errors. Fix this by using getline(cin, FileName);.

Third, just to note this is crazy code having a method call another method of the same class just for setting a string. You really should stay away from making a method call another method. I do like that you used a class to do all the console controls.
1
2
GradingSystem Program;
Program.MenuDisplayer();

Why couldn't GradingSystem have a constructor method in place of "MenuDisplayer" when all your doing is calling it first thing?

So your main problem being "GradingSystem::ReadFile()" you wrote this so complicated compared to what it could've been it might just be easier if you rewrite that one section and really look at what you did wrong. By the way you only used cout on Line1 and Line2. So if you were really just wondering why things weren't printing, that is why.

EDIT: In this program it would not have caused errors unless you tired to store a number bigger then 2147483647 or less then 0. It is something to be aware of you really shouldn't type cast unless you really know how it will change the values pulled from the cast.
Last edited on
How do you check if a file is actually opened?

Ahh cool thanks I was wondering about that being triggered everytime I ran it.

Yeah, this is for an intro to programming class so my skills at writing elegant code are nowhere close to polished.

I'm not entirely sure what it is that you are proposing.

Yeah, could you show me a mockup of how Reading from a file and accessing information like i have can be done?

If you run "see grades" after the data is entered it should print the first first persons information (since i only took one line) but it sets it all to 1000 (which is the id and not the grade)
closed account (43RGz8AR)
I realize your are a beginner and I'm not going to give you complete code. I believe that wont really teach or help you. I suggested before
you wrote this so complicated compared to what it could've been it might just be easier if you rewrite that one section and really look at what you did wrong.
That way you will really learn from your mistake. You will find what you did wrong if you really look at what your doing or what you did.

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
void GradingSystem::ReadFile() {
	fstream InFile(FileName.c_str(), ios::in);
	getline(InFile, Line1);
	getline(InFile, Line2);

        // I didn't really see why you do this
	Number = 0;

        // here you make a buffer for I don't what?
	double Buffer = 0;

        // Then again have more buffers
	double TempQ1 = 0,
                    TempQ2 = 0,
                    TempQ3 = 0,
                    TempQ4 = 0,
                    TempQ5 = 0,
                    TempM1 = 0,
                    TempM2 = 0,
                    TempE1 = 0;

	double FakeId = 0;

        // StuName is never set.
	string StuName;

	InFile >> Buffer;
	FakeId = Buffer;
        // now the long list of buffer = buffer

	InFile >> Buffer;
	StuName = Buffer;

	InFile >> Buffer;
	TempQ1 = Buffer;

	InFile >> Buffer;
	TempQ2 = Buffer;

	InFile >> Buffer;
	TempQ3 = Buffer;

	InFile >> Buffer;
	TempQ4 = Buffer;

	InFile >> Buffer;
	TempQ5 = Buffer;

	InFile >> Buffer;
	TempM1 = Buffer;

	InFile >> Buffer;
	TempM2 = Buffer;

	InFile >> Buffer;
	TempE1 = Buffer;

	List.push_back(Student(FakeId,
                                              StuName,
                                              TempQ1,
                                              TempQ2,
                                              TempQ3,
                                              TempQ4,
                                              TempQ5,
                                              TempM1,
                                              TempM2,
                                              TempE1));


	cout << Line1 << endl << Line2 << endl;
       // doesn't print anything else, but I have a feeling you wanted to see more.
}
Last edited on
To check if a file is open:
1
2
if (inFile.is_open())
   // do something 


You can't expect to use std::getline(inFile, str) unless there is a unique line for the data to go into.
Just use pure inFile >> to read into the data.

1
2
3
4
5
6
inFile >> fakeID;
inFile >> StuName;
inFile >> TempQ1;
inFile >> TempQ2;
// and so on. Don't worry about a temp space. Read directly into the data.
// Just make sure that the data in the file has white space between each number and name. 
closed account (43RGz8AR)
Or:
1
2
3
if(InFile.fail()) {
      // Oh no it failed!
}

This will tell you if anything went wrong at all after any fstream operation.
void GradingSystem::ReadFile()
{
fstream InFile(FileName.c_str(), ios::in);
if (InFile.is_open())
{
getline(InFile, Line1);
getline(InFile, Line2);
Number = 0;
double TempQ1 = 0, TempQ2 = 0, TempQ3 = 0, TempQ4 = 0, TempQ5 = 0, TempM1 = 0, TempM2 = 0, TempE1 = 0;
double FakeId = 0;
string StuName;

InFile >> FakeId;
InFile >> StuName;
InFile >> TempQ1;
InFile >> TempQ2;
InFile >> TempQ3;
InFile >> TempQ4;
InFile >> TempQ5;
InFile >> TempM1;
InFile >> TempM2;
InFile >> TempE1;

List.push_back(Student(FakeId, StuName, TempQ1, TempQ2, TempQ3, TempQ4, TempQ5, TempM1, TempM2, TempE1) );

}

cout << Line1 << endl << Line2 << endl;
}

This is what I have now and it works like I want to except I can't figure out how to be able to tell if im at an end of file


while ( !InFile.eof )

gives me Error 4 error C2276: '!' : illegal operation on bound member function expression
closed account (43RGz8AR)
Try using while(!InFile.eof())
You solved my problems. What would I ever do without you? :P

I may be back in a few minutes with more >.>
Is there some sort of easy way to do what is described here?


a. Each quiz is graded on the basis of 10 points. Among the five quizzes, discard the lowest
score and use the remaining four scores for the grade calculation.

b. Each midterm is graded on the basis of 100 points.

c. The final exam is graded on the basis of 100 points.

d. For the numeric average, the final exam counts for 40 percent, midterms count for 40
percent, and quizzes count for 20 percent, respectively. Any average of 90 or more is
an ‘A’, any average of 80 or more (but less than 90) is a ‘B’, any average of 70 or more
(but less than 80) is a ‘C’, any average of 60 or more (but less than 70) is a ‘D’, and any
average below 60 is a ‘F’,


The way I'm going to do it is ridiculously complicated but will work
Topic archived. No new replies allowed.