Using Substrings for a mileage calculator

Hi,
I am trying to make a mileage calculator which will figure out whether or not there is enough gas in the car to reach your destination. The only problem is that we are not supposed to use if statements. It says that we should first subtract desired distance from how many miles the car can actually go, which I did. Then it says to set the value of a second variable n to 1 if x is greater or equal to 0 (meaning the user hasn't used more gas than is in the tank), and set n to 0 if x is less than 0. I know how to do this using if statements, but we are not supposed to have learned them yet, so can anyone give me a hint to an easier way to do this?
Thanks!
Last edited on
int n=x>=0;
or
int n=x>=0 ? 1 : 0;

However if you haven't even covered if, it seems unlikely that you've covered bool->int conversions or the ternary operator.
Yes I dont know how to use the second one. But I tried the first one, and dont really understand what is going on. We are supposed to set the value of n to 1 if x >= 0, and set n to 0 if x < 0. Then if we can do that, we are supposed to input the following code that has been provided to us:
string answer = " not ";
cout << "You will" + answer.substr(0, 5 - 4 * n) + "make it";

But I don't understand how to tell the program to set it under those circumstances.

I hope you can understand my question :S I am bad at phrasing things :)
Thanks
Yes I dont know how to use the second one. But I tried the first one, and dont really understand what is going on.

That makes it likely that you aren't allowed to use the first one either.
What's going on is that x>=0 evaluates to either true or false. When converting a bool to int (which is done implicitly here), true converts to 1 and false converts to 0.

Last edited on
Oh ok I see.. So how do we get that to print?
Get what to print?
closed account (1vRz3TCk)
1
2
3
4
5
6
7
8
int main()
{
    int miles_to_go = 150;
    int will_go = 145;
    int n = (( will_go - miles_to_go) >= 0);
    string answer = " not "; 
    cout << "You will" + answer.substr(0, 5 - 4 * n) + "make it";
}
Topic archived. No new replies allowed.