-2

When I run this code it doesn't print the contents of the .txt file which is numbers 1 to 100, it prints all of the even numbers up to 100 (e.g. 2 4 6 8 so on.) And I don't know why, it didn't before and I don't think I changed anything. I'm using xcode. Anybody got any ideas?

#include <stdio.h>     
#include <iostream>     
#include <cmath>        
#include <string>       
#include <sstream>      
#include <fstream>


using namespace std;

int main () {
    string line;
    int Points[100];
    ifstream myfile("StatNum.txt");
    if (myfile.is_open())
    {
        while ( getline (myfile,line) )
        {
            getline(myfile,line);
            stringstream(line) >> Points[100];  //uses stringstream to convert Myline (which is a string) into a number and put it into an index of Points
            cout << Points[100] << endl;
        }
        myfile.close();
    }

    else cout << "Unable to open file" << endl;

    return 0;
}
  • You should go through your code line by line and explain each one to your [rubber duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging). The issue will become obvious. – Baum mit Augen Oct 23 '16 at 17:46
  • 1
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Oct 23 '16 at 17:48
  • line collected by `while ( getline (myfile,line) )` is simply thrown away/unused. Then you move to next line(even line num). – Saurav Sahu Oct 23 '16 at 18:01

2 Answers2

1

This happens because you call getline twice per iteration:

  • First, you call it in the while header
  • Then you call it inside the loop.

One invocation (the one in the while header) is sufficient, because the result is saved in the line variable, which the loop body is free to examine.

Removing the second invocation will fix the problem.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

As @dasblinkenlight pointed out, you are calling std::getline() twice and that's the problem that you see.

The problem that you can't see is that you are writing data to Points[100] which is an invalid location, outside of the array's bounds. The 100 valid locations in your array are indexes 0 to 99, that is Points[0], Points[1], ..., Points[99] (because counting in C++ starts from 0, not 1).

Writing to Points[100] is Undefined Behavior which means that your program may crash, or worse: may not crash while corrupting its own data.

Since you're using C++ you have std::vector and other containers at your disposal, where you can easily store the numbers you read:

#include <vector>

// ...

vector<int> points;

while (getline(myfile, line))
{
    int temp;

    stringstream(line) >> temp;
    points.push_back(temp);
    cout << temp << endl;
}
Community
  • 1
  • 1
user7023624
  • 571
  • 5
  • 14