x=(total/20);
cout<<"Distance of first jump <="<<x<<" "<<":"<<" ";
cin>>n;
cout<<endl;
if (n<=x)
{
while (counter<=total)
}
else
cout<<"incorrect first jump distance, setting first jump distance to: "<<x<<endl;
return 0;
}]
1) Input a positive integer total that represents the total distance in meters a person needs to travel.
2) Input a positive integer first that represents the distance of the first jump. This distance should be less than or equal to total/20. If first is greater than total/20 then set it to total/20.
3) Using a loop, calculate how many jumps are needed to cover the total distance. After every jump, the person jumps 1 meter more than the previous jump.
#include <iostream>
int main(){
int total, jump;
std::cout << "Enter the total distance required to travel: ";
std::cin >> total;
std::cout << "\nEnter the distance that represents the first jump: ";
std::cin >> jump;
if(jump > total/20)
jump = total/20;
for (int i = 0; jump + i <= total; i++)
std::cout << ""; //Sorry about this but I'm writing it quick
std::cout << "You need " << i << " jumps to cover the total distance entered";
return 0;
}
Here you go. Just use the for loop (I haven't tested this so I'm not 100% sure it works, just 99.9%)
when i run the program it says that there is proplem in line 17
which is ((( error: name lookup of 'i' changed for ISO 'for' scoping ))
can u solve it plzzzz
Variables declared within the loop do not exist outside of the loop. Older C++ had longer lifetime.
This:
1 2 3 4
for (int i = 0; jump + i <= total; ++i)
std::cout << "";
std::cout << i; // error
is same as this:
1 2 3 4 5 6
for (int i = 0; jump + i <= total; ++i)
{
std::cout << "";
}
std::cout << i; // error
is same as this:
1 2 3 4 5 6 7 8 9
{
int i = 0;
for ( ; jump + i <= total; ++i)
{
std::cout << "";
}
} // end of scope, where the i exists
// there is no i any more
std::cout << i; // error
Therefore:
1 2 3 4 5 6 7
int i = 0;
for ( ; jump + i <= total; ++i)
{
std::cout << "";
}
std::cout << i; // ok