I'm trying to import this file

45256	Rodrigues	Joana		58	75	58	61	59	75	63	92
37915	Wright		Michelle	98	83	56	62	63	90	57	67
81984	Williams	Jenny		55	67	54	63	89	84	93	75
73984	Phaneuf		Lesley		78	85	57	51	68	94	51	83
80886	Laflamme	Nicole		76	51	71	94	69	78	87	91
39473	Kenyon		Patricia	65	54	90	68	94	70	95	97
12127	McCabe		Kelly		51	96	0	64	54	75	71	94
52458	Whitten		Sarah		99	58	94	82	81	75	82	70
77921	Connors		Sarah		52	58	88	63	61	65	78	78
28810	Navin		Joshua		94	75	62	93	64	92	87	94
31571	Toporowski	Crystal		93	0	77	77	63	68	88	58
33580	Ziolko		John		74	64	98	92	98	89	0	79
14508	Stronach	Kurt		80	95	96	84	78	86	53	59
44520	Ecklord		Ryan		0	61	56	86	98	98	59	83
15246	Berling		Danielle	85	64	0	75	69	0	54	85
16137	Littlefield	Arionna		71	85	74	97	69	64	82	95
62631	Niedojadlo	Evan		73	66	83	97	97	51	66	88
59640	Knieriem	Brandon		52	87	97	66	97	90	93	56
44102	Spence		Arthur		85	84	100	82	91	0	95	62
42331	Rose		Nicole		83	50	96	68	54	62	0	93
72054	Houde		Jessica		72	66	71	62	75	71	86	100
24609	Cooper		Camille		62	93	92	72	58	76	57	66
85736	Hepburn		Spencer		71	0	62	60	52	62	64	51
87129	Morang		Nicholas	77	67	57	89	88	64	68	78
43865	Hildebrandt	Stephenson	97	94	93	93	82	88	78	100
78575	Suslovic	Vikilynn	65	54	74	67	61	76	69	76
58751	Flores		Jose		58	80	0	74	87	95	96	69
18140	Galotti		Salvator	78	65	90	66	88	57	93	98
10364	Denaro		Tony		73	85	55	72	68	91	55	89
99189	Vasquez		Oskar		66	60	0	75	63	92	92	52
21705	Roy		Jake		86	0	89	94	82	0	60	98
50838	Burr		Jermiah		83	84	94	90	77	73	72	63
32998	Benway		Eileen		76	98	80	69	75	94	77	95
87361	Perlmutter	Diadre		96	60	81	88	81	53	91	74

into a structure with the following code:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;

struct student
{
	int id;
	string fname;
	string lname;
	int gr[6];
	double avg;
	char letGr;
};

void print(student arr[], int c)
{
	cout << endl;
	for (int i=1;1<=c;i++)
	{
		cout << setw(2) << i << ' ' << left << setw(10) << arr[i].id
			<< arr[i].fname << arr[i].lname << right;
		for (int j=1;j<=5;j++)
			cout << setw(5) << arr[i].gr[j];
		cout << setw(6) << arr[i].avg << setw(3) << arr[i].letGr << endl;
	}
	cout << endl;
}


void main()
{
	ifstream inF;
	student myStuds[20];
	int cnt=0;
	int id;
	string fname, lname;

	cout << fixed << setprecision(1);

	inF.open("students.txt");
	inF >> id;
	while (!inF.eof())
	{
		cnt++;
		myStuds[cnt].id = id;
		myStuds[cnt].fname = fname;
		myStuds[cnt].lname = lname;
		for (int i = 1; i < 5; i++)
			inF >> myStuds[cnt].gr[i];
		myStuds[cnt].avg = 0.0;
		myStuds[cnt].letGr = 'Y';
		inF >> id;
	}
	inF.close();
	cout << "Cnt = " << cnt << endl;
	print (myStuds, cnt);
}

This compiles correctly, but when the dos screen comes up, the program immediately times out without an output....what's wrong?

Recommended Answers

All 23 Replies

Your attempt at reading the data from the file needs a lot of work. You're only trying to read the id and the grades, for example, but you pretend as if you've read the names. Also, you need to learn how to index arrays.

Your attempt at reading the data from the file needs a lot of work. You're only trying to read the id and the grades, for example, but you pretend as if you've read the names. Also, you need to learn how to index arrays.

Any other suggestions about how to fix it? I pretty much copied this verbatim from an example that our professor gave us in class, except that I inserted a studetn id and first name. Is it my reading of the file that is causing the problem?

Any other suggestions about how to fix it? I pretty much copied this verbatim from an example that our professor gave us in class, except that I inserted a studetn id and first name. Is it my reading of the file that is causing the problem?

Well, if you're not reading from the file, where does the data automagically come from? Yes you need to fix this.

You're not reading the names from the input file. It seems you have 8 columns of grades. I'm assuming you need to calculate the average from the data, and I'm also assuming that you'd categorize the average into letter grades.

An array [I]type[/I] array[N] is indexed from 0 to N; element array[N] is outside array bounds.

Are you familiar with getline and stringstreams? And did you know it's not a good idea to use .eof() as a loop control?

To get it to work and be reasonably responsible with error checking, I changed roughly 70% of the code you posted.

[edit]My output for my "home version" is

0 45256  Rodrigues    Joana       58  75  58  61  59  75  63  92 67.625 D
 1 37915  Wright       Michelle    98  83  56  62  63  90  57  67 72     C
 2 81984  Williams     Jenny       55  67  54  63  89  84  93  75 72.5   C
 3 73984  Phaneuf      Lesley      78  85  57  51  68  94  51  83 70.875 C
 4 80886  Laflamme     Nicole      76  51  71  94  69  78  87  91 77.125 C
 5 39473  Kenyon       Patricia    65  54  90  68  94  70  95  97 79.125 C
 6 12127  McCabe       Kelly       51  96   0  64  54  75  71  94 63.125 D
 7 52458  Whitten      Sarah       99  58  94  82  81  75  82  70 80.125 B
 8 77921  Connors      Sarah       52  58  88  63  61  65  78  78 67.875 D
 9 28810  Navin        Joshua      94  75  62  93  64  92  87  94 82.625 B
10 31571  Toporowski   Crystal     93   0  77  77  63  68  88  58 65.5   D
11 33580  Ziolko       John        74  64  98  92  98  89   0  79 74.25  C
12 14508  Stronach     Kurt        80  95  96  84  78  86  53  59 78.875 C
13 44520  Ecklord      Ryan         0  61  56  86  98  98  59  83 67.625 D
14 15246  Berling      Danielle    85  64   0  75  69   0  54  85 54     F
15 16137  Littlefield  Arionna     71  85  74  97  69  64  82  95 79.625 C
16 62631  Niedojadlo   Evan        73  66  83  97  97  51  66  88 77.625 C
17 59640  Knieriem     Brandon     52  87  97  66  97  90  93  56 79.75  C
18 44102  Spence       Arthur      85  84 100  82  91   0  95  62 74.875 C
19 42331  Rose         Nicole      83  50  96  68  54  62   0  93 63.25  D

This part is wrong :

inF >> id;
while (!inF.eof())
	{
		cnt++;
		myStuds[cnt].id = id;
		myStuds[cnt].fname = fname;
		myStuds[cnt].lname = lname;
		for (int i = 1; i < 5; i++)
			inF >> myStuds[cnt].gr[i];
		myStuds[cnt].avg = 0.0;
		myStuds[cnt].letGr = 'Y';
		inF >> id;
	}

I assume you wan't something like this :

while(!inF.eof()){
	inF >> myStuds[cnt].id;
	inF >> myStuds[cnt].fname;
	inF >> myStuds[cnt].lname;
	for (int i = 0; i < 6; i++)
		inF >> myStuds[cnt].gr[i];
	inF >> myStuds[cnt].avg;
        int grade = 0;
	inF >> grade;
       myStuds[cnt].letGr =  convertNumberToLetterGrade(grade);

    ++cntr;
}

This part is wrong :

inF >> id;
while (!inF.eof())
	{
		cnt++;
		myStuds[cnt].id = id;
		myStuds[cnt].fname = fname;
		myStuds[cnt].lname = lname;
		for (int i = 1; i < 5; i++)
			inF >> myStuds[cnt].gr[i];
		myStuds[cnt].avg = 0.0;
		myStuds[cnt].letGr = 'Y';
		inF >> id;
	}

I assume you wan't something like this :

while(!inF.eof()){
	inF >> myStuds[cnt].id;
	inF >> myStuds[cnt].fname;
	inF >> myStuds[cnt].lname;
	for (int i = 0; i < 6; i++)
		inF >> myStuds[cnt].gr[i];
	inF >> myStuds[cnt].avg;
        int grade = 0;
	inF >> grade;
       myStuds[cnt].letGr =  convertNumberToLetterGrade(grade);

    ++cntr;
}

do I still need to initialize the cnt++ for every loop this takes though...I don't know if you left that out of purpose or if you just glanced over it. Otherwise the cnt would always be 0, since it was initialized at that value and never incremented...right???

His line 12 should have been ++cnt; Though I agree with a previous poster that you have 8 grades, and do not have an average or a letter grade in your input file, which makes his sample code flawed as it tries to read the average and grade.

His line 12 should have been ++cnt; Though I agree with a previous poster that you have 8 grades, and do not have an average or a letter grade in your input file, which makes his sample code flawed as it tries to read the average and grade.

ok....I will add that in. Yeah, the avg and the letter grade were (and are ) going to be functions that I'm going to incorporate in the very near future, but i just wanted to see if I was on the right track with inputting everything before I got in too deep...

Ok....so this is what I have, but it's still error-ing out when I try to compile it....any further suggestions?

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;

struct student
{
	int id;
	string fname;
	string lname;
	int gr[5];
	double avg;
	char letGr;
};

void print(student arr[], int c)
{
	cout << endl;
	for (int i=1;1<=c;i++)
	{
		cout << setw(2) << i << ' ' << left << setw(10) << arr[i].id
			<< arr[i].fname << arr[i].lname << right;
		for (int j=1;j<=5;j++)
			cout << setw(5) << arr[i].gr[j];
		cout << setw(6) << arr[i].avg << setw(3) << arr[i].letGr << endl;
	}
	cout << endl;
}
double studAvg(student rec)
{
	int s=0;

	for (int i=1; i<=5; i++)
		s += rec.gr[i];
	return s / 5.;
}

double average(student arr[], int c, int i)
{
	double s=0;

	for (int j=1; j<=c; j++)
	{
		if (i > 0)
			s += arr[j].gr[i];
		else
			s += arr[j].avg;
	}
	return s/c;
}

char letterGrade(double sAvg, double cAvg)
{
	if (sAvg >= cAvg + 15) return 'A';
	else if (sAvg >= cAvg + 5) return 'B';
	else if (sAvg >= cAvg - 5) return 'C';
	else if (sAvg >= cAvg -15) return 'D';
	else return 'F';
}

void main()
{
	ifstream inF;
	student myStuds[20];
	int cnt=0;
	double classAvg;
	string fname, lname;

	cout << fixed << setprecision(1);

	inF.open("students.txt");

	while (!inF.eof())
	{
		inF >> myStuds[cnt].id;
		inF >> myStuds[cnt].fname;
		inF >> myStuds[cnt].lname;
		for (int i = 1; i < 5; i++)
			inF >> myStuds[cnt].gr[i];
		inF >> myStuds[cnt].avg;
		int grade = 0;
		classAvg = average(myStuds, cnt, 0);
		myStuds[cnt].letGr = letterGrade(myStuds[20].avg, classAvg);

		cnt++;
	}
	inF.close();
	cout << "Cnt = " << cnt << endl;
	print (myStuds, cnt);
}

if you're getting a compile error, would it trouble you too much to include the error?

if you're getting a compile error, would it trouble you too much to include the error?

I can't actually....it compiles correctly without error, but the error comes when the DOS-type screen comes up...once the screen comes up the program goes into a "students.exe has stopped working" mode....I don't know why

Well, if you're not reading from the file, where does the data automagically come from? Yes you need to fix this.

You're not reading the names from the input file. It seems you have 8 columns of grades. I'm assuming you need to calculate the average from the data, and I'm also assuming that you'd categorize the average into letter grades.

An array [I]type[/I] array[N] is indexed from 0 to N; element array[N] is outside array bounds.

Are you familiar with getline and stringstreams? And did you know it's not a good idea to use .eof() as a loop control?

To get it to work and be reasonably responsible with error checking, I changed roughly 70% of the code you posted.

[edit]My output for my "home version" is

0 45256  Rodrigues    Joana       58  75  58  61  59  75  63  92 67.625 D
 1 37915  Wright       Michelle    98  83  56  62  63  90  57  67 72     C
 2 81984  Williams     Jenny       55  67  54  63  89  84  93  75 72.5   C
 3 73984  Phaneuf      Lesley      78  85  57  51  68  94  51  83 70.875 C
 4 80886  Laflamme     Nicole      76  51  71  94  69  78  87  91 77.125 C
 5 39473  Kenyon       Patricia    65  54  90  68  94  70  95  97 79.125 C
 6 12127  McCabe       Kelly       51  96   0  64  54  75  71  94 63.125 D
 7 52458  Whitten      Sarah       99  58  94  82  81  75  82  70 80.125 B
 8 77921  Connors      Sarah       52  58  88  63  61  65  78  78 67.875 D
 9 28810  Navin        Joshua      94  75  62  93  64  92  87  94 82.625 B
10 31571  Toporowski   Crystal     93   0  77  77  63  68  88  58 65.5   D
11 33580  Ziolko       John        74  64  98  92  98  89   0  79 74.25  C
12 14508  Stronach     Kurt        80  95  96  84  78  86  53  59 78.875 C
13 44520  Ecklord      Ryan         0  61  56  86  98  98  59  83 67.625 D
14 15246  Berling      Danielle    85  64   0  75  69   0  54  85 54     F
15 16137  Littlefield  Arionna     71  85  74  97  69  64  82  95 79.625 C
16 62631  Niedojadlo   Evan        73  66  83  97  97  51  66  88 77.625 C
17 59640  Knieriem     Brandon     52  87  97  66  97  90  93  56 79.75  C
18 44102  Spence       Arthur      85  84 100  82  91   0  95  62 74.875 C
19 42331  Rose         Nicole      83  50  96  68  54  62   0  93 63.25  D

How in the heck are you getting it to output everything!?! I've been working on this and I still can't figure it out....

If you don't have a compile error, you can add debug print statements (or run in a debugger) to determine how far into your program it gets.

I'd at least add a print before you read the id with the record number you're on. And maybe a print of the first and last name to confirm you're staying aligned with your data. (I don't think you are in the code I see.)

How in the heck are you getting it to output everything!?! I've been working on this and I still can't figure it out....

As I mentioned, you need to first read the data from the file correctly. Print as you read and/or take my previous advice as well as that of others.

It makes no sense to try to process data you have not obtained.

As I mentioned, you need to first read the data from the file correctly. Print as you read and/or take my previous advice as well as that of others.

It makes no sense to try to process data you have not obtained.

well....I'm trying to take what others are saying, but there are a ton of different suggestions...the code I'm testing now is only giving me a blank cmd.exe type screen with a cursor that doesnt' move...

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;

struct student
{
	string id;
	string fname;
	string lname;
	int gr[5];
	double avg;
	char letGr;
};

void print(student arr[], int c)
{
	cout << endl;
	for (int i=1;1<=c;i++)
	{
		cout << setw(2) << i << ' ' << left << setw(10)<< arr[i].fname << arr[i].id 
			<< arr[i].lname << right;
		for (int j=1;j<=5;j++)
			cout << setw(5) << arr[i].gr[j];
		cout << setw(6) << arr[i].avg << setw(3) << arr[i].letGr << endl;
	}
	cout << endl;
}
double studAvg(student rec)
{
	int s=0;

	for (int i=1; i<=5; i++)
		s += rec.gr[i];
	return s / 5.;
}

double average(student arr[], int c, int i)
{
	double s=0;

	for (int j=1; j<=c; j++)
	{
		if (i > 0)
			s += arr[j].gr[i];
		else
			s += arr[j].avg;
	}
	return s/c;
}

char letterGrade(double sAvg, double cAvg)
{
	if (sAvg >= cAvg + 15) return 'A';
	else if (sAvg >= cAvg + 5) return 'B';
	else if (sAvg >= cAvg - 5) return 'C';
	else if (sAvg >= cAvg -15) return 'D';
	else return 'F';
}

void sort(student arr[], int c)
{
	student t;

	for (int i=c-1; i>=1; i--)
		for (int j=1; j <= i; j++)
			if (arr[j+1].fname < arr[j].fname)
			{
				t = arr[j]; arr[j] = arr[j+1]; arr[j+1] = t;
			}
}


void main()
{
	ifstream inF;
	student myStuds[50];
	int cnt=0;
	double classAvg;
	string fname, lname, id;

	cout << fixed << setprecision(1);

	inF.open("students.txt");

	inF >> id;
	while (!inF.eof())
	{
		cnt++;

		inF >> myStuds[cnt].id;
		inF >> myStuds[cnt].fname;
		inF >>myStuds[cnt].lname;
		for (int i = 1; i < 6; i++)
			inF >> myStuds[cnt].gr[i];
		inF >> myStuds[cnt].avg;
		inF >> myStuds[cnt].letGr;

		inF >> id;
	}
	inF.close();
	cout << "Cnt = " << cnt << endl;
	print(myStuds, cnt);
	sort(myStuds, cnt);
	classAvg = average(myStuds, cnt, 0);
	for (int i=1; i <= cnt; i++)
		myStuds[i].letGr = letterGrade(myStuds[i].avg, classAvg);
	print(myStuds, cnt);
	cout << endl;
}

I don't see anything obvious about why your code would just stop without exiting.

I guess your next step is to either run the program in the debugger (if you have one) or to add debug print statements to the code so you can see how far it gets.

well....I'm trying to take what others are saying, but there are a ton of different suggestions...the code I'm testing now is only giving me a blank cmd.exe type screen with a cursor that doesnt' move...

Your file reading loop has now become an infinite loop. This is because you don't match correctly the elements you are trying to read, putting the stream into an error state. You don't check for successfully reading data, so this is happily ignored and you have the infinite loop.

You need to have your input operations match the file, or you need to vastly improve your error handling (or preferably both). The fact that you've still got this line in your code

for (int i = 1; i < 6; i++)

tells me that you are ignoring much of what I have to say.

Your file reading loop has now become an infinite loop. This is because you don't match correctly the elements you are trying to read, putting the stream into an error state. You don't check for successfully reading data, so this is happily ignored and you have the infinite loop.

You need to have your input operations match the file, or you need to vastly improve your error handling (or preferably both). The fact that you've still got this line in your code

for (int i = 1; i < 6; i++)

tells me that you are ignoring much of what I have to say.

I thought I needed to use a loop to incorporate the five grades for each individual instead of identifying each element individually (i.e. Q1, Q2, etc.). I am trying to follow your words from my last post(s), where my code is roughly 12 or so pages long using Q1, Q2, etc. and each function takes only one grade column into consideration at a time....

It's not that I'm ignoring what you say, first off, perhaps it's the way that you're trying to teach me that might be the issue, DS. I have no problem following instruction...I want to LEARN how to do this, not just be handed the answer(s)....I am just not as skilled as you yet, so it might take me a little while longer to figure out how to do things....please be patient....Thanks

I thought I needed to use a loop to incorporate the five grades for each individual instead of identifying each element individually (i.e. Q1, Q2, etc.). I am trying to follow your words from my last post(s), where my code is roughly 12 or so pages long using Q1, Q2, etc. and each function takes only one grade column into consideration at a time....

It's not that I'm ignoring what you say, first off, perhaps it's the way that you're trying to teach me that might be the issue, DS. I have no problem following instruction...I want to LEARN how to do this, not just be handed the answer(s)....I am just not as skilled as you yet, so it might take me a little while longer to figure out how to do things....please be patient....Thanks

All righty then. Let's review...

Your attempt at reading the data from the file needs a lot of work. You're only trying to read the id and the grades, for example, but you pretend as if you've read the names. Also, you need to learn how to index arrays.

And ...

Well, if you're not reading from the file, where does the data automagically come from? Yes you need to fix this.

You're not reading the names from the input file. It seems you have 8 columns of grades. I'm assuming you need to calculate the average from the data, and I'm also assuming that you'd categorize the average into letter grades.

An array [I]type[/I] array[N] is indexed from 0 to N; element array[N] is outside array bounds.

Are you familiar with getline and stringstreams? And did you know it's not a good idea to use .eof() as a loop control?

To get it to work and be reasonably responsible with error checking, I changed roughly 70% of the code you posted.

Later you got some of reading from the file spoonfed to you by another poster, but didn't seem to want to take a poke at it yourself.

This part is wrong :

inF >> id;
while (!inF.eof())
	{
		cnt++;
		myStuds[cnt].id = id;
		myStuds[cnt].fname = fname;
		myStuds[cnt].lname = lname;
		for (int i = 1; i < 5; i++)
			inF >> myStuds[cnt].gr[i];
		myStuds[cnt].avg = 0.0;
		myStuds[cnt].letGr = 'Y';
		inF >> id;
	}

I assume you wan't something like this :

while(!inF.eof()){
	inF >> myStuds[cnt].id;
	inF >> myStuds[cnt].fname;
	inF >> myStuds[cnt].lname;
	for (int i = 0; i < 6; i++)
		inF >> myStuds[cnt].gr[i];
	inF >> myStuds[cnt].avg;
        int grade = 0;
	inF >> grade;
       myStuds[cnt].letGr =  convertNumberToLetterGrade(grade);

    ++cntr;
}

I also recommended looking into how to index an array. I posted a description. Another poster showed an example. Your latest version unincorporated those necessary and recommended changes.

You never answered my questions about getline and stringstreams, which might make reading the input easier. But I still can't read your mind as to how you answered this.

To me it seems like you are just waiting to be spoonfed a bit of working code that does the input correctly. That is where you should be focused, and you seem to be ignoring this part.

How many columns of data do you have? What type is each column (int, string?)? How do you obtain input of this column data from a file? Are you doing this? Show the code. If you need 8 integers, how does reading 5 into the middle of an array "solve" the problem?

Did you have any questions about the .eof() ? Do you know how to index an array yet (your code doesn't show that you do)?

How am I supposed to think you're doing anything but ignoring me when you don't answer my questions and make absolutely no discernible attempt to modify your code with my suggestions?

I have to agree with Dave...it almost looks like you aren't trying.

I took your last posted code and did the following:

  • modified it to support the number of grades you actually have
    (Which has been mentioned more than once.)
  • Used getline(stream, string) and istringstream to parse the input
  • Corrected array indexes to start from zero and to stop at size -1
    (something declared as int grade[5] has elements grade[0] through grade[4])
  • Changed the print function to keep data aligned and on one row
    (It wasn't as pretty before I did)
  • Actually called studAvg() to calculate the student's average grade

There might have been a little more, I didn't take notes as I fixed it, this was generated from memory.

I produced the following output:

0 Benway      32998 Eileen      76  98  80  69  75  94  77  95  83.0  B
 1 Berling     15246 Danielle    85  64   0  75  69   0  54  85  54.0  F
 2 Burr        50838 Jermiah     83  84  94  90  77  73  72  63  79.5  B
 3 Connors     77921 Sarah       52  58  88  63  61  65  78  78  67.9  C
 4 Cooper      24609 Camille     62  93  92  72  58  76  57  66  72.0  C
 5 Denaro      10364 Tony        73  85  55  72  68  91  55  89  73.5  C
 6 Ecklord     44520 Ryan         0  61  56  86  98  98  59  83  67.6  D
 7 Flores      58751 Jose        58  80   0  74  87  95  96  69  69.9  C
 8 Galotti     18140 Salvator    78  65  90  66  88  57  93  98  79.4  B
 9 Hepburn     85736 Spencer     71   0  62  60  52  62  64  51  52.8  F
10 Hildebrandt 43865 Stephenson  97  94  93  93  82  88  78 100  90.6  A
11 Houde       72054 Jessica     72  66  71  62  75  71  86 100  75.4  C
12 Kenyon      39473 Patricia    65  54  90  68  94  70  95  97  79.1  B
13 Knieriem    59640 Brandon     52  87  97  66  97  90  93  56  79.8  B
14 Laflamme    80886 Nicole      76  51  71  94  69  78  87  91  77.1  C
15 Littlefield 16137 Arionna     71  85  74  97  69  64  82  95  79.6  B
16 McCabe      12127 Kelly       51  96   0  64  54  75  71  94  63.1  D
17 Morang      87129 Nicholas    77  67  57  89  88  64  68  78  73.5  C
18 Navin       28810 Joshua      94  75  62  93  64  92  87  94  82.6  B
19 Niedojadlo  62631 Evan        73  66  83  97  97  51  66  88  77.6  C
20 Perlmutter  87361 Diadre      96  60  81  88  81  53  91  74  78.0  B
21 Phaneuf     73984 Lesley      78  85  57  51  68  94  51  83  70.9  C
22 Rodrigues   45256 Joana       58  75  58  61  59  75  63  92  67.6  D
23 Rose        42331 Nicole      83  50  96  68  54  62   0  93  63.3  D
24 Roy         21705 Jake        86   0  89  94  82   0  60  98  63.6  D
25 Spence      44102 Arthur      85  84 100  82  91   0  95  62  74.9  C
26 Stronach    14508 Kurt        80  95  96  84  78  86  53  59  78.9  B
27 Suslovic    78575 Vikilynn    65  54  74  67  61  76  69  76  67.8  C
28 Toporowski  31571 Crystal     93   0  77  77  63  68  88  58  65.5  D
29 Vasquez     99189 Oskar       66  60   0  75  63  92  92  52  62.5  D
30 Whitten     52458 Sarah       99  58  94  82  81  75  82  70  80.1  B
31 Williams    81984 Jenny       55  67  54  63  89  84  93  75  72.5  C
32 Wright      37915 Michelle    98  83  56  62  63  90  57  67  72.0  C
33 Ziolko      33580 John        74  64  98  92  98  89   0  79  74.3  C

If you don't like the list starting with zero, that's a pretty easy fix.
I don't know why you had the id between the 2 names, but I left it there.

So your concept appears to be sound, but there are some details to work out. As a general rule, we don't write your code for you, we help you to write it yourself. So start taking some of the suggestions, add them to your code and see what happens.

PS- As I mentioned before, if your program is not running like you think it should and you can't see what it is doing, either run it in a debugger or start adding debug print statements. I was doing some of both. I added some print statements like "opening file", "file open", "closing file", "sorting array" and the like. I also manually printed all of the data from a single record right after I read it so I could see that I read what I thought I was supposed to. (I commented that one out after I got your 'print them all' function to work like I wanted it to.)

All righty then. Let's review...
And ...

Later you got some of reading from the file spoonfed to you by another poster, but didn't seem to want to take a poke at it yourself.


I also recommended looking into how to index an array. I posted a description. Another poster showed an example. Your latest version unincorporated those necessary and recommended changes.

You never answered my questions about getline and stringstreams, which might make reading the input easier. But I still can't read your mind as to how you answered this.

To me it seems like you are just waiting to be spoonfed a bit of working code that does the input correctly. That is where you should be focused, and you seem to be ignoring this part.

How many columns of data do you have? What type is each column (int, string?)? How do you obtain input of this column data from a file? Are you doing this? Show the code. If you need 8 integers, how does reading 5 into the middle of an array "solve" the problem?

Did you have any questions about the .eof() ? Do you know how to index an array yet (your code doesn't show that you do)?

How am I supposed to think you're doing anything but ignoring me when you don't answer my questions and make absolutely no discernible attempt to modify your code with my suggestions?

First off, excellent use of the red highlighter! Secondly, I do not know how to use getline and stringstreams. As I understood it, and I can understand that I might be wrong, the

.eof()

would read until the end of the file, which seemed to be a plausible use. I have adjusted my information going back to the very beginning, and took another look at the situation, and made some of the corrections that you, and others have suggested. However, it goes straight to the "Press Any Key to Continue", which I will assume means that it is finally reading it right. However, I'm still not seeing anything print out .

Here it is:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;

struct student
{
	int id;
	string fname;
	string lname;
	int gr[8];
	double avg;
	char letGr;
};

void print(student arr[], int cnt)
{
	cout << endl;
	for (int i=0;1<=cnt;i++)
	{
		cout << setw(2) << i << ' ' << left << setw(10)<< arr[i].fname << arr[i].id 
			<< arr[i].lname << right;
		for (int j=0;j<=5;j++)
			cout << setw(5) << arr[i].gr[j];
		cout << setw(6) << arr[i].avg << setw(3) << arr[i].letGr << endl;
	}
	cout << endl;
}
double studAvg(student rec)
{
	int s=0;

	for (int i=0; i<=8; i++)
		s += rec.gr[i];
	return s / 5.;
}

double average(student arr[], int cnt, int i)
{
	double s=0;

	for (int j=0; j<=cnt; j++)
	{
		if (i > 0)
			s += arr[j].gr[i];
		else
			s += arr[j].avg;
	}
	return s/cnt;
}

char letterGrade(double sAvg, double cAvg)
{
	if (sAvg >= cAvg + 15) return 'A';
	else if (sAvg >= cAvg + 5) return 'B';
	else if (sAvg >= cAvg - 5) return 'C';
	else if (sAvg >= cAvg -15) return 'D';
	else return 'F';
}

void sort(student arr[], int c)
{
	student t;

	for (int i=c-1; i>=1; i--)
		for (int j=1; j <= i; j++)
			if (arr[j+1].fname < arr[j].fname)
			{
				t = arr[j]; arr[j] = arr[j+1]; arr[j+1] = t;
			}
}


void main()
{
	ifstream inF;
	student myStuds[50];
	int cnt=0;
	string fname, lname, id;

	cout << fixed << setprecision(1);

	inF.open("students.txt");


	while (!inF.eof())
	{
		inF >> myStuds[cnt].id;
		inF >> myStuds[cnt].fname;
		inF >> myStuds[cnt].lname;
		for (int i = 0; i < 9; i++)
			inF >> myStuds[cnt].gr[i];
		inF >> myStuds[cnt].avg;
		int grade = 0;
		inF >> grade;
		myStuds[cnt].letGr = 'Y';


		cnt++;

	}
	inF.close();
	cout << "Cnt = " << cnt << endl;
	print (myStuds, cnt);
}

Also, please don't act like you are all high and mighty. I understand that you know more than me in this field (see I can do it too!), and I appreciate all your help, as well as the help of everyone else, but there is no reason for you to talk down to me as if I am an invalid. I am doing my best....

I have to agree with Dave...it almost looks like you aren't trying.

I took your last posted code and did the following:

  • modified it to support the number of grades you actually have
    (Which has been mentioned more than once.)
  • Used getline(stream, string) and istringstream to parse the input
  • Corrected array indexes to start from zero and to stop at size -1
    (something declared as int grade[5] has elements grade[0] through grade[4])
  • Changed the print function to keep data aligned and on one row
    (It wasn't as pretty before I did)
  • Actually called studAvg() to calculate the student's average grade

There might have been a little more, I didn't take notes as I fixed it, this was generated from memory.

I produced the following output:

0 Benway      32998 Eileen      76  98  80  69  75  94  77  95  83.0  B
 1 Berling     15246 Danielle    85  64   0  75  69   0  54  85  54.0  F
 2 Burr        50838 Jermiah     83  84  94  90  77  73  72  63  79.5  B
 3 Connors     77921 Sarah       52  58  88  63  61  65  78  78  67.9  C
 4 Cooper      24609 Camille     62  93  92  72  58  76  57  66  72.0  C
 5 Denaro      10364 Tony        73  85  55  72  68  91  55  89  73.5  C
 6 Ecklord     44520 Ryan         0  61  56  86  98  98  59  83  67.6  D
 7 Flores      58751 Jose        58  80   0  74  87  95  96  69  69.9  C
 8 Galotti     18140 Salvator    78  65  90  66  88  57  93  98  79.4  B
 9 Hepburn     85736 Spencer     71   0  62  60  52  62  64  51  52.8  F
10 Hildebrandt 43865 Stephenson  97  94  93  93  82  88  78 100  90.6  A
11 Houde       72054 Jessica     72  66  71  62  75  71  86 100  75.4  C
12 Kenyon      39473 Patricia    65  54  90  68  94  70  95  97  79.1  B
13 Knieriem    59640 Brandon     52  87  97  66  97  90  93  56  79.8  B
14 Laflamme    80886 Nicole      76  51  71  94  69  78  87  91  77.1  C
15 Littlefield 16137 Arionna     71  85  74  97  69  64  82  95  79.6  B
16 McCabe      12127 Kelly       51  96   0  64  54  75  71  94  63.1  D
17 Morang      87129 Nicholas    77  67  57  89  88  64  68  78  73.5  C
18 Navin       28810 Joshua      94  75  62  93  64  92  87  94  82.6  B
19 Niedojadlo  62631 Evan        73  66  83  97  97  51  66  88  77.6  C
20 Perlmutter  87361 Diadre      96  60  81  88  81  53  91  74  78.0  B
21 Phaneuf     73984 Lesley      78  85  57  51  68  94  51  83  70.9  C
22 Rodrigues   45256 Joana       58  75  58  61  59  75  63  92  67.6  D
23 Rose        42331 Nicole      83  50  96  68  54  62   0  93  63.3  D
24 Roy         21705 Jake        86   0  89  94  82   0  60  98  63.6  D
25 Spence      44102 Arthur      85  84 100  82  91   0  95  62  74.9  C
26 Stronach    14508 Kurt        80  95  96  84  78  86  53  59  78.9  B
27 Suslovic    78575 Vikilynn    65  54  74  67  61  76  69  76  67.8  C
28 Toporowski  31571 Crystal     93   0  77  77  63  68  88  58  65.5  D
29 Vasquez     99189 Oskar       66  60   0  75  63  92  92  52  62.5  D
30 Whitten     52458 Sarah       99  58  94  82  81  75  82  70  80.1  B
31 Williams    81984 Jenny       55  67  54  63  89  84  93  75  72.5  C
32 Wright      37915 Michelle    98  83  56  62  63  90  57  67  72.0  C
33 Ziolko      33580 John        74  64  98  92  98  89   0  79  74.3  C

If you don't like the list starting with zero, that's a pretty easy fix.
I don't know why you had the id between the 2 names, but I left it there.

So your concept appears to be sound, but there are some details to work out. As a general rule, we don't write your code for you, we help you to write it yourself. So start taking some of the suggestions, add them to your code and see what happens.

PS- As I mentioned before, if your program is not running like you think it should and you can't see what it is doing, either run it in a debugger or start adding debug print statements. I was doing some of both. I added some print statements like "opening file", "file open", "closing file", "sorting array" and the like. I also manually printed all of the data from a single record right after I read it so I could see that I read what I thought I was supposed to. (I commented that one out after I got your 'print them all' function to work like I wanted it to.)

Thank you... and I am glad that you agree with DS!

Please see the new code that I have posted, as I have listed the changes that I have made thanks to you and DS. However, as I mentioned before, I am now getting a "Press any Key to Continue" message, which I assume means that it is at least (and finally) reading the data right, but not my print functions.

Specific comments on your code posting:

You still have not added any debug prints to track your way through your program. From your comments about how your program runs, I'm presuming that you either don't have access to or don't know how to use a debugger.

Line 25, 35 and 93 should all start at index 0 and stop BEFORE 8

Something like:

for (int ii = 0; ii < 8; ii++)

Style comment: I always use at least doubled letters for my for loop indexes, it makes them LOTS easier to see and search for.

Second style comment: I used a define in place of the 8 in your code. That way if it ever had to change it to say to 9 or 10, it would be easy and all the uses of it would remain consistent.

Lines 95 and 97 are attempting to read data that does not exist in the data file.

Line 21 has a typo that should produce an infinite loop if you have any records

It currently reads:
for the integer i starting from zero, continuing while one is less than the count, incrementing i each pass

it should read:
for the integer i starting from zero, continuing while i is less than the count, incrementing i each pass

First off, excellent use of the red highlighter! Secondly, I do not know how to use getline and stringstreams.

With stringstreams, you might have been able to read a line and not care about getting every field. Without using one, you've got to make damn sure that you match the input with the file itself.

Murtan has already described the array and reading non-existent values that you need to fix.

As I understood it, and I can understand that I might be wrong, the

.eof()

would read until the end of the file, which seemed to be a plausible use.

It would seem plausible, but it requires much more of an effort than you show in your code to do it correctly. Until then...
Avoid Loop Control Using eof()

Also, please don't act like you are all high and mighty. I understand that you know more than me in this field (see I can do it too!), and I appreciate all your help, as well as the help of everyone else, but there is no reason for you to talk down to me as if I am an invalid. I am doing my best....

It's up to you how you choose to take my advice.

I have had a consistent theme throughout:

As I mentioned, you need to first read the data from the file correctly. Print as you read and/or take my previous advice as well as that of others.

It makes no sense to try to process data you have not obtained.

The "print as you read" part has been mentioned multiple times as well. Although you may find this excessive, had you done so, you might have discovered most of the bugs in input by yourself long ago.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.