Problem lies in the order of operations inside while()
loop:
while (i != 1000)
{
++i;
num.push_back(i);
cout <<num[i]<<"\t"<< sqrt(num[i]) << "\n";
}
i
starts from 0. In each iteration, you push_back
an element and then print it using counter i
– after its incrementation. So, num[i]
refers to a non-yet-existing element.
Change your code to:
while (i < 1000)
{
num.push_back(i + 1);
cout <<num[i]<<"\t"<< sqrt(num[i]) << "\n";
++i;
}
1
solved an error i can’t seem to find [closed]