File isn't storing into a string??

Nov 3, 2019 at 7:35pm
I've referenced my textbook, the internet, and some friends. I cannot figure out why I cannot grab anything from the file. Here is the file:

1
2
3
4
5
6
7
Joseph Blake	1	81	90	95	78
Susy Marywhether   0	67	80	81	79
Brandon Cortez	1	90	91	88	93
Paul Johnson	1	93	89	78	90
Joshua Banks	1	98	91	89	97
Angela Robins	0	57	70	63	72
Shawn Martinez	0	89	92	90	92


So the text file is written like first name[space]lastname[tab]1 for undergrad or 0 for grad[tab]score[tab] and so forth.

regardless of how to place my information into the structure that you will see referenced in the code, I would like to be able to output the first and last name followed by the rest of the information to make sure the file is grabbing everything correctly. However, I go to the Locals Window under Debug and both string athleteF and athleteL (first and last names respectively) are empty. Any help would be greatly appreciated.




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
81
82
83
84
 int main()
{
	//User input for file name
	string fileName;

	//User input for #s
	int numAthletes;
	int numScores;

	//Read from file

	string athleteL;

	//Read from file
	int score;
	int whatClass;

	//File variable 
	ifstream fileN;
	
	//Structure Variable
	Student athlete1;

	//string end = ".txt";

//Here, I will use the user's input to open the file. But for the sake of knowing how to properly read from a file, I hardcoded the name of my example file.
	//cout << "Please enter the name of the file you wish to read from: ";
	//getline(cin, fileName);
	//fileName.append(end);
	fileN.open("name.txt");

	cout << "Please enter the number of athletes on the file: ";
	cin >> numAthletes;

	cout << "Please enter the number of scores per athlete: ";
	cin >> numScores;

	string athleteF;

	fileN.open(fileName.c_str());

	if (fileN.is_open())
	{
		cout << "yeah ok." << endl;
	}

	else
	{
		cout << "This file could not be opened." << endl;
	}

	while (fileN)
	{
		fileN >> athleteF;
                fileN >> athleteL;
		fileN >> whatClass;
		if (whatClass == 0)
		{
			athlete1.stStatus = UNDERGRADUATE;
		}
		else if (whatClass == 1)
		{
			athlete1.stStatus = GRADUATE;
		}
		
		
	}
	


	cout << athlete1.fullName;
//This is where I got desperate thinking that I was just outputting it wrong.
	for (int i = 0; i < numScores; i++)
	{
		cout << athleteF;

	}
	cout << endl;
	cout << athletetF;
	cout << athleteL;
	cout << endl;
	showMenu();
return 0;
}
Last edited on Nov 3, 2019 at 8:20pm
Nov 3, 2019 at 7:38pm
Nowhere in your code do you put any data into the string athleteF. It's a blank string.
Nowhere in your code do you put any data into the string athleteL. It's a blank string.
Nov 3, 2019 at 7:41pm
I'm so sorry, I must have not copied and pasted right.

Even when I use fileN >> athleteF; in the while(fileN) loop, I am still getting a blank string.

Thanks for the catch, I'll update my code.
Nov 3, 2019 at 7:49pm
Looks like your loop tries to do this (after you editied the code), over and over:

Read firstname.
Read lastname
Read class.

So you read the values in, in sets of three, over and over.

Joseph Blake 1 81 90 95 78
Your code will try to read in:
FIRST LOOP: Joseph to athleteF. Then Blake to athleteL. Then 1 to whatClass.
NEXT LOOP: 81 to athleteF. 90 to athleteL. 95 to whatClass.

81 as the athlete's first name. That sure seems odd.

I see also that each time you read in to athleteF, it just gets overwritten next loop. This code makes no sense. Replace your loop with this, and see what you're actually reading:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 while (fileN)
	{
		fileN >> athleteF;
cout << "READ athleteF: " << athleteF << endl;
                fileN >> athleteL;
cout << "READ athleteL: " << athleteL << endl;
		fileN >> whatClass;
cout << "READ whatClass: " << whatClass << endl;
		if (whatClass == 0)
		{
			athlete1.stStatus = UNDERGRADUATE;
		}
		else if (whatClass == 1)
		{
			athlete1.stStatus = GRADUATE;
		}
	}


cout << athlete1.fullName;
Your code never sets this value, so I imagine it's blank.
Last edited on Nov 3, 2019 at 7:52pm
Nov 3, 2019 at 8:02pm
Your explanation makes perfect sense and I see where it went wrong.

However, I still am not outputting anything. atheleteF and athleteL are still assigned to "" and I do not even see "READ athleteF: " on the screen. I have no idea what is wrong with my code.
Nov 3, 2019 at 8:10pm
This code. Does it compile? Because there's a lot odd with it. Like numAthletes. This varable never gets created anywhere (although you do create one called numAlthetes ), which suggests this code can't be compiling, and if it doesn't compile, you can't be running it.
Last edited on Nov 3, 2019 at 8:15pm
Nov 3, 2019 at 8:15pm
It does compile, but I do not get anything for my strings. I am not sure if I am even going into the WHILE**** loop.

numAthletes is inputted by the user and it will allow me to know how many structures I will have control of. each athlete will have an average score, a name, score array, etc for them.

The reason why I haven't done the array for the scores yet or done much else with the structure is because I want to know that I am reading from the file correctly before I try to finish the rest of my code.

The code exits with code 0 every time, and it seems to actually open the file (hence the "yeah ok" outputting on the screen when I run it).
Last edited on Nov 3, 2019 at 8:16pm
Nov 3, 2019 at 8:19pm
It does NOT compile.

Look, here:
cin >> numAthletes;

This variable, numAthletes , does not exist.
You created a variable much earlier: int numAlthetes;

Different spelling. Spelling mistake. The code you've shown above CANNOT compile, therefore whatever you're running CANNOT be the code above.
Nov 3, 2019 at 8:19pm
I apologize for the inconveniences. Here is my entire code for absolute transparency:

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iomanip>
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <fstream>
#include <cstring>

using namespace std;

//enum
enum status { UNDERGRADUATE, GRADUATE };

//structures
struct Athlete
{
	string fullName;
	int* scores;
	status stStatus;
	int average;
	int* max;
};

//function prototypes
void outputA_Struct(Athlete, int);
int minScore(int*, int);
int * maxScore(int*, int);
void changeScore(Athlete*, string, int, int, int, int);
int computeAvg(int*, int);
vector<Athlete> listOfHigherThan_X(Athlete, int, int);
vector<Athlete> listOfAll_As(Athlete, int, int);
void outputVector(vector<Athlete>);

//my functions
void showMenu();

int main()
{
	//User input for file name
	string fileName;

	//User input for #s
	int numAthletes;
	int numScores;

	//Read from file

	string athleteL;

	//Read from file
	int score;
	int whatClass;

	//File variable 
	ifstream fileN;
	
	//Structure Variable
	
        Athlete athlete1;

	//string end = ".txt";

	cout << "Please enter the name of the file you wish to read from: ";
	//getline(cin, fileName);
	//fileName.append(end);
	fileN.open("name.txt");

	cout << "Please enter the number of students on the file: ";
	cin >> numAthletes;

	cout << "Please enter the number of scores per student: ";
	cin >> numScores;

	string athleteF;

	fileN.open(fileName.c_str());

	if (fileN.is_open())
	{
		cout << "yeah ok." << endl;
	}

	else
	{
		cout << "This file could not be opened." << endl;
	}

	while (fileN)
	{
		fileN >> athleteF;
		cout << "READ athleteF: " << athleteF << endl;
		fileN >> athleteL;
		cout << "READ athleteL: " << athleteL << endl;
		fileN >> whatClass;
		cout << "READ whatClass: " << whatClass << endl;
		if (whatClass == 0)
		{
			athlete1.stStatus = UNDERGRADUATE;
		}
		else if (whatClass == 1)
		{
			athlete1.stStatus = GRADUATE;
		}
	}



	showMenu();
/*
	int array2[] = { 99, 90, 95, 43 };
	int minimum;
	int size = 4;
	minimum = minScore(array2, size);
	cout << minimum;
	int * maximum;
	maximum = maxScore(array2, size);
*/
	return 0;
}


I didn't know whether to include headerfiles or not.
Last edited on Nov 3, 2019 at 8:27pm
Nov 3, 2019 at 8:22pm
Obviously it's a typo. A typo that makes the code impossible to compile. The code above cannot be what you're running.
Nov 3, 2019 at 8:31pm
I edited the above message.

I am not sure if it gives notifications or not.

I am also unsure of what exactly I did to have that typo. I must have edited it when I was going through the code on the website and mistyped, but it was right in my code. I apologize sincerely.
Nov 3, 2019 at 8:36pm
fileN.open("name.txt");
and then a few lines later
fileN.open(fileName.c_str());

Only open the file once.
Nov 3, 2019 at 8:39pm
Thank you so much!

It's honestly crazy how the smallest things can take so long and a lot of sources to figure out. You're a life saver. I appreciate your help so much.

I can't believe it was something so tedious and insignificant. Thank you once again. :) Have a wonderful day.
Topic archived. No new replies allowed.