Please, I know this is a simple question. I'm copying this code out of my text book, almost ver-mother#&$(ing-batm And it mother#&$(ing refueses, REFUSES to open the mother#&$(ing file and write to the mother#&$(ing array. WTF - Sorry for the language, but this is so stupid and silly and I have NO mother#&$(ing clue why this isn't working. I've spent more time than I want to admit publicly on this problem PLEASE HELP!

What's happening is ifstream is doing God-knows-what, and then it creates an array and that array if filled with what ever crap was in that memory location. There is no communication betwixt ifstream and the array. Please, please, please ket me know what I am doing wrong. I'm newish to programming, but this is right out of my book.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    const int len = 8;
    int numsRead[len];
    int count = 0;
    ifstream inputFile;


   inputFile.open("eight.txt");

    while(count < len && inputFile >> numsRead[len])
        count++;

    inputFile.close();

        cout << numsRead[1] << " " << endl;

return 0;
}

Recommended Answers

All 3 Replies

Show the contents of eight.txt.

The problem lies in:

while(count < len && inputFile >> numsRead[len])
    count++;

You're constantly trying to read to numsRead[8] as len = 8, which is out of bounds for the array.
You need to read to numsRead[count]

You should also check as to whether the file is successfully opened using is_open()

THANK YOU! Works perfect, and I will definately use is the is_open(), so helpful guys! Really appreciate it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.