I am very new here and to coding in general so apologies in advance for any mistakes in my questions and code.
I am currently working on this problem:
Primary U.S. interstate highways are numbered 1-99. Odd numbers (like the 5 or 95) go north/south, and evens (like the 10 or 90) go east/west. Auxiliary highways are numbered 100-999, and service the primary highway indicated by the rightmost two digits. Thus, I-405 services I-5, and I-290 services I-90. Note: 200 is not a valid auxiliary highway because 00 is not a valid primary highway number. Given a highway number, indicate whether it is a primary or auxiliary highway. If auxiliary, indicate what primary highway it serves. Also indicate if the (primary) highway runs north/south or east/west.
EX: if the input is:
290
the output is:
I-290 is auxiliary, serving I-90, going east/west.
My code is currently as shown:
#include iostream
using namespace std;
int main()
{
int A; // A is the value for the Auxiliary highway
// This message will display when the code is run//
cout << "Please enter the three digit Auxiliary highway number" << endl;
cin >> A; // User inputs the Auxiliary highway number
do A-= 100;
while (A>100);
I am not sure what I am doing but I have no clue how to go about this so I am first starting with the second part(trying to make the code understand that if I am at the auxiliary number of I-290 then I would be servicing I-90). I tried this by trying to subtract 100 when the value for A was over 100 which would in theory leave me with a 2-digit number that would be the interstate highway number. I know that an error will happen when a number that ends in two zeros is entered so I planned to just use an if-else statement at the beginning of the code that would essentially just prevent this but, again, no clue how to do it but I think it may work
2
Answers
The first thing you want to do with a problem like this is think through it step by step. How do you solve this problem as a human with a pen and paper if you are given a highway number?
First you need a piece of code to tell you if the number is primary, so the code needs to tell you if the number is less than or equal to 99, if yes, then it is primary, if no, then it is auxillary.
In the case that is is not primary, you need a piece of code to tell you what the last 2 digits are of the number. The easiest way to do this is to convert the number to a string and remove the first character, and then convert back into an integer.
Lastly, you need a piece of code that tells you whether the primary road runs north/south, or east/west. So you need to check whether the number is even or odd. The easiest way to do this is to use the modulo function (x%2==y). If y is equal to zero, then you know the road runs east/west, otherwise it runs north south.
Hopefully you can see that the problem is a series of little problems that you can solve 1 by 1 to get the full solution. Problems become a lot less scary then.
I’m afraid I don’t know C++ that well to give you a coded solution, but hopefully you can figure it out from here. It will be good practice for you to work through it because a lot of programming is about banging your head against a wall until you figure out the solution.
There are many ways to solve this problem. Here is one:
first, fix your header
#include <iostream>
accept user "highwayNumber"
cin >> highwayNumber;
write an if loop to determine highway properties:
print output to user