Guys I have to deceipher this code but I can't make heads or tails out of it cause it closes the comand window before i can see anything I tried adding a system pause at the end but that just gave me an error.

// Program 6.9: Validate a telephone access code
#include <iostream>

#include <cstdlib>
using namespace std;

// Prototypes for utility functions that main will use

bool Get( istream &in, int &d1, int &d2, int &d3,
          int &d4, int &d5 );

bool Valid( int d1, int d2, int d3, int d4, int d5);

int main()
{
    int d1;
    int d2;
    int d3;
    int d4;
    int d5;

    if ( Get( cin, d1, d2, d3, d4, d5 )&&
         Valid( d1, d2, d3, d4, d5 ) )
        return 0;
    else
        return 1;
}



bool Get( istream &sin, int &d1, int &d2, int &d3,
          int &d4, int &d5 )
{
    char c1;
    char c2;
    char c3;
    char c4;
    char c5;

    if ( sin >> c1 >> c2 >> c3 >> c4 >> c5)
    {
        if ( isdigit( c1 ) && isdigit( c2 ) && isdigit( c3 ) &&
             isdigit( c4 ) && isdigit( c5 ) )
        {
            d1 = c1 - '0';
            d2 = c2 - '0';
            d3 = c3 - '0';
            d4 = c4 - '0';
            d5 = c5 - '0';
            return true;
        }
        else
            return false;
    }
    else
        return false;
} // end Get function



bool Valid( int d1, int d2, int d3, int d4, int d5 )
{
    if ( d4 == 0 )
        return false;
    else
        return ( ( d1 + d2 + d3 ) % d4 ) == d5;
} // end Valid function

// end code file prog6-9.cpp

Recommended Answers

All 5 Replies

That is because this program requires the proper use of getchar() or cin.get() so that the junk (newline character) is filtered out of the input stream. Also considering that your return statements are in abundance, it becomes a pain in the neck to make out what it is going on. I can say that this is not the best of the codes that I have seen... ;)

Try this one:

#include <iostream>

#include <cstdlib>
using namespace std;

// Prototypes for utility functions that main will use

bool Get( istream &in, int &d1, int &d2, int &d3,
          int &d4, int &d5 );

bool Valid( int d1, int d2, int d3, int d4, int d5);

int main()
{
    int d1;
    int d2;
    int d3;
    int d4;
    int d5;

    if (Get( cin, d1, d2, d3, d4, d5)&& Valid( d1, d2, d3, d4, d5 ))
    {
        cout << "The code has been vaildated" << endl;
        getchar();
        return 0;
    }
    else
    {
        cout << "The code is invalid";
        getchar();
        return 1;
    }
}



bool Get( istream &sin, int &d1, int &d2, int &d3,
          int &d4, int &d5 )
{
    char c1;
    char c2;
    char c3;
    char c4;
    char c5;

    if ( sin >> c1 >> c2 >> c3 >> c4 >> c5)
    {
        getchar();
        if ( isdigit( c1 ) && isdigit( c2 ) && isdigit( c3 ) &&
             isdigit( c4 ) && isdigit( c5 ) )
        {
            d1 = c1 - '0';
            d2 = c2 - '0';
            d3 = c3 - '0';
            d4 = c4 - '0';
            d5 = c5 - '0';
            return true;
        }
    }
    return false;
}



bool Valid( int d1, int d2, int d3, int d4, int d5 )
{
    if ( d4 == 0 )
        return false;
    else
        return ( ( d1 + d2 + d3 ) % d4 ) == d5;
}

Or:

int main()
{
    int rtn;
...
    if (Get( cin, d1, d2, d3, d4, d5)&& Valid( d1, d2, d3, d4, d5 ))
    {
        cout << "The code has been vaildated" << endl;
        rtn = 0;
    }
    else
    {
        cout << "The code is invalid";
        rtn = 1;
    }
    getchar();
    return rtn;
}

I personally prefer this method because only one exit from the program is used. But it's strictly a personal style issue.

Yes even I normally prefer a single exit point since it makes it easier for me to see what exactly is returned without hunting for multiple returns, but I didn't change the code for the OP to let him know where he was going wrong.

Thanks guys I see what you mean. How did you get your text to be highlighted? I added the code=syntax in between the [ ]. Thats what it sayed in the announcements page, but my code always comes out black.

You need to replace the term syntax with the langauge of the snippet you are posting. For eg. while posting C++ code, replace syntax with cplusplus, when dealing with C code, replace it with c, and so on...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.