Correct my teacher

Pages: 12
This is a copy of my teacher's program that won't run when I compile it. Help me fix it so that I can finish my homework.

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
// Lab12P1.cpp – Read dropout data from file, calculate dropout rate

#include <iostream>
#include <fstream>

using namespace std;

int main( )
{
	double numStudents = 0.0;
	double numDropouts = 0.0;
	double dropoutRate = 0.0;

	ifstream dropout;
	dropout.open("dropout115.txt", ios::in);

	if (dropout.is_open())
	{
		for (int count = 0; count < 4; count = count + 1)
		{
			dropout>>numStudents>>numDropouts;

			dropoutRate = numDropouts / numStudents;
			cout << "Dropout rate from section " << count+1 << ": " <<
dropoutRate << endl;
		}
		dropout.close();
	}
	else
	{
		cout << "The file could not be opened." << endl;
	}

	return 0;
}



Dropout rate from section 1: -nan
Dropout rate from section 2: -nan
Dropout rate from section 3: -nan
Dropout rate from section 4: -nan

Process returned 0 (0x0)   execution time : 0.002 s
Press ENTER to continue.
  

lol,
Help me fix it so that I can finish my homework.
Kind of hard to do my homework if the teacher's example was wrong....
what's the homework?

Correct the program?
Last edited on
It won't run for me because I don't have the input file "dropout115.txt".
Do you have sample input data for testing purposes?
35 4
42 4
27 3
36 5
The program is to read the file dropout115.txt and display the information in a terminal console. The first number is the number of students (numStudents) and the 2nd number is the number of dropouts (numDropouts).
looks like you don't have the file "dropout115.txt in the right location
it's giving me the proper output, what are you using to compile?

Well, when I ran it with "dropout115.txt" having no data, it returned the same values you are outputting. Once I put in the sample data

35 4
42 4
27 3
36 5

it gave me the correct output.
Last edited on
I'm using codeblocks. I have the txt file in the same folder as the cpp file. Is that wrong, its worked for all my other programs.
well you're not triggering the
cout << "The file could not be opened." << endl;
so it seems like your file is opening, you have that sample data in the "dropout115.txt"?
yes, its just like it is on the first post. I am doing this in ubuntu. Could that have anything to do with it, I'm a new ubuntu user.
If the file is in the wrong location, you will get the error message, "The file could not be opened."

Perhaps there are several different copies of the input file, containing different data?

With the correct file, the program works ok for me.
Last edited on
Nope I'm pretty sure I only put one txt file in the folder with it called dropout115.
Is anyone trying this on a linux based machine?
I am now. Was doing it on a VM with Visual Studios and it worked great. I then tried it on Xcode on my Mac and found the possible issue. Put in the full location of the file "dropout115.txt".

ie. "/Documents/Cpp/ReadFrom/dropout115.txt"
 
dropout.open("/Users/Documents/Cpp/ReadFrom/dropout115.txt", ios::in);
Last edited on
Nope still getting the same output.
k, I'm pulling up my Backtrack VM right now, going to put codeblocks on there and try it also, give me a few.
What does -nan mean?
lol, I'm a bit of a Cpp newb, I have no idea. I just know that I got -nan as an output when I had no data in the "dropbout115.txt".
Pages: 12