pls help me

Can you help me code this problem by using simple if-else statements? thank you!

Problem: A 25m Race Track is divided into 5m of Regular Soil, 4m of Rubberized Floor, 7m of Mud and 9m of Asphalt. Create a program that will accept the distance covered in meters from the starting line and outputs which part of the Race Track they will land.

Ex:

If input is 2m, they will land on Regular Soil.

If input is 8m, they will land on Rubberized Floor.

If input is 19m, they will land on Asphalt.

If input is 27m, they will land on Regular Soil





here is my code:
#include<iostream>
using namespace std;

main() {
int meters_covered;
cin>>meters_covered;

meters_covered = 25%10;

if(meters_covered >=0 && meters_covered <= 5)
cout<<"regular road";

else if(meters_covered >= 6|| meters_covered <= 9)
cout<<"rubberized";

else if(meters_covered >= 10 || meters_covered <= 16)
cout<<"mud";

else if(meters_covered >= 17|| meters_covered <= 25)
cout<<"asphalt";
}





please do help me for I am really excited to solve this problem :D thank you!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
using namespace std;

main() {
int meters_covered;
cin>>meters_covered;

meters_covered = 25%10;

if(meters_covered >=0 && meters_covered <= 5)
cout<<"regular road";
else
meters_covered -= 5;

else if(meters_covered >= 6|| meters_covered <= 4)
cout<<"rubberized";
else
meters_covered -= 4;

etc.
}
I got the impression from the example that the racers can do multiple laps. Therefore, the line 8 is required.

However, it had logical error. You set the meters_covered to 5. That is not want you want. You want to deduct all full laps from the distance. The operator% is the correct tool, but the divisor has to be the lenght of the lap, not 10.


Edit: While the if..else chain is trivial and good practice for a starter, it is not the only approach. For example, this one chooses text from a "table":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>     // std::setw
#include <algorithm>   // std::upper_bound
#include <vector>
#include <string>

int main () {
  constexpr int Max = 10;
  std::vector<std::pair<int,std::string>> foo {{6, "Albert"}, {Max, "Berta"}};

  for ( int x = 0; x < 16; ++x ) {
    int y = x % Max;
    const auto elem = std::upper_bound( foo.begin(), foo.end(), y,
            [](int lhs, const std::pair<int,std::string> & rhs){ return lhs < rhs.first; });

    if ( foo.end() != elem )
        std::cout << "D=" << std::setw(2) << x << " R=" << y << " W=" << (*elem).second << '\n';
  }
  return 0;
}
Last edited on
Therefore, the line 8 is required.
How could this be required? It is always 5. He probably meant this (and his code works if he replaces || with &&):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
using namespace std;

main() {
int meters_covered;
cin>>meters_covered;

meters_covered %= 25; // Note: not 25%10

if(meters_covered >=0 && meters_covered <= 5)
cout<<"regular road";

else if(meters_covered >= 6 &&  meters_covered <= 9) // Note: &&
cout<<"rubberized";

else if(meters_covered >= 10 && meters_covered <= 16) // Note: &&
cout<<"mud";

else if(meters_covered >= 17 && meters_covered <= 25) // Note: &&
cout<<"asphalt";
}


My code did not work bcause of this:
1
2
3
4
if(meters_covered >= 0 && meters_covered <= 4) // Yep
cout<<"rubberized";
else
meters_covered -= 4;


I definitely should have been more careful.
Topic archived. No new replies allowed.