Help with debugging?

I'm getting a weird error when I try to debug this in visual studios. It just prints the greeting and then goes straight into an infinite loop. Please 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
  
#include <iostream>
#include <string>
using namespace std;

void GetNameAndNumber(string, int long long);
void PrintGreeting();
bool isValid(long long number);
int getSize(long long d);
int getDigit(int digit);
int sumOfDoubleEvenPlace(long long number);
int sumOfOddPlace(long long number);
int getPrefix(long long number, int k);
bool prefixMatched(long long number, int d);

int main()
{
   int long long number;
   string name;
   
   // DO_4: Write a call to the function to print the greeting.
   //Write a call to the function to get the name and number.
   PrintGreeting();
   GetNameAndNumber(name, number);
   while (cin)
   {
      if (isValid(number))
         cout << name << "'s card number " << number << " is valid."
            << endl << endl;
      else
         cout << name << "'s card number " << number << " is invalid." 
              << endl << endl;
      void GetNameAndNumber(string, int long long);
   }
   return 0;
}

//---------------------------------------------------------------------
// Function displays a greeting.
// params: ()
//---------------------------------------------------------------------
void PrintGreeting()
{
   cout << "************************************************" << endl;
   cout << "* Welcome to the credit card number validator! *" << endl;
   cout << "************************************************" << endl;
}

//---------------------------------------------------------------------
// Function gets the name and card number from the user.
// params: (out, out)
//---------------------------------------------------------------------
void GetNameAndNumber(string name, int long long number)
{
   cout << "Enter the name on the credit card: " << endl
        << "Ctrl+Z+Enter to quit: ";
   cin >> name;
   cout << endl;
   cout << "Enter the credit card number, " << endl
        << "Ctrl+Z+Enter to quit: ";
   cin >> number;
   cout << endl;
}

//---------------------------------------------------------------------
// Function returns true if the card number is valid.
// params: (in)
//---------------------------------------------------------------------
bool isValid(long long number)
{
   return  (getSize(number) >= 13) && (getSize(number) <= 16) &&
           (prefixMatched(number, 4) || prefixMatched(number, 5) ||
            prefixMatched(number, 6) || prefixMatched(number, 37)) &&
           (sumOfDoubleEvenPlace(number) + 
            sumOfOddPlace(number)) % 10 == 0;
}

//---------------------------------------------------------------------
// Function returns the number of digits in d.
// params: (in)
//---------------------------------------------------------------------
int getSize(long long d)
{
  
      int counter = 0;
      while (d)
      {
         d = d / 10;
         counter++;
      }
      return (counter);
 }
   
   
   
   
   
   


//---------------------------------------------------------------------
// Function returns this number if it is a single digit, otherwise, 
// return the sum of the two digits.
// params: (in)
//---------------------------------------------------------------------
int getDigit(int digit)
{
   int result = 0;
   int first;
   int tenths;
   first = digit % 10;
   tenths = digit / 10;
   digit = first + tenths;

   return result;
}

//---------------------------------------------------------------------
// Function returns the result from Step 2
// params: (in)
//---------------------------------------------------------------------
int sumOfDoubleEvenPlace(long long number)
{
   int result = 0;

   number = number / 10; // Starting from the second digit from right
   while (number != 0)
   {
      result += getDigit(int((number % 10) * 2));
      number = number / 100; // Move to the next even place
   }

   return result;
}

//---------------------------------------------------------------------
// Function returns the sum of odd place digits in number.
// params: (in)
//---------------------------------------------------------------------
int sumOfOddPlace(long long number)
{
   long result = 0;

   while (number != 0)
   {
      result += (number % 10);
      number = number / 100; // Move two positions to the left
   }

   return result;
}

//---------------------------------------------------------------------
// Function returns the first k number of digits from number. If the 
// number of digits in number is less than k, return number.
// params: (in)
//---------------------------------------------------------------------
int getPrefix(long long number, int k)
{
   long long result = number;
   
   int count = 0;
   while (count < (getSize(number) - k))
   {
      result /= 10;
      count++;
   }

   return int(result);
}

//---------------------------------------------------------------------
// Function returns true if the number d is a prefix for number.
// params: (in)
//---------------------------------------------------------------------
bool prefixMatched(long long number, int d)
{
   return getPrefix(number, getSize(d)) == d;
}
Last edited on
Line 33 is the prototype and thus not calling the function.

By the way: If you want GetNameAndNumber(...) to retrieve name and number you need to pass the parameters by reference not by value. The prototype would look like this:

void GetNameAndNumber(string &, int long long &); // Note: &
Do you know how I would call that function?
else
cout << name << "'s card number " << number << " is invalid."
<< endl << endl;
GetNameAndNumber(name, number);

**with the above & fix, else it won't do anything.

Also, it does not actually respond to your ctrl z combo yet.

I don't really know what you are going for here but one thing you might do there is

bool GetNameAndNumber( ...
{
...code to read name
if(detected ctrlz etc)
return false;
... read number and detect again, again return false if found
...
all good, return true
}

and you can use the true/false return to detect if the user aborted. There are other ways, that is one. You could also assign name and number to a default invalid value that you can check after but that is a little clunky; only reason to do that is if the prototype insisted on void type by some design requirement.

Last edited on
Topic archived. No new replies allowed.