So my professor gives us a code to use for our homework with arrays, and I understand arrays well enough, but how the code he gave us works is another story. From what I can tell, it is suppose to pull data from another file he gave us.
I assumed it would work correctly but I am not sure if I broke something or not because the issue that I have isolated is limited to the num int variable. It remains 0 the entire time while running step by step in the debugger which leads me to believe that it is either because it is not loading the filing properly or not translating it to the variable. Any research or help I try to get normally is limited to either open or c_str, and the understanding I got about them is limited.
I ran the test that is in my textbook to check if the file opened and while debugging it confirmed the file opened, so my only guess is there is a problem with either fin or the file opening line.
I hope I explained in this in a clear enough fashion. Thank you for your time.
This is half of the script mainly pertaining to the num variable never changing and the few tests I have done.
#include <iostream>
#include <iomanip>
#include <fstream>
usingnamespace std;
void main()
{
string dataFile = "ExamScores.txt";
ifstream fin;
int num, sum, ns;
double a; //average
//************************************************
// Read scores from a file and compute average
//************************************************
fin.open( dataFile.c_str() );
if (fin);
{ cout<< "confirm";
}
sum=0;
ns=0; //number of data values read
num = 0;
fin>>num; //read first data value
while( !fin.eof() )
{
++ns; //count after read & before eof
cout<<num<<" ";
sum+=num;
fin>>num; //read next data value
}
cout<<endl;
Edit: Scratch that, I am just a moron. I didn't realize Visual Studio made a folder inside the directory for the project which meant my text file wasn't in the same directory like I thought it was causing it to not open. I checked this earlier but I didn't realize my project was seperated when I made it.
Your professor deftly handled the EOF issue, but he still shouldn't be looping on it.
33 34 35 36 37 38
while (fin>>num)
{
cout<<num<<" ";
++ns;
sum+=num;
}
JSYK.
Hmm, after typing this up I realized that I just read cire's answer and totally forgot he just said this.
Also, I meant to say that debuggers often change the internal behavior of your program, so when you step through and find nothing happening to a variable you must consider that the debugger might be doing something to it...