Bugs in 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.

EDIT: I have erased the program instructions so that other students cannot google them and copy.
Last edited on
Here is my code:
Part 1
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 (functions):
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. When executed the addTen function (if number is below 10) will say "Adding 10 = number" twice. Same with functions 5 and 6 (except instead of saying Adding 10 twice it says the output of those functions twice. I am a perfectionist and although the program works I would like for it to not repeat itself to the user. This is the hardest program I've had this far in my class and I know something is not right with my code (either in the main function or in the separate functions or both) Any feedback?

Edit: Also when testing the program occasionally when I enter 0 (to exit the program) it will say "Invalid Choice!" (going to the default in my switch(choice) function) and requires me to enter 0 again to exit the program. I am using Xcode on a Mac and I don't understand why sometimes entering 0 does not exit the program unless entered twice?
Last edited on
Firstly, the reason the code outputs "Invalid Choice!" when you enter the number 0 is because the do while loop you have used checks the condition after the iteration of the loop has executed. Instead, it should just be simply changed to a while loop where the condition is checked at the top before the loop iteration.

Secondly, the reason it says the output of adding 10 to a number twice is because inside each case statement you have called the addTen function twice. You call it once without making a variable equal its output then straight away you call it again and set its value to currentNum. Lines 61 and 62 for example. You appear to have done this for all the case statements.
Thank you for replying, I appreciate it very much. The while loop makes sense now. Another person on this site also helped me with the addTen function which in turn opened my eyes to fix function 5 and 6. My program is now complete. Thank you.
Last edited on
Can you message me the full instructions? This looks like a program I would like to try.
Topic archived. No new replies allowed.