-1

I've been stuck on this homework question for a couple of days now. I did some research and I got to this:

#include<iostream>
#include<fstream>
#include<string>
#include<array>
using namespace std;

const int MAX_SIZE = 100;

bool RepeatCheck(int storage[], int size, int checker);

int main()
{
    ifstream input("input.txt");

    if (!input) // if input file can't be opened
    {
        cout << "Can't open the input file successfuly." << endl;
        return 1;
    }

    int storage[MAX_SIZE];
    int inputCount;
    int checker;


    while (!input.eof())
    {
        for (int i = 0; i <= MAX_SIZE; i++)
        {
            input >> checker;
            if (!RepeatCheck(storage, MAX_SIZE, checker))
            {
                input >> storage[i];
            }

        }
    }

    // print array
    for (int i = 0; i < 100; i++)
    {
        cout << storage[i] << " ";
    }

    return 0;
}

bool RepeatCheck(int storage[], int size, int checker)
{
    for (int i = 0; i <= MAX_SIZE; i++)
    {
        if (storage[i] == checker)
        {
            return false;
        }
    }
}

My input file needs to be filled with integers separated by white space or on new lines:

1 2 2 3 5 6 5 4 3 20 34 5 7 5 2 4 6 3 3 4 5 7 6 7 8

I need to read the integers from the file and store them in the storage[] array only if they aren't already in the array.

It's not doing what I need it to do. Please take a look at my code and tell me where the logic error is. Much appreciated.

nzxt440
  • 1
  • 1
  • Since it is not doing what you want, what does it do? What output do you get? A debugger will greatly help, you want to use stepping to figure out where the logic is failing. – Nic3500 Dec 06 '17 at 03:34
  • [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301 Dec 06 '17 at 03:35

1 Answers1

0

If std::set is allowed, use it instead of doing your own duplication check.

For your code, the problem happens here:

while (!input.eof())
{
    //for loop not needed
    for (int i = 0; i <= MAX_SIZE; i++) //increment regardless whether input is successful or not?
    {
        input >> checker; //take in a number
        //if the number is not repeated
        if (!RepeatCheck(storage, MAX_SIZE, checker)) 
        {
            input >> storage[i]; //read in another number to put it?
        }

    }
}

Also here

bool RepeatCheck(int storage[], int size, int checker)
{
    for (int i = 0; i <= MAX_SIZE; i++) 
    //check all values in the array regardless whether it has been used or not?
    //should just check until the end of used space, which is what size is meant for
    {
        if (storage[i] == checker)
        {
            return false;
        }
    }
    //there should be a return true here right?
}

Have a look at this link to see why while (!iostream.eof()) is considered wrong.

The first part should look like this:

int i = 0;
while (input >> checker)
{
    if (!RepeatCheck(storage, i, checker))
    {
        storage[i] = checker;
        ++i;
    }
}

For second part, use size instead of max size

lamandy
  • 962
  • 1
  • 5
  • 13
  • Ohh I see so I would change input >> storage[i]; to storage[i] = checker;. – nzxt440 Dec 06 '17 at 03:30
  • That's will fix part of the problem. For the second part, you may or may not face the problem but you fix it for consistency as well. – lamandy Dec 06 '17 at 03:31
  • I'm unsure as to how to fix my RepeatCheck function to make it do what I want.. Any tips on how to only check up to the used index instead of all 100 indices? – nzxt440 Dec 06 '17 at 03:32
  • Also, can I PM you instead if I require further assistance? – nzxt440 Dec 06 '17 at 03:33
  • in your loop, use size instead. And when you call that function, the caller should provide how many positions have been used (not MAX_SIZE). Also , in the first part, the inner for loop is wrong. – lamandy Dec 06 '17 at 03:34
  • Now when I run the program it prints out garbage instead of the values that should be stored in the array – nzxt440 Dec 06 '17 at 03:43
  • I have added the fix at the end of my answer. – lamandy Dec 06 '17 at 03:44
  • Also for the final printing, just print until i, values after that will be garbage. – lamandy Dec 06 '17 at 03:45
  • Now for the final printing it won't print out the values that should be there. Instead, it prints out all garbage values starting from index 0 to 99. – nzxt440 Dec 06 '17 at 03:49
  • Instead of doing it with a lot of value, create a text file with just 1 number, see if it can go in, then add number to see different number can go in, then add duplicate number to see if it can be ignored, then only go with your actual test data. – lamandy Dec 06 '17 at 03:53
  • So I tested it the way you've suggested and it looks like not even one number in the input file is being stored in the array. I do not know why this is happening. The code you've provided makes sense to me and it should work.. – nzxt440 Dec 06 '17 at 03:56
  • Ahh, your repeatCheck is reversed, it return false when it is duplicate, return true when there is no duplicate – lamandy Dec 06 '17 at 04:01
  • Thank you so much for assistance. I can finally take a break after 5 hours of trying to do this question :) Have a good night/day. – nzxt440 Dec 06 '17 at 04:03