0

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;
}
4
  • 2
    try with Digits % 10 == 7 Commented Feb 25, 2014 at 21:45
  • 1
    It won't make a difference here, but 0004 is an octal literal, and equivalent to 04, which is just 4. Were you to do, say, 0014, it would have the value 12. Commented Feb 25, 2014 at 21:46
  • Is there a way to put placeholders or variables in the If Functions? So someone could enter 3324 or 1274 and it display the "Origin: Florida". Commented Feb 25, 2014 at 21:48
  • @TranquilityByDesign: If you have to work on codes that are more complex than simple numbers (say phonenumbers like +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. Commented Feb 26, 2014 at 10:12

2 Answers 2

1

A useful "trick" is to use the modulo (%) operator to get a number's digits. Say you want to get the last one...

int n = 1234;
int last_digit = n % 10; // last_digit == 4

To get the one next to the last, divide the number by 10 (which makes the last digit to go away) and do the same:

n /= 10;
int second_to_last = n % 10; // second_to_last == 3
Sign up to request clarification or add additional context in comments.

2 Comments

It seems by doing that you can only receive the first number of the integer without bumping into doubles. I am looking in this case to have the last number of the 4-digit code to be the KEY in the equation that provides which answer to display. Such as, entering 1234 or 2234 or 5564 "Origin: Florida" and, entering 1237 or 2237 or 5567 "Origin: California". So no matter the first 3 #'s of the 4-digit code I need the last digit to be the determining factor of which message to display. Is there a type of variable I can use with absolute 4th digit
@TranquilityByDesign: Computers don't store digits (base 10), they store bits (base 2). That's why you need the explicit % 10.
0

See if this helps: First, you may need to validate if the entered number of digits are 4, Something like this: // "number" is unsigned int so we remove the possibility of entering -ve number int digits = 0; while (number) { number /= 10; digits++; }

If the digits != 4 display invalid, otherwise check the last digit using the Modulo operator as mentioned by @Faranwath. Yes, and one more thing you need to save the number as you will need it later on to check the last digit.

1 Comment

I'm sorry. I believe everyone is giving me wonderful answers to the problem, yet I have no clue how to implement that into what I am working on. I'll google around a bit for implementation and hopefully figure out what you guys mean lol Thank you for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.