I suck at explaining my question.

Ok I have this code made, but now we are given a new assignment and instead of a user inputting the data we are getting it from a .txt file, how do I do that and make it so it shows in the organized format?

below is my code so far and a .txt file

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
#include <iostream>
#include <stdio.h>
#include <conio.h>

using namespace std;
class student
{
private :
	int sno;
	char name[20];
	double g1,g2,g3,g4;
	int absen;
public :
	void Getdata()
	{
		cout << "Enter Student ID:  "; cin >> sno; // User inputs their Student ID
		cout << "Enter Student NAME:  "; cin >> name; // User inputs the Studet Name
		cout << "Enter Student TEST SCORES (3):  "; cin >> g1 >> g2 >> g3; // User inputs the Test Scores
		cout << "Enter # of Absences:  "; cin >> absen; // User inputs how many absences
	}
	void Putdata()
	{
		cout  << "Student ID:  " << sno << endl; // displays Student ID
		cout << "Student NAME:  " << name << endl; // displays Student Name
		cout << "# of Absences:  " << absen << endl; // Displays # of Absences
		if (absen == 0) {
			g4 = 2;
		} else {
			g4 = 0;
		}
		cout << "Grade:  " << (g1+g2+g3 + g4)/3 << endl; // Displays Grade and if Successful/Unsuccessful
		if ((((g1+g2+g3 + g4)/3) >= 73))
		{cout << "Successful" << endl;}
		else
		{cout << "Unsuccessful" << endl;}
	};
};


int main()
{
	student s;
	s.Getdata();
	s.Putdata();
	getch();
	return 0;
}


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
Wiley, Nicholas I.,9,23,97,0
Schwarz, Larry C.,64,18,47,0
Grimes, Linda R.,17,53,43,2
Brown, Ronald R.,53,63,85,1
Hines, Neil I.,3,49,68,0
Jain, David A.,92,85,83,1
Glass, Cindy L.,68,69,54,4
Fowler, Jacob O.,82,91,40,3
Schneider, Wanda C.,58,51,37,0
Gilbert, Edith I.,39,99,34,0
Parker, Nicole A.,60,88,44,0
Shaw, Cecil H.,37,9,19,0
Gold, Caroline O.,1,41,48,0
Monroe, John O.,64,6,12,2
McDowell, Steve C.,56,79,25,1
Sumner, William U.,59,51,98,3
Pugh, Francis U.,32,65,53,2
Crane, Gary R.,62,39,13,2
Stevens, Eleanor T.,7,42,82,0
Greer, Anne R.,5,54,20,0
McKay, Dana C.,30,86,21,0
Crawford, Pauline R.,49,21,9,1
Stephenson, James T.,19,92,63,1
Stark, Peter T.,27,43,98,1
Rodgers, Lucille O.,94,57,86,0

does the text file have to be in the format :
1
2
3
4
5
6
7
8
9
Wiley, Nicholas I.,9,23,97,0
Schwarz, Larry C.,64,18,47,0
Grimes, Linda R.,17,53,43,2
Brown, Ronald R.,53,63,85,1
Hines, Neil I.,3,49,68,0
Jain, David A.,92,85,83,1
Glass, Cindy L.,68,69,54,4
Fowler, Jacob O.,82,91,40,3
You don't really need 2 threads for the same thing.
Binary -> I guess I could change the text file up if that makes it easier

Raezzor -> Sorry I thought my other one sucked at explaining the question.
if thats the case its extremely easy. Change a line to have only one piece of the puzzle.

.txt
1
2
3
4
Name
test scores
ID Number
(repeat for each student)


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

ifstream reader("students.txt");
	string name, scores, id;
	int track=1;
	if (reader.is_open()){ // while the file can be opened...

    while (reader.good()){ // while the file can be read...
	
		if(track==4){track=1;}
		
		
		if(track==1){
			getline(reader,name);
			 cout<<"name: "<<name<<endl;
		}
		if(track==2){
			getline(reader,scores);
			cout<<"scores: "<<scores<<endl;
		}
		if(track==3){ 
		getline(reader, id);
		cout<<"id: "<<id<<endl;
		}
	 
	  
	  track++;

    }
    reader.close(); //close the file.
  }
You'll want to use ifstream. Its very simple, and you include <fstream>:
1
2
3
4
5
6
7
#include <fstream>
#include <string>
using namespace std;

//...

ifstream infile("students.txt");   //opens the text file that is located within the same directory as your program 


To read textual information you would use the >> operator, in the same manner as cin:

1
2
string lastname;
infile>>lastname;


this reads until the first space or '\n' character. This means that you would also include the comma. However, the istream object has a method called get(...) that allows you to read up until a delimiting character. See here:
http://www.cplusplus.com/reference/istream/istream/get/

You don't need to close the file since the destructor has a method that closes the file stream automatically when the object is destroyed.

so you could do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include <ios>
#include <limits>
using namespace std;

//....

char* lastname, firstname, middle;
char delim = 0;
int g1 = 0;
int g2 = 0;
int g3 = 0;
int absences = 0;

infile.get(lastname, numeric_limits<streamsize>::max(), ','); //reads until the ',' character is found
infile.get();  //read in the next character, which is a comma
infile.get();  //read in the next character, which is a space
infile.get(firstname, numeric_limits<streamsize>::max(), ','); //reads in the first and middle initial, size the file is not consistent with its formatting. 


your text file is not consistent with its formatting.. It uses spaces as delimiters in the beginning but also commas. You'll need to manipulate the loop in order accommodate for this. Try to void changing the text file. its not yours, you'll have to work around it.
Topic archived. No new replies allowed.