// Lab12P1.cpp – Read dropout data from file, calculate dropout rate
#include <iostream>
#include <fstream>
usingnamespace 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.
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).
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"?
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".