Edit2: Turns out I did not define the value of i to 1 when writing the loop, Oops. Code works flawlessly now. Thanks for the help!
I've been scratching my head as this is supposed to be simple, yet I'm dumbfounded because of an arbitrary issue that literally makes 0 sense whatsoever.
Although I'm learning C++ and my knowledge isn't great yet but I can research and fix problems I'm having with my code. No matter how much I've looked online, I cannot find a single issue that comes close to this. The code compiles fine and technically should work, and it does work. Except it works 1/4 times.
The code is supposed to read the contents of U1.txt and create then output to a file U1rez.txt
The output is supposed to look like this:
10000 g
3 backpacks
Yet sometimes it looks like this:
10000 g
0 backpacks
Or this:
0 g
0 backpacks
Hell sometimes it even hangs up and I'm forced to close the damn program.
Bellow is the cursed code:
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;
int main ()
{
int number, //First number, How many Backpacks are there
backpack=0, //Chosen Backpack
heaviest=0, //Heaviest Backpack
lighter=0 //How many backpacks that are more than 2 times lighter than the heaviest one.
;
ifstream file("U1.txt");
file >> number; //reads the number of Backpacks
//cout << number; //Debug
for (int i; i<=number; i++){ //Loop, choosing the heaviest Backpack
// cout << "\nLoop Start "; //Debug
file >> backpack; //Reads the weight of the choosen backpack
// cout << backpack; //Debug
if(backpack>=heaviest) heaviest = backpack; //If choosen backpack is heavier than before, writes it heaviest.
// cout << " " << backpack << "\n"; //Debug
}
file.clear();
file.seekg(0, ios::beg);//Starts reading the file from start
file >> number; //Again reads the number of backpacks, idk how to avoid this.
for (int i; i<=number; i++){ //Another loop deterring which backpack more than 2 times lighter than the heaviest one
file >> backpack; //reads the backpack weight
if(backpack*2<=heaviest) lighter=lighter+1; //If the choosen backpack is ligher than the heaviest one when its weight is multipied by two, it gets recorded.
//cout <<"\n" << lighter; //Debug
}
file.close();
ofstream result("U1rez.txt");
result << heaviest <<" g\n"<< lighter <<" Backpacks are more than 2 times lighter than the heaviest one";
cout << heaviest <<" g\n"<< lighter <<" Backpacks are more than 2 times lighter than the heaviest one";
result.close();
return 0;
}
Contents of U1.txt:
6
5000
4500
5500
3500
10000
5650
If anyone has time, could they please help me out and tell me why this is happening? Could this be a compiler issue? Since I'm using Ubuntu 22.04 and it includes a GNU GCC compiler by default. I've tried compiling from the terminal to see if it was the IDE and still got the same results. I've tried compiling other programs that I've made yet they run fine without error.
Edit: Changed the variables for easier readability to:
x = numbers
tk = backpack
k = heaviest
l = lighter
p = file
r = result