0
\$\begingroup\$

I'm writing a program that has to check which are the two largest three digit numbers that are missing from a text file. I'm writing this in C++ and I'm trying to solve the mystery in a while loop. How can I "move" the search back to the beggining of the text file? More explicit: if the checking is now around the middle of the text file and I have to check at this point of time again all the numbers from the beggining of the file, how can I do this? I'm attaching only the code part which contains the while.

    while(!in.eof()&&first_number>=100&&second_number>=100)
    {   in >> number_to_compare;

        if(first_number==number_to_compare)
        {   first_number--;
            if (first_number == second_number)
                first_number--;
        }
        else if(second_number==number_to_compare)
        {   second_number--;
            if (second_number == first_number)
                second_number--;
        }
    }

I would need in bouth of the mainif operators cases a line code that makes the search to start again from the beginning of the text file. I'm just starting to learn programming, I don't know any OOP. I tried also something similar with the logic of bubble sort algorithm (loop in loop) but I think that it's not working because the file reading will not start again from the beginning of the file for a new iteration of the main while ... so it's almost the same problem ... how can I make the program to start again to read the text file from the beginning? Here is the second attempt:

int need_to_search = 1;

    while (need_to_search) 
    {   need_to_search = 0;

        while (!in.eof() && first_number >= 100 && second_number >= 100)
        {   in >> number_to_compare;

            if (first_number == number_to_compare)
            {   first_number--;
                if (first_number == second_number)
                    first_number--;

                need_to_search = 1;
            }
            else if (second_number == number_to_compare)
            {   second_number--;
                if (second_number == first_number)
                    second_number--;

                need_to_search = 1;
            }
        }
    }

Example: If the file would contain the folowing numbers:

1292 5422 997 125 7735 1 5474 956 7557 1249 966 624 12 5499 5677 884 964 998 546 455 778 6658 957

The output should be:

999 996

because these are the largest two numbers of three digits that are missing from the text file.

I would really appreciate a hint! Thanks!

\$\endgroup\$

closed as off-topic by Jerry Coffin, Mathias Ettinger, mdfst13, Tunaki, Quill May 13 '16 at 10:38

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – Jerry Coffin, Mathias Ettinger, mdfst13, Tunaki, Quill
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    \$\begingroup\$ First things first: pick a language! Do you want C or C++? They look similar and some constructs work in both languages, but that doesn't make them identical. They're really, really different languages. \$\endgroup\$ – Mast May 12 '16 at 21:59
  • \$\begingroup\$ I know what you are saying... I'm writing in the C++ that I have learned until now. I heard that until this stage what I'm using (like other beginners) is a C with cout and cin...this is what I know so far. It's how students usually start to learn C++ in highschool ... looks more like C code. \$\endgroup\$ – CCBet May 12 '16 at 22:05
  • \$\begingroup\$ @David Foerster, I added a Input/Output example. Thanks! \$\endgroup\$ – CCBet May 12 '16 at 22:40
  • 1
    \$\begingroup\$ @Skyp89, he meant a code :) Just show us what happens inside the main function, which is not touching your algorithm. For example calling the function you wrote, or creating an instance of data structure and illustrating what it can do. Simplified: show us the full code that you used to get output you posted. \$\endgroup\$ – Incomputable May 12 '16 at 22:54
  • 2
    \$\begingroup\$ Why is iostream::eof inside a loop condition considered wrong? \$\endgroup\$ – Martin York May 12 '16 at 23:24
1
\$\begingroup\$

If you're interested in learning OOP and don't have the ambition to work with embedded electronics (roughly everything smaller than a phone), you're really more interested in C++ than C. This review is written based on the assumption you want to write C++ and ignores C.

Your current method of reading a file is odd. While checking for EOF is a relatively easy method to check whether you've hit the end of a file yet, it's not the only thing you should check for when reading a file. C++ can check whether the input is still 'good'. You could do so like this:

#include <iostream>
#include <string>
#include <fstream>
#include <vector>

void readFile()
{
    std::ifstream input("file.txt", std::ios::in);
    std::string currentLine;
    std::vector<std::string> allLines;
    std::vector<std::string>::iterator itLines;
    while (input.good())
    {
        getline(input, currentLine);
        allLines.push_back(currentLine);
    }
    for (itLines = allLines.begin(); itLines != allLines.end(); itLines++)
    {
        // Iterate over the data you just read
    }

}

Note that since we don't return anything, a void will do.

Now you're reading all lines of data (up till every newline) and iterate over them, which appears to be what you want to do. Not only will this stop when the file has reached EOF, it will also check whether the stream hasn't failed or took a bad chunk somewhere along the way.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.