Bugs with Program Due Tomorrow (User defined functions)

I am not asking anyone to do my homework, in fact I've got the program almost finished. However, there are a few bugs in the program that when executed repeats the same thing twice. I've spent hours on end trying to fix these bugs only to have something worse go wrong. This is my first post so I am just looking for feedback.
EDIT: I have deleted the program instructions so that other students cannot google them and copy.
Last edited on
Part 1 of my 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
125
126
127
128
129
130
131
132
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <string>

using namespace std;

int randomNumGenerator(int);
void menuOptions();

long int funcOne(long int, long int); //1
long int funcTwo(long int, long int); //2
long double funcThree(long double, int, long int); //3
long int funcFour(long int, long int); //4
long double funcFive(string, string, string, long double, long int); //5
long double funcSix(string, long double, long int); //6

long int addTen(long int, long int);
void isPrime(long int, int);

void validInput();
void cleanup();

int main()
{
    int randNum, choice, choice2, divisor;
    long int currentNum;
    long int doubledNum;
    long double reversedNum = 0;
    long double poweredNum;
    long int sumOfNum = 0;
    string firstToSecond, num1, num2;
    string secondToThird;
    long double twoDigitPowered, threeDigitPowered;
    long int add;
    
START:
    cout << fixed << showpoint << setprecision(2) << endl;
    cout << "The following random number is generated: ";
    
    currentNum = randomNumGenerator(randNum);
    cout << currentNum;
    cout << "\n\n";
    
    isPrime(currentNum, divisor);
    menuOptions();
    cin >> choice;
    cout << "\n";
 
do
{
    switch(choice)
    {
        case 1:
            cout << "Doubled = " << funcOne(doubledNum, currentNum)
                 << "\n\n";
            currentNum = funcOne(doubledNum, currentNum);

            addTen(currentNum, add);
            currentNum = addTen(currentNum, add);
            isPrime(currentNum, divisor);
            break;
        case 2:
            cout << "Reversed Number = " << funcTwo(reversedNum, currentNum)
                 << "\n\n";
            currentNum = funcTwo(reversedNum, currentNum);
            
            addTen(currentNum, add);
            currentNum = addTen(currentNum, add);
            isPrime(currentNum, divisor);
            break;
        case 3:
            
            cout << "Raise to the power of 2, 3, or 4?: ";
            cin >> choice2;
            cout << "\n";
            cout << funcThree(poweredNum, choice2, currentNum)
                 << "\n\n";
            if (choice2 == 2 || choice2 == 3 || choice2 == 4) //prevents bugging
            currentNum = funcThree(poweredNum, choice2, currentNum);
            
            addTen(currentNum, add);
            currentNum = addTen(currentNum, add);
            isPrime(currentNum, divisor);
                break;
        case 4:
            cout << "Sum of the digits of the number = "
                 << funcFour(sumOfNum, currentNum) << "\n\n";
            currentNum = funcFour(sumOfNum, currentNum);
            
            addTen(currentNum, add);
            currentNum = addTen(currentNum, add);
            isPrime(currentNum, divisor);
            break;
        case 5:
            {  cout << "First Digit raised to Second Digit = "
                 << funcFive(firstToSecond, num1, num2, twoDigitPowered, currentNum)
                 << "\n\n";
            currentNum = funcFive(firstToSecond, num1, num2, twoDigitPowered, currentNum);
            
            addTen(currentNum, add);
                currentNum = addTen(currentNum, add);
            }
            isPrime(currentNum, divisor);
            break;
        case 6:
            {  cout << "First two digits raised to the third digit = "
                 << funcSix(secondToThird, threeDigitPowered, currentNum)
                 << "\n\n";
            currentNum = funcSix(secondToThird, threeDigitPowered, currentNum);
            
            addTen(currentNum, add);
            currentNum = addTen(currentNum, add);
            }
            isPrime(currentNum, divisor);
            break;
        case -1:
            goto START;
        default:
            cout << "Invalid Choice!\n\n" << "Current Number: "
                 << currentNum << "\n";
    }
    cleanup();
    menuOptions();
    cin >> choice;
    cout << "\n";
}
    while (choice != 0);
    return 0;
}
.....
Part 2:

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
void menuOptions()
{
    cout << "Enter the corresponding number with what you want to do with this: \n";
    cout << "1: Double the number.\n";
    cout << "2: Reverse the digits of the number.\n";
    cout << "3: Raise the number to the power of 2, 3, or 4.\n";
    cout << "4: Sum the digits of the number.\n";
    cout << "5: If the number is a two digit number, then raise the first digit to the power of the second digit.\n";
    cout << "6: If the number is a three digit number and the last digit is less than or equal to 4, then raise the first two digits to the power of the last digit.\n";
    cout << "-1: Reset Random Number.\n\n";
    cout << "0: Terminate the program.\n\n";
    cout << "Enter: ";
}

//Random Number Generator
int randomNumGenerator(int randNum)
{
    while (randNum < 10 || randNum > 100)
    {     srand(static_cast <unsigned int> (time(0)));
        randNum = rand() % 100;
    }
    return randNum;
}

//Choice 1:
long int funcOne(long int doubledNum, long int currentNum)
{
    doubledNum = currentNum * 2;
        return doubledNum;
}

//Choice 2:
long int funcTwo(long int reversedNum, long int currentNum)
{
    while(currentNum != 0.0)
    {
        int remainder = currentNum % 10;
        reversedNum = reversedNum * 10 + remainder;
        currentNum /= 10;
    }
    return reversedNum;
}

//Choice 3:
long double funcThree(long double poweredNum, int choice2, long int currentNum)
{
    switch(choice2)
    {
        case 2:
            poweredNum = pow(currentNum, 2);
            cout << currentNum << " squared = ";
            break;
        case 3:
            poweredNum = pow(currentNum, 3);
            cout << currentNum << " cubed = ";
            break;
        case 4:
            poweredNum = pow(currentNum, 4);
            cout << currentNum << " to the 4th power = ";
            break;
        default:
            cout << "Not an option....";
            return currentNum;
    }
    return poweredNum;
}

//Choice 4
long int funcFour(long int sumOfNum, long int currentNum)
{
    while ( currentNum > 0 )
    {
        sumOfNum += currentNum % 10;
        currentNum /= 10;
    }
    return sumOfNum;
}

//Choice 5
long double funcFive(string firstToSecond, string num1, string num2, long double twoDigitPowered, long int currentNum)
{
    firstToSecond = to_string(currentNum);
    if(firstToSecond.length() != 2)
    {
        cout << "Not a 2-digit number\n\n";
        cout << "Current number: ";
        return currentNum;
    }
    else if(firstToSecond.length() == 2)
    {
        num1 = firstToSecond.substr(0,1);
        num2 = firstToSecond.substr(1,1);
        int numb1 = stoi(num1);
        int numb2 = stoi(num2);
        twoDigitPowered = pow(numb1, numb2);
    }
    return twoDigitPowered;

}

//Choice 6
long double funcSix(string secondToThird, long double threeDigitPowered, long int currentNum)
{
    secondToThird = to_string(currentNum);
    
    unsigned long pos1 = secondToThird.length() - 1;
    string substr =  secondToThird.substr(pos1, 1);
    if(secondToThird.length() != 3)
    {   cout << "Not a 3-digit number\n\n";
        cout << "Current number: ";
    return currentNum;}
    else if(secondToThird.length() == 3 && secondToThird.length() <= 4)
    {
        string num1 = secondToThird.substr(0,2);
        string num2 = secondToThird.substr(2,1);
        int number1 = stoi(num1);
        int number2 = stoi(num2);
        threeDigitPowered = pow(number1, number2);          
    }
    return threeDigitPowered;
}


//Add Ten
long int addTen(long int currentNum, long int add)
{
    if (currentNum < 10)
    {
        add = currentNum + 10;
        cout << "Adding 10 = " << add << "\n\n";
        currentNum = add;
    }
    return currentNum;
}

//Test Prime
void isPrime(long int currentNum, int divisor)
{
    if (currentNum >= 2)
    {    for (divisor = 2; divisor <= currentNum / 2; divisor++)
        if (currentNum % divisor == 0)
            break;
        
        if ((currentNum > 1) && (!(divisor <= currentNum / 2)))
            
            cout << "The number "<< currentNum
            << " is a prime number.\n\n";
            
            else
            cout << "The number "<< currentNum
            << " is NOT a prime number.\n\n";
    }

}

void cleanup()
{
    cin.clear();
    cin.ignore(INT_MAX, '\n');
    //system("cls"); for Windows
}


The bugs are with the addTen function as well as the 5 and 6 functions. Any feedback?
Last edited on
I'm not sure why you are passing the "add" argument to addTen. All that happens is the current value of add is copied into the function and overwritten in line 129. The new value is assigned to currentNum, which is returned. The new value of add is thrown away when the function returns.

1
2
addTen(currentNum, add);
currentNum = addTen(currentNum, add);


Also, the calls to addTen without the assignment (like the first above) are doing nothing. The function calculates a new sum and returns it, which you promptly throw away by not assigning it to anything. You only need (and want) the second line in each of these cases.

Now,
The bugs are with the addTen function as well as the 5 and 6 functions.
Can you be a little bit more specific? What are the bugs you are seeing?
Thank you for the addTen function help! It worked! Now when the number is below 10 the current number is the number with 10 added. So I just needed line 2 of your example under each case. I have been trying to crack that for days! Now that my eyes are opened I have changed each case to only call the function by assigning it to the current number. Thanks to your help as well as another's on this site, I have completed the program. This is now solved. Should I post my full code to show the completed program? Or should I just mark this as solved?
Last edited on
I'm glad you solved the problem. If you want to post your code, go ahead. It's up to you.

Good luck with your future C++ endeavors.
Topic archived. No new replies allowed.