0

I have two files, and I am supposed to input both of them into arrays. One of them is:

2
3
4
5
7
8

The second one is

2
4
5
6
7
8
2
3
4
5
7
8

(it's much longer, but it doesn't matter). I need to have one array with the first 6 numbers, and then I am supposed to check if first six numbers from the second file are the same as the numbers in the second array, same with the next six numbers and so on (like checking for a lottery winner). I guess that I am supposed to load numbers from one file into multiple arrays, but I have no idea how to do it, and I can't find it anywhere.

The code for the first array that I have so far is:

#include<iostream>
#include<fstream>
using namespace std;
int main(){
int numbers[6];
int count = 0;
ifstream inputFile;
inputFile.open("Numbers.txt");
while (count < 20 && inputFile >> numbers[6]){
    count++;
    inputFile.close();
    for (count=0; count < 20; count++)
    cout << numbers[count];}
    return 0;
    }

Another problem is that instead of displaying the numbers correctly, it displays "-858993460" 6 times - even though my code is basically copied from a book...

What is wrong with my code, and how do I input the second file?

bobtheboy
  • 386
  • 10
  • 24
krmtb
  • 11
  • 1
  • 5
  • 1
    check your code vs. the book again... – bobtheboy Dec 06 '14 at 02:38
  • also if you have a book, it should explain it pretty clearly – bobtheboy Dec 06 '14 at 02:39
  • I would also recommend you to instead use Rd(data.in) for the input file. Also please put the exact code out so that we can explain it to you if you still do not understand it. Also, btw, for the second file I am guessing that you input it by making a new file? (not sure) – bobtheboy Dec 06 '14 at 02:44
  • There isn't much explained, just this code, I went through it character by character and I don't see any difference (of course besides the name of the file, but that is surely correct). No idea what's wrong. And what do you mean by Rd(data.in)? – krmtb Dec 06 '14 at 02:47
  • `inputFile >> numbers[6]` – Galik Dec 06 '14 at 02:51
  • well it was supposed to be numbers[count] - but it doesn't change anything, I'm still getting 858993460 – krmtb Dec 06 '14 at 02:57
  • OK. Well first off you close the input file after reading the first number. – Galik Dec 06 '14 at 03:04
  • I don't understand... you mean that I close the input File after reading the first number and I shouldn't do it, or I don't close it and I should do it? I have no idea what I should change in the code. – krmtb Dec 06 '14 at 03:08
  • You do this in your loop: `inputFile.close();`. It closes the file you are trying to read.You should do that ***after*** the loop. – Galik Dec 06 '14 at 03:11
  • I tried putting "inputFile.close()" after "cout << numbers[count]", but it doesn't change anything. I tried making various changes, the code that I posted is exactly what I have in my textbook... – krmtb Dec 06 '14 at 03:15

2 Answers2

1

Your main problem is doing stuff inside your loop that should not be there. The loop is supposed to run once for each value it reads in from the file. After it is finished is when you should close the file and print the results.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int numbers[6];
    int count = 0;

    ifstream inputFile;
    inputFile.open("Numbers.txt");

    if(!inputFile.is_open()) // always check for errors
    {
        std::cerr << "ERROR opening input file:" << std::endl;
        return 1; // error
    }

    // make sure count < 6 so you don't overflow your array
    while(count < 6 && inputFile >> numbers[count])
    {
        count++;
        // inputFile.close(); // don't close the file yet!!
        //for(count = 0; count < 20; count++) // don't output yet!!
        //  cout << numbers[count];
    }

    // now close your file and output what you have

    inputFile.close();

    for(count = 0; count < 6 /* not 20!! */; count++) // don't output yet!!
        cout << numbers[count] << '\n';

    return 0;
}
Galik
  • 42,526
  • 3
  • 76
  • 100
0

This is an example of reading numbers from your first file. This example can be added to, to read numbers from your second file and compare them with the numbers read from the first file.

Updated:

#include <iostream>
#include <fstream>

using namespace std;

int main() {
    int numbers[6];
    int count = 0;
    ifstream inputFile;

    inputFile.open("Numbers.txt");

    // get each number from the file until the end-of-file bit
    // is retrieved.
    while ((inputFile >> numbers[count]) && (count < 6)){
        // iterate count by one
        count++;
    }

    inputFile.close();

    // run through the array of numbers and ouput each index
    for(int i = 0; i < 6; i++) {
        cout << "numbers["  << i  << "] = " << numbers[i] << "\n";
    }
}

Input: "Numbers.txt"

2
3
4
5
7
8

Output: cout

numbers[0] = 2
numbers[1] = 3
numbers[2] = 4
numbers[3] = 5
numbers[4] = 7
numbers[5] = 8

Update:

Faulty Input:

d
3
4
5
7
8

Output

numbers[0] = 0
numbers[1] = 0
numbers[2] = -1527900896
numbers[3] = 32627
numbers[4] = -1527901752
numbers[5] = 32627
Jonny Henly
  • 3,725
  • 4
  • 23
  • 41
  • You should not be looping on `eof()`: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong Also `getline()` doesn't work like that. – Galik Dec 06 '14 at 02:54
  • This code gives me "no instance of overloaded function" error at inputFile.getline(). – krmtb Dec 06 '14 at 03:05
  • @Galik never knew that about `eof()`, my professor in school even taught us to test for eof in the while loop, I should probably ask from my money back. The `getline()` mistake is from working with the java `Scanner` class recently. – Jonny Henly Dec 06 '14 at 03:11
  • From what I have seen there appears to b a *lot* of professors in college who are teaching bad practices. I strongly advise you get some *recommended* reading. Do what your professor tells you *for him* but learn how to do things **properly** *from experts*. http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Galik Dec 06 '14 at 03:15
  • Ok so what I am getting now is: numbers[0] = -858993460, numbers[1] = -858993460, etc. – krmtb Dec 06 '14 at 03:30
  • Maybe it's a matter of the compiler? I'm using Windows Visual Studio 2010 – krmtb Dec 06 '14 at 03:31
  • @krmtb the contents of your "Numbers.txt" must be off. Check and make sure that it contains only 6 numbers and each number is on it's own line or separated from the other numbers with a space. – Jonny Henly Dec 06 '14 at 03:43
  • Hehe, you should check the count ***before*** you read in the value... ;) – Galik Dec 06 '14 at 03:54
  • Yes, the .txt file is off, it contains the exactly 6 numbers, each of them in a seperate line, still getting -858993460. Actually I am getting -858993460 no matter if I put numbers, letters or anything else in the .txt file... – krmtb Dec 06 '14 at 04:20