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!
cout
andcin
...this is what I know so far. It's how students usually start to learn C++ in highschool ... looks more like C code. – Skyp89 May 12 at 22:05