Looping problem

Im having a problem with my loop. If someone gives me a credit card number less then 13 or greater than 16, i want it to loop and ask the user to enter a new number. It doesnt do that. Its on line 92 til 95
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include<iostream>
#include<sstream>
#include<string>
#include<cmath>

using namespace std;

//Method for check first numbers in string
bool creditCardType(string cardNumber)
{

    return
    //For Visa cards
    (cardNumber.compare(0, 1, "4") == 0) ||
    //For Mastercards
    (cardNumber.compare(0, 1, "5") == 0) ||
    //For American Express card
    (cardNumber.compare(0, 2, "37") == 0) ||
    //For Discover cards
    (cardNumber.compare(0, 1, "6") == 0);
    
}
//Create a method called getdigit which keeps adding
//Numbers for credit card validation.
int getDigit(int number)
{
    int result =0;
    while(number)
    {
        result = result + (number%10);
        number = number/10;
    }
    return result;
}
// to calculate sum of numbers at even places.
int sumOfDoubleEvenPlaces(const string& cardNumber)
{
    int sum_even_places = 0;
    for (int i = cardNumber.length() -2; i>=0; i-=2)
    {
        sum_even_places = sum_even_places + getDigit(2*(cardNumber[i] - '0'));
    }
    return sum_even_places;
}
//to calculate sum of numbers at odd places.
int sumofoddplace(const string& cardNumber)
{
    int sumoddplaces = 0;
    for (int i = cardNumber.length() -1; i>=0; i-=2)
    {
        sumoddplaces = sumoddplaces +((cardNumber[i] - '0'));
    }
    return sumoddplaces;
}

// function to check card is valid or not.
bool isValid(const string& cardNumber)
{
    return ((sumOfDoubleEvenPlaces(cardNumber)+sumofoddplace(cardNumber))%10==0);
}

//Write a program that asks the user for a credit card number.
//Execute using conditions below.
int main()
{
    long int cardnNum;
    int choice;
    //Enter credit card number.
    cout << "Please enter your credit card number: ";
    cin >>cardnNum;
    cout << endl;
    stringstream ss;
    ss << cardnNum;
    string cardnumber = ss.str();
    // 1= use asset logic on length of credit card number
    cout <<"Press 1 to use asset logic on length of credit card number" << endl;
    // 2= print error message and continue on to next credit card number.
    cout <<"Press 2 to use creditCardType logic on credit card number " << endl;
    cin >> choice;
    if(choice == 1)
    {
        cout << cardnumber << " is a ";
        if(cardnumber[0] == '4')
            cout <<" VISA";
        if(cardnumber[0] == '5')
            cout <<" MASTER";
        if(cardnumber[0] == '6')
            cout <<" DISCOVER";
        if(cardnumber[0] == '3')
            cout <<" AMERICAN EXPRESS";
        cout << endl;
        do{{
        if((cardnumber.length()>=13 && cardnumber.length()<=16))
            cout << cardnumber << " has a length of " <<cardnumber.length() <<" is a valid card " << endl;
        }while((cardnumber[0]>=13)&&(cardnumber[0]<=16));
        else
        {
        cout << cardnumber<< " has a length of " <<cardnumber.length() <<" is an invalid card " << endl;
        }
    else if(choice == 2)
    {
        if(creditCardType(cardnumber) && isValid(cardnumber))
        {
            cout << cardnumber << " is a ";
            if(cardnumber[0] == '4')
                cout <<" VISA";
            if(cardnumber[0] == '5')
                cout <<" MASTER";
            if(cardnumber[0] == '6')
                cout <<" DISCOVER";
            if(cardnumber[0] == '3')
                cout <<" AMERICAN EXPRESS";
            cout <<" Is Valid card " << endl;
        }
        cout << cardnumber << " has invalid type so "<< cardnumber<<" is an invalid card " << endl;
        return 0;
    }}
in while you decide what condition that make your program won't go out of loop. Must be condition You do not want.

while((cardnumber.length()<13)||(cardnumber.length()>16));
Last edited on
Thanks but it didnt solve my issue. I actual need it to ask the user to ask the first question again on line 69 which i just realized was my mistake. What i want it to do is if the user enters a length shorter than 13 or longer than 16 to ask the question again. Here is the up to date code.
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include<iostream>
#include<sstream>
#include<string>
#include<cmath>

using namespace std;

//Method for check first numbers in string
bool creditCardType(string cardNumber)
{
    
    return
    //For Visa cards
    (cardNumber.compare(0, 1, "4") == 0) ||
    //For Mastercards
    (cardNumber.compare(0, 1, "5") == 0) ||
    //For American Express card
    (cardNumber.compare(0, 2, "37") == 0) ||
    //For Discover cards
    (cardNumber.compare(0, 1, "6") == 0);
    
}

int getDigit(int number)
{
    int result =0;
    while(number)
    {
        result = result + (number%10);
        number = number/10;
    }
    return result;
}

// to calculate sum of numbers at even places.
int sumOfDoubleEvenPlaces(const string& cardNumber)
{
    int sum_even_places = 0;
    for (int i = cardNumber.length() -2; i>=0; i-=2)
    {
        sum_even_places = sum_even_places + getDigit(2*(cardNumber[i] - '0'));
    }
    return sum_even_places;
}

//to calculate sum of numbers at odd places.
int sumofoddplace(const string& cardNumber)
{
    int sumoddplaces = 0;
    for (int i = cardNumber.length() -1; i>=0; i-=2)
    {
        sumoddplaces = sumoddplaces +((cardNumber[i] - '0'));
    }
    return sumoddplaces;
}

// function to check card is valid or not. Should get sumofdoubleven and sumofoddplaces.
bool isValid(const string& cardNumber)
{
    return ((sumOfDoubleEvenPlaces(cardNumber)+sumofoddplace(cardNumber))%10==0);
}

//Write a program that asks the user for a credit card number.
//Execute using conditions below. 7/18
int main()
{
    long int cardnNum;
    int choice;
    do{
    //Enter credit card number.
    cout << "Please enter your credit card number: ";
    cin >>cardnNum;
    }while((cardnNum.length()<13)||(cardnNum.length()>16));
    cout << endl;
    
    stringstream ss;
    ss << cardnNum;
    string cardnumber = ss.str();
    
    // 1= use asset logic on length of credit card number
    cout <<"Press 1 to use asset logic on length of credit card number" << endl;
    
    // 2= print error message and continue on to next credit card number.
    cout <<"Press 2 to use creditCardType logic on credit card number " << endl;
    cin >> choice;
    
    //If choice 1, use assert logic.
    if(choice == 1)
    {
        cout << cardnumber << " is a ";
        if(cardnumber[0] == '4')
            cout <<" VISA";
        if(cardnumber[0] == '5')
            cout <<" MASTER";
        if(cardnumber[0] == '6')
            cout <<" DISCOVER";
        if(cardnumber[0] == '3')
            cout <<" AMERICAN EXPRESS";
        cout << endl;
        
            if((cardnumber.length()>=13 && cardnumber.length()<=16))
                cout << cardnumber << " has a length of " <<cardnumber.length() <<" is a valid card " << endl;
            else
                cout << cardnumber<< " has a length of " <<cardnumber.length() <<" is an invalid card " << endl;
    }
    else if(choice == 2)
    {
        if(creditCardType(cardnumber) && isValid(cardnumber))
        {
            cout << cardnumber << " is a ";
            if(cardnumber[0] == '4')
                cout <<" VISA";
            if(cardnumber[0] == '5')
                cout <<" MASTER";
            if(cardnumber[0] == '6')
                cout <<" DISCOVER";
            if(cardnumber[0] == '3')
                cout <<" AMERICAN EXPRESS";
            cout <<" Is Valid card " << endl;
        }
        cout << cardnumber << " has invalid type so "<< cardnumber<<" is an invalid card " << endl;
        system("pause");
        return 0;
    }}
Sorry my mistake, I saw you use cardnNum.length() so I suggest that condition.(But that condition still good to use)

Now I saw you used long int cardnNum;, the .length() is for string.

Change long int cardnNum; to be string cardnNum;?
I saw your code use cardnNum like a string more than a long.
Last edited on
Thanks for the help. Appreciate it. I hate not seeing those simple mistakes
Just an experience from facing too many errors before, LOL.
Last edited on
I came across another error now. It will loop for both valid and invalid so if i put a number less than 13 and greater than 16 it loops but also does the same for valid entries.
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include<iostream>
#include<sstream>
#include<string>
#include<cmath>

using namespace std;

//Method for check first numbers in string
bool creditCardType(string cardNumber)
{
    
    return
    //For Visa cards
    (cardNumber.compare(0, 1, "4") == 0) ||
    //For Mastercards
    (cardNumber.compare(0, 1, "5") == 0) ||
    //For American Express card
    (cardNumber.compare(0, 2, "37") == 0) ||
    //For Discover cards
    (cardNumber.compare(0, 1, "6") == 0);
    
}

int getDigit(int number)
{
    int result =0;
    while(number)
    {
        result = result + (number%10);
        number = number/10;
    }
    return result;
}

// to calculate sum of numbers at even places.
int sumOfDoubleEvenPlaces(const string& cardNumber)
{
    int sum_even_places = 0;
    for (int i = cardNumber.length() -2; i>=0; i-=2)
    {
        sum_even_places = sum_even_places + getDigit(2*(cardNumber[i] - '0'));
    }
    return sum_even_places;
}

//to calculate sum of numbers at odd places.
int sumofoddplace(const string& cardNumber)
{
    int sumoddplaces = 0;
    for (int i = cardNumber.length() -1; i>=0; i-=2)
    {
        sumoddplaces = sumoddplaces +((cardNumber[i] - '0'));
    }
    return sumoddplaces;
}

// function to check card is valid or not. Should get sumofdoubleven and sumofoddplaces.
bool isValid(const string& cardNumber)
{
    return ((sumOfDoubleEvenPlaces(cardNumber)+sumofoddplace(cardNumber))%10==0);
}

//Write a program that asks the user for a credit card number.
//Execute using conditions below. 7/18
int main()
{
    string cardnNum;
    int choice;
    do{
    //Enter credit card number.
    cout << "Please enter your credit card number: ";
    cin >>cardnNum;
    }while((cardnNum.length()<13)||(cardnNum.length()>16));
    cout << endl;
    
    stringstream ss;
    ss << cardnNum;
    string cardnumber = ss.str();
    
    // 1= use asset logic on length of credit card number
    cout <<"Press 1 to use asset logic on length of credit card number" << endl;
    
    // 2= print error message and continue on to next credit card number.
    cout <<"Press 2 to use creditCardType logic on credit card number " << endl;
    cin >> choice;
    
    //If choice 1, use assert logic.
    if(choice == 1)
    {
        cout << cardnumber << " is a ";
        if(cardnumber[0] == '4')
            cout <<" VISA";
        if(cardnumber[0] == '5')
            cout <<" MASTER";
        if(cardnumber[0] == '6')
            cout <<" DISCOVER";
        if(cardnumber[0] == '3')
            cout <<" AMERICAN EXPRESS";
        cout << endl;
        
            if((cardnumber.length()>=13 && cardnumber.length()<=16))
                cout << cardnumber << " has a length of " <<cardnumber.length() <<" is a valid card " << endl;
            else
                cout << cardnumber<< " has a length of " <<cardnumber.length() <<" is an invalid card " << endl;
    }
    else if(choice == 2)
    {
        if(creditCardType(cardnumber) && isValid(cardnumber))
        {
            cout << cardnumber << " is a ";
            if(cardnumber[0] == '4')
                cout <<" VISA";
            if(cardnumber[0] == '5')
                cout <<" MASTER";
            if(cardnumber[0] == '6')
                cout <<" DISCOVER";
            if(cardnumber[0] == '3')
                cout <<" AMERICAN EXPRESS";
            cout <<" Is Valid card " << endl;
        }
        cout << cardnumber << " has invalid type so "<< cardnumber<<" is an invalid card " << endl;
        system("pause");
        return 0;
    }}
Could you give me an example of your input and your result?
I don't face that problem.
If i put 31234567 that 8 so it should ask again if i put3123456789123 its between 13 and 16 so it shouldnt loop.
Maybe its just my compiler? I use Xcode.
For me 3123456789123 doesn't get back to the loop.
Are you sure you entered right?
Which IDE you're using?

Maybe, but that doesn't make sense.
try cout << cardnumber.length();
Last edited on
I just ran it on shell instead and it didnt happen so im not worried about it.
Okay,

but this issue's gonna stuck in my head for a while.
Maybe I have to find someway to try Xcode, LOL.
Well ill try and post a screen shot.
Ok what happened now was that quit Xcode and then reopened it and pasted the code. Now it runs just the way it should. Thats really weird. Now i Cant get that screen shot. I dont know why that happened. I guess its a wierd compiler lol
Weird, too.
I won't use it I promise, lol.
i didnt read you whole code just the line you said.. i think becuase do while has a 2 first bracket
Topic archived. No new replies allowed.