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?

10
  • 1
    check your code vs. the book again... Commented Dec 6, 2014 at 2:38
  • also if you have a book, it should explain it pretty clearly Commented Dec 6, 2014 at 2: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) Commented Dec 6, 2014 at 2: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)? Commented Dec 6, 2014 at 2:47
  • inputFile >> numbers[6] <- what are you expecting that to do? " how do I input the second file?" Same way you input the first one. Commented Dec 6, 2014 at 2:51

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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

9 Comments

You should not be looping on eof(): stackoverflow.com/questions/5605125/… Also getline() doesn't work like that.
This code gives me "no instance of overloaded function" error at inputFile.getline().
@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.
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. stackoverflow.com/questions/388242/…
Ok so what I am getting now is: numbers[0] = -858993460, numbers[1] = -858993460, etc.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.