Use conditional statements to check if the input is valid, primary, or auxiliary. Use modulo operator to determine if the highway runs north/south or east/west.
To code this in C++, you can use conditional statements and string concatenation to generate the output based on the given highway number. Here's an example code:
#include <iostream>
#include <string>
using namespace std;
int main() {
int highwayNum;
string output;
cout << "Enter the highway number: ";
cin >> highwayNum;
if (highwayNum >= 1 && highwayNum <= 99) {
output = "I-" + to_string(highwayNum) + " is primary, going ";
if (highwayNum % 2 == 0) {
output += "east/west.";
} else {
output += "north/south.";
}
} else if (highwayNum >= 100 && highwayNum <= 999 && highwayNum % 100 != 0) {
int primaryNum = highwayNum % 100;
output = "I-" + to_string(highwayNum) + " is auxiliary, serving I-" + to_string(primaryNum) + ", going ";
if (primaryNum % 2 == 0) {
output += "east/west.";
} else {
output += "north/south.";
}
} else {
output = to_string(highwayNum) + " is not a valid interstate highway number.";
}
cout << output << endl;
return 0;
}
The code first prompts the user to enter the highway number, then uses conditional statements to determine if the number is a valid primary or auxiliary highway number. The output is generated using string concatenation based on the type of highway and its direction. If the highway number is invalid, the output indicates so. Finally, the output is displayed to the user using the cout statement.