I'm doing rather beginner work with C++ and I am having a difficult time understanding what I am doing wrong with a simple program I am writing. So far, I have got the program written out some what properly, but I am confused on how to place variable place holders in IF Functions. Assuming of course, that is what is supposed to make it work properly.
To start with a little background on how the program is supposed to function. The user enters a 4-digit code and as long as the last digit of the code is a "7" it should display "Origin: Florida". Part two of the IF function would be, if the user enters a 4-digit number and the last digit of the code is a "4" it should display "Origin: California". I finished it with an else statement for any other entries to display "Invalid". The problem I am having is figuring out how to add variable digits for the first 3-digits of the code and when the last digit is entered with a "7" or "4" have a certain "Origin" displayed. I have searched and searched for guides on the interwebs for help, but I've had very little luck. If anyone could help me with this I would be very grateful for the assistance.
Below is the code I've written so with of course fault being in the IF statements:
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int Digits = 0;
cout << " ..:::Grocery Program:::.. " << endl;
cout << " ..::Created By Justin::.. " << endl;
cout << "\n";
cout << " Enter the orange's 4-digit code: ";
cin >> Digits;
if ( Digits == 7 )
{
cout << " Origin: Florida ";
}
else if ( Digits == 0004 )
{
cout << " Origin: California ";
}
else
{
cout << " Origin: Invalid ";
}
cout << "\n";
cout << "\n";
cout << "\n";
cout << "\n";
cout << "\n";
system("pause");
return 0;
}
Digits % 10 == 70004is an octal literal, and equivalent to04, which is just 4. Were you to do, say,0014, it would have the value 12.+316-1111-2222), you'd use "Regular Expressions". Those have explicit placeholders. In this simple case, we could write the regular expression^\d{3}4$which means start with 3 digits, then a 4. As yu see, for simple examples regular expressions are a bit overkill. In C++ they're provided by the<regex>header.