Correct my teacher

Pages: 12
Nov 3, 2012 at 8:13pm
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.
  

Nov 3, 2012 at 8:23pm
lol,
Help me fix it so that I can finish my homework.
Nov 3, 2012 at 8:24pm
Kind of hard to do my homework if the teacher's example was wrong....
Nov 3, 2012 at 8:26pm
what's the homework?

Correct the program?
Last edited on Nov 3, 2012 at 8:26pm
Nov 3, 2012 at 8:26pm
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?
Nov 3, 2012 at 8:27pm
35 4
42 4
27 3
36 5
Nov 3, 2012 at 8:30pm
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).
Nov 3, 2012 at 8:34pm
looks like you don't have the file "dropout115.txt in the right location
Nov 3, 2012 at 8:35pm
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 Nov 3, 2012 at 8:37pm
Nov 3, 2012 at 8:37pm
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.
Nov 3, 2012 at 8:39pm
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"?
Nov 3, 2012 at 8:44pm
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.
Nov 3, 2012 at 8:46pm
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 Nov 3, 2012 at 8:46pm
Nov 3, 2012 at 8:47pm
Nope I'm pretty sure I only put one txt file in the folder with it called dropout115.
Nov 3, 2012 at 8:47pm
Is anyone trying this on a linux based machine?
Nov 3, 2012 at 8:50pm
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 Nov 3, 2012 at 8:51pm
Nov 3, 2012 at 8:53pm
Nope still getting the same output.
Nov 3, 2012 at 8:54pm
k, I'm pulling up my Backtrack VM right now, going to put codeblocks on there and try it also, give me a few.
Nov 3, 2012 at 8:56pm
What does -nan mean?
Nov 3, 2012 at 8:58pm
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