.txt files and internal sobbing.

Hey everyone.

SO I just started my first C++ class and we are working on a Lab assignment.

All I need to do is make a program that reads this

Sandra King 75 62 84 98 96 86 70
Jason Briggs 82 71 65 72 81 95 90
Tina Simpson 55 60 62 54 55 68 31

from a .txt file

displays the names LastName, FirstName

and the average grade for each student.

My problem is that I can't for the life of me figure out how to get

the program to read the text. This is my 3rd program I have ever tried to build

and my progress is honestly pathetic.

Any help is much appreciated. Or even if you could tell me what to put in a

search bar to find this out would be great. Everything I've looked for only

show writing text to files, and reading files and printing them.

But I can find anything about how to turn the data into integers

heres what Ive got so far thanks



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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;



int main(int argc, char** argv) {

ifstream inputFile;

string name;

inputFile.open("grades");



inputFile >> name; 

cout << name << endl; 

inputFile >> name; 

cout << name << endl; 
inputFile >> name; 

cout << name << endl; 

inputFile.close(); 
return 0;

}


I know it's not much but at the moment im just trying to get the names to at lease print. all im getting back in the terminal is whitespace.... well blackspace i guess but you get my point. Thanks. Forgive my ignorance.
Hello DanTheHuman,

My first question is how do know that the file is open? Its a good idea to check and make sure the file is open. I use this something like this:
1
2
3
4
5
6
7
8
9
10
if (inFile.is_open())
{
        cout << "\n File is open" << endl; 
}
else
{
	cout << "\n File did not open" << endl;
        Sleep(3000);  //  wait for 3 seconds
        exit(1);  //  No reason to continue after this
}

the way you have started there is know way to know if you actually have read anything in. Except that nothing is printed out. Line 13 is a good start, but consider something like this:
inputFile >> fname >> lname >> grade1 >> grade2 >> grade3 >> grade4 >> grade5 >> grade6 >> grade7;.
Based on the file layout this is the quickest way to input everything at one time and do not forget to define the grade variables as ints.

Once you have the file open try this to test it out:
1
2
3
4
while (inFile >> fname >> lname >> grade1 >> grade2 >> grade3 >> grade4 >> grade5 >> grade6 >> grade7)
{
	cout << fname << ", " << lname << endl;
}

from this you can build the rest of your program.

Hope this helps,

Andy
Thank you both for the reply.
This is all super helpful.
I'll be working on this for the next couple hours.
I'll keep you updated on the progress.
Thanks a million!
It printed! for the love of god yes! thank you lol.

Okay so now all i have to do is some math and have it print with the names

I have it arranged so that is outputs last name and then first. Because that's how

the professor wants it. I'm posting this bit of code for anyone with the same problem as

me. I've actually seen this similar question popping up around the web a good bit.

Thank you guys so much. And good luck to anyone else working on the same thing .

I know this is pretty basic stuff but I'm way excited right now.



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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;



int main(int argc, char** argv) {

ifstream myFile;

string fname, lname;

double Q1,Q2,Q3,Q4,Q5,MDT,FNL;

myFile.open("C:/Users/USERNAME/Downloads/grades.txt"); 

while (myFile >> fname >> lname >> Q1 >> Q2 >> Q3 >> Q4 >> Q5 >> MDT >> FNL)
{
	
	cout << lname << (", ") << fname << endl;
}


myFile.close(); 
return 0;

}
Topic archived. No new replies allowed.