0

C++: "Transportation" Test

I don't know why this code won't work. (Assume that x=231. And please look carefully.) #include <iostream> using namespace std; int main() { //your code goes here int x; int y=50; int z=y-(x%y); cin>>x; if (x<=y){ cout<<y-x<<endl; } else{ cout<<y-(x%y); } return 0; } At first, I used the Z value in the "else" curly brackets, but then since that didn't work, I typed out the whole thing instead. It worked this time. So why didn't Z's value (which is the exact same thing) work with 231, but typing out its full value did? Edit: Sorry, okay, I was pretty confused at first as to what I had to do, so I ended up using if...else since that was the first thing that came to mind.

16th Apr 2021, 1:03 AM
Koloroj
2 Antworten
+ 4
1. You don't need the if else statement at at. 1 line is all you need after getting the number of passengers. 2. How do you expect this line to work; int z=y-(x%y); when you are getting the value for x after it and not before? FYI, the r-value of that statement is all you need to output.
16th Apr 2021, 2:34 AM
ChaoticDawg
ChaoticDawg - avatar
0
Koloroj Why you are writing if else even you can get solution in few lines. Just do int seats = 50 - passengers % 50; cout << seats; Where passengers is input.
16th Apr 2021, 3:10 AM
A͢J
A͢J - avatar