credit card details

Hi everyone, im trying to put in a 16 digit credit card with space or "-" after each 4 digits. the code works fine if I put in a correct 16 digit code... but if I put in the wrong code it asks me once again to enter the details and I put in the correct details it will keep asking me re enter the detail. now if I put in a bad code it will ask me once to re enter but if I put in the right code it will pop up 4 times saying to enter details???? any ideas.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using namespace std;
#include <iostream>
int main () {
    string credit;
    float good;
    bool valid = true;

do
{    cout << "\n\t\tPlease enter yor credit card details: ";
    getline (cin, credit, '\n');
        if (credit.length () != 19)
            valid=false;
       else if (!(credit.at(4)==' '||credit.at(4)=='-'))
                valid = false;
       else if (!(credit.at(9)==' '||credit.at(9)=='-'))
                valid = false;
       else if (!(credit.at(14)==' '||credit.at(14)=='-'))
                valid = false;
else for     (unsigned short i = 0; i <= credit.length () - 16; i++)
            if (!isdigit(credit.at(i)))
                valid = false;
else for     (unsigned short j = 5; j <= credit.length () - 11; j++)
            if (!isdigit(credit.at(j)))
                valid = false;
else for     (unsigned short p = 10; p <= credit.length () - 6; p++)
            if (!isdigit(credit.at(p)))
                valid = false;
else for     (unsigned short i = 15; i <= credit.length () - 1; i++)
            if (!isdigit(credit.at(i)))
                valid = false;

if (valid) cout<<"thanks";
}
while (!valid);
return 0;
}
Set it to true before asking the user to input a password (inside do/while loop). You are never resetting it to true so it will stay false for ever.
You need to implement a way to change the value of valid to true once it has been set to false. Your code in the do while loop only sets value to false, so if you entered it right the second time there is no way for valid to become true. Initialize your bool variable at the beginning of your loop instead of outside of it.
Last edited on
Thanks very much to both of ye have been at it all day :) but great satisfaction out of getting it done now :). Here are the changes and works a treat. Thanks again

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using namespace std;
#include <iostream>
int main () {
    string credit;
    float good;
    bool valid = true;

do
{    cout << "\n\t\tPlease enter yor credit card details: ";
    getline (cin, credit, '\n');
        if (credit.length () != 19)
            valid=false;
       else if (credit.at(4)==' '||credit.at(4)=='-')
                valid = true;
       else if (credit.at(9)==' '||credit.at(9)=='-')
                valid = true;
       else if (credit.at(14)==' '||credit.at(14)=='-')
                valid = true;
else for     (unsigned short i = 0; i <= credit.length () - 16; i++)
            if (isdigit(credit.at(i)))
                valid = true;
else for     (unsigned short j = 5; j <= credit.length () - 11; j++)
            if (isdigit(credit.at(j)))
                valid = true;
else for     (unsigned short p = 10; p <= credit.length () - 6; p++)
            if (isdigit(credit.at(p)))
                valid = true;
else for     (unsigned short i = 15; i <= credit.length () - 1; i++)
            if (isdigit(credit.at(i)))
                valid = true;

if (valid) cout<<"thanks";
}
while (!valid);
return 0;
}
I would have just put valid = true on line 9. move the rest down 1 line.
Then check if it is not valid and if it is not valid set it to false.

basically like this in pseudo code:


1
2
3
4
5
6
7
8
do
{
    valid = true;
    //get input
    if( it has dashes at 4 , 9 , 14 ) valid = false;
    else if( letters 0-3 && 5-8 && 10-18  are not digits ) valid = false;
} while( !valid );
cout << "Your password is valid." << endl;
Last edited on
thanks giblet I was maybe abit hasty in thinking it was done last week as it was not working correctly so I have just used what you said and have done a few tests and it seems to be working right this time thanks for the help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using namespace std;
#include <iostream>
int main () {
    string credit, expiry;
    float good;
    bool valid;

do
{      valid = true;
    cout << "\n\t\tPlease enter yor credit card details: ";
    getline (cin, credit, '\n');
        if (credit.length () != 19)
            valid=false;
       else if (!(credit.at(4)==' '||credit.at(4)=='-'))
                valid = false;
       else if (!(credit.at(9)==' '||credit.at(9)=='-'))
                valid = false;
       else if (!(credit.at(14)==' '||credit.at(14)=='-'))
                valid = false;
else for     (unsigned short i = 0; i <= credit.length () - 16; i++)
            if (!isdigit(credit.at(i)))
                valid = false;
else for     (unsigned short j = 5; j <= credit.length () - 11; j++)
            if (!isdigit(credit.at(j)))
                valid = false;
else for     (unsigned short p = 10; p <= credit.length () - 6; p++)
            if (!isdigit(credit.at(p)))
                valid = false;
else for     (unsigned short i = 15; i <= credit.length () - 1; i++)
            if (!isdigit(credit.at(i)))
                valid = false;

if (valid) cout<<"thanks please enter your expiry date";

while(!valid)

return 0;
}
Topic archived. No new replies allowed.