.c_str() and fstream issue

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.

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
37
38
39
40
41
42
43
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace 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;

Last edited on
if (fin);

in line 20 is equivalent to

1
2
 if (fin)
     ;


So line 22 always prints out "confirm"

I believe you wanted:

1
2
if (fin)
    cout << "confirm" ;


You shouldn't be checking for the file reaching eof.

1
2
3
4
5
6
while ( fin >> num )
{
    ++ns ;
    cout << num << " " ;
    sum += num ;
}
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.

Sorry for wasting time.
Last edited on
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...

Hope this helps.
Topic archived. No new replies allowed.