I just finished writing this code and am ready to start debugging and testing. However, when I try to run it in GDB the console remains blank. After letting it sit for five minutes nothing is appearing in the console below the code (no errors or program text). What am I missing that is causing the program not to run through the compiler? Any help is appreciated and the code and descriptions of what each function/line is supposed to do is included below.
// This program is able to generate a random number to be doubled, digits reversed, raise to 2 3 and 4 power, sum the digits, // if two digit raise first digit to second power, and if three digits and the last digit is <= 4 then raise the first two digitss to the power of the last digit. // In addition if after an operation is performed the number is < 10, 10 will be added to the number, the number will be checked for prime and each successive // operation will use the most recent number generated. //This is for Week 6 Assignment CS265 //These three lines initialize the required predefined functions to run the program. #include <iostream> #include <stdlib.h> #include <time.h> //This sets the namespace for the entire program to std. using namespace std; //This function doubles the current number. int doub(int d) { //This creates a variable to store the doubled number. int duo; //This doubles the number. duo = (d*2); //This returns the value for the doubled number for use in output. return duo; } //This function reverses the digits of the current number. int reverse(int r) { //This creates a variable to store the reversed number. int revnum = 0; //This while loop runs to split the digits of the number. while (r > 0) { //This reverses the number. revnum = revnum*10 + r%10; //This specifies the digit order of the number. r = r/10; } //This returns the reversed number for use in output. return revnum; } //This function raises the number by user selected power. int power(int p) { //This creates a variable to store the number post operations. int powe = 0; //This creates a variable for the power to raise by. int rais; //This explains to the user to enter the power to raise by out of 2, 3, 4. cout << "Welcome to Power Raiser." << endl; cout << "Please enter the power to raise by: 2, 3, or 4." << endl; //This stores the user input for the power to raise by. cin >> rais; //This if statement raises the number to the second power if 2 is entered by the user. if (rais == 2) { powe = (p^2); } //This if statement raises the number to the third power if 3 is entered by the user. if (rais == 3) { powe = (p^3); } //This if statement raises the number to the fourth power if 4 is entered by the user. if (rais == 4) { powe = (p^4); } //This closes the program if anything other than 2, 3, 4 is entered by the user. else { //These lines explain the error. cout << "Input Error" << endl; cout << "You entered: " << rais << endl; cout << "Cannot raise to this power currently." << endl; cout << "Exiting Program" << endl; cout << ". . . . . . . ." << endl; //These two lines exit the program. exit(18); return(18); } //This returns the number raised to the selected power for use in output. return powe; } //This function takes the sum of the digits of the number. int sum(int s) { //This creates a variable to store the sum. int su = 0; //This creates a loop control variable. int n = 0; //This sets the LCV to equal the number. n = s; //This while loop sums the digits of the number. while (n != 0) { //These two lines perform the operations required to sum the digits of the number. su = (su + n % 10); n = n/10; } //This line returns the sum of the number for use in output. return su; } //This function raises the first digit of the number by the second. int twodigit(int two) { //These two lines create variables for the first and second digit of the number. int d_one; int d_two; //This creates a variable to store the result from the operations. int dig; //These two lines split the number into the first and second digits. d_one = (two / 10); d_two = (two % 10); //This raises the first digit to the power of the second digit. dig = (d_one ^ d_two); //This returns the answer from the operations to be used in output. return dig; } //This function raises the first two digits of the number by the third digit if the third digit is less than 4. int threedigit(int three) { //These three lines create variables for the three digits in the number. int di_one; int di_two; int di_three; //These three lines specify which digit of the number is first, second, and third. di_one = (three / 100 % 10); di_two = (three / 10 % 10); di_three = (three % 10); //This loop raises the first two digits to the power of the third digit. while (three >= 100) { //This if statement checks if the third digit is less than or equal to 4. if (di_three <= 4) { //This creates a variable to store the result of the operation. int di_four; //This raises the first two digits to the power of the third digit. di_four = ( ((di_one * 100) + (di_two * 10)) ^ di_three ); //This returns the result for use in output. return di_four; } //This else statement runs when the third digit in the number is greater than 4. else { //These two lines explain the error. cout << "The number's third digit is greater than 4." << endl; cout << "Cannot perform the requested operation." << endl; //This line restarts the program. return(0); } } } //This function checks if the current number is prime. int prime(int pri) { //These two lines create the LCV and a place to store if the number is prime. int v; int prim; //This for loop checks if the number is prime. for (v = 2; v <= pri; v++) { //This if statement stores if the number is prime. if (pri % v == 0) { prim = 1; } } //This if statement outputs if the number is prime. if (prim == 1) { cout << "The number is Prime" << endl; } //This statement outputs if the number is not prime. if (prim != 1) { cout << "The number is not Prime" << endl; } } //This function checks if the current number is less than ten. int lessten(int l) { //This creates a variable to store the number plus ten. int plusten = 0; //This if statement checks if the number is less than 10. if (l < 10) { //These two lines explain the number is less than 10 and that 10 will be added to the number. cout << "The number post operation is less than 10." << endl; cout << "Adding 10 to the number." << endl; //This adds 10 to the number. plusten = (l + 10); //This returns the number plus ten for use in output. return plusten; //This tells the user what the number plus ten is. cout << "The number plus 10 is: " << plusten << endl; } //This else statement handles if the number is greater than 10. else { //This tells the user the number is greater than 10. cout << "The number is greater than 10" << endl; //This returns the number for use in output. return l; } } //This function is used to exit the program. int quit() { //These two lines explain that the program is closing. cout << "Exiting Program" << endl; cout << ". . . . . . . ." << endl; //These two lines exit the program. exit(1); return(1); } //This function generates random numbers between 1 and 99 int randomgen() { //This creates a variable to store the random number. int ran; //This creates a LCV for the do loop below and sets it equal to 0. int y = 0; //This do loop generates the random number. do { //This generates a random time seed. srand(time(0)); //This generates a random number between 1 and 99. ran = rand() % 100; //This while controls the do loop. }while (y != 9); //This returns the random number for use in the rest of the program. return ran; } //This starts the main program. int main() { //This creates a variable to control which number is used as the current. int z = 1; //This creates a LCV for the do loop below. int x = 0; //This creates the variable for user menu selection. int menu; //This do loop controls the menu function of the program. do { //These four lines create variables for the random number, the number after operations, and the current number. int randnum; int stored; int num; //This makes the program only create a random number the first pass through the loop. if (z < 2) { //This runs the random number generator function. return randomgen(); //This sets the result of the function as the random number. randnum = randomgen(); //This sets the random number to the current number. num = randnum; } //This makes the program perform operations on the most recent number after operations are performed. else { //This sets the current number equal to the result of the operations performed. num = stored; } //These three lines explain what the program does. cout << "Welcome to Random Number Opertor." << endl; cout << "This program allows for multiple opertions to be performed on a randomly generated number." << endl; cout << "Once an operation has been performed, any addition operation will be performed on the number from the last operation." << endl; //These eight lines provide the menu options to navigate the program. cout << "====================== Menu ======================" << endl; cout << "1. Double the Number" << endl; cout << "2. Reverse the Digits" << endl; cout << "3. Raise the Number by the 2, 3, or 4 Power" << endl; cout << "4. Sum the Digits" << endl; cout << "5. Raise First Digit by Second Digit(2-Digit Numbers Only)" << endl; cout << "6. Raise First two Digits by Third Digit if it is less than 4 (3-Digit Numbers Only)" << endl; cout << "7. Exit" << endl; //This tells the user the current number. cout << "The current number is " << num << endl; //These two lines ask for a menu selection and store the answer in the menu variable. cout << "Please pick an operation from the menu" << endl; cin >> menu; //This if statement runs if the user selects menu option 1. if (menu == 1) { //This passes the current number through the doubling function. return doub(num); //This sets the current number to the result of doubling. num = doub(num); //This tells the user the result of the operation. cout << "The number doubled is: " << num << endl; //This tells the user of the prime number check. cout << "Checking if number is prime." << endl; //This passes the current number through the prime number checking function. return prime(num); //This tells the user a > 10 < test is being performed on the number. cout << "Checking if number is greater than 10." << endl; //This passes the current number through the less than 10 check and operations function if required. return lessten(num); //This sets the stored number equal to the current number so the menu can rerun with the number after operations. stored = num; } //This if statement runs if the user selects menu option 2. if (menu == 2) { //This passes the current number through the digit reverse function. return reverse(num); //This sets the current number to the result of the function pass. num = reverse(num); //This tells the user the reversed number. cout << "The number reversed is: " << num << endl; //This tells the user of the prime number check. cout << "Checking if number is prime." << endl; //This passes the number through the prime checking function. return prime(num); //This tells the user a > 10 < test is being performed on the number. cout << "Checking if number is greater than 10." << endl; //This passes the current number through the less than 10 check and operations function if required. return lessten(num); //This sets the stored number equal to the current number so the menu can rerun with the number after operations. stored = num; } //This if statement runs if the user selects menu option 3. if (menu == 3) { //This passes the current number throught the function to raise the numbers power by 2, 3, or 4. return power(num); //This sets the current number equal to the result of the power function. num = power(num); cout << "The number raised to your selected power is: " << num << endl; //This tells the user of the prime number check. cout << "Checking if number is prime." << endl; //This passes the number through the prime checking function. return prime(num); //This tells the user a > 10 < test is being performed on the number. cout << "Checking if number is greater than 10." << endl; //This passes the current number through the less than 10 check and operations function if required. return lessten(num); //This sets the stored number equal to the current number so the menu can rerun with the number after operations. stored = num; } //This if statement runs if the user selects menu option 4. if (menu == 4) { //This passes the current number through the sum function. return sum(num); //This sets the sum of the digits equal to the current number. num = sum(num); cout << "The sum of the number is: " << num << endl; //This tells the user of the prime number check. cout << "Checking if number is prime." << endl; //This passes the number through the prime checking function. return prime(num); //This tells the user a > 10 < test is being performed on the number. cout << "Checking if number is greater than 10." << endl; //This passes the current number through the less than 10 check and operations function if required. return lessten(num); //This sets the stored number equal to the current number so the menu can rerun with the number after operations. stored = num; } //This if statement runs if the user selects menu option 5. if (menu == 5) { //This passes the current number through the funciton to raise the first digit by the second. return twodigit(num); //This stores the result of the function as the current number. num = twodigit(num); cout << "The first digit of the number raised to the power of the second is: " << num << endl; //This tells the user of the prime number check. cout << "Checking if number is prime." << endl; //This passes the number through the prime checking function. return prime(num); //This tells the user a > 10 < test is being performed on the number. cout << "Checking if number is greater than 10." << endl; //This passes the current number through the less than 10 check and operations function if required. return lessten(num); //This sets the stored number equal to the current number so the menu can rerun with the number after operations. stored = num; } //This if statement runs if the user selects menu option 6. if (menu == 6) { //This passes the current number through the function to raise the first two digits by the third if the third digit is less than or equal to 4. return threedigit(num); //This stores the result of the function as the current number. num = threedigit(num); cout << "The first two digits raised to the power of the third is: " << num << endl; //This tells the user of the prime number check. cout << "Checking if number is prime." << endl; //This passes the number through the prime checking function. return prime(num); //This tells the user a > 10 < test is being performed on the number. cout << "Checking if number is greater than 10." << endl; //This passes the current number through the less than 10 check and operations function if required. return lessten(num); //This sets the stored number equal to the current number so the menu can rerun with the number after operations. stored = num; } //This if statement runs if the user selects menu option 7. if (menu == 7) { //This runs the function to exit the program. return quit(); } //This if statement handles the user enters something not listed on the menu. if (menu != 1 && menu != 2 && menu != 3 && menu != 4 && menu != 5 && menu != 6 && menu != 7) { //These five lines explain the error and reason for program shutdown. cout << "Error" << endl; cout << "Option Selected does not exist." << endl; cout << "Please enter options between 1-7 only." << endl; cout << "Exiting Program" << endl; cout << ". . . . . . . ." << endl; //These two lines exit the program. exit(15); return(15); } //This increase the value of the LCV by 1 for each loop pass. This allows for the program to choose between the random number and stored number for operations. z = z++; //This while statement controls the do loop. }while (x > 0); } 112 Answers
Whoops you have an infinite loop here
//This creates a LCV for the do loop below and sets it equal to 0. int y = 0; //This do loop generates the random number. do { //This generates a random time seed. srand(time(0)); //This generates a random number between 1 and 99. ran = rand() % 100; //This while controls the do loop. }while (y != 9); y is initialized to 0 and you are looping while y!=9. And that is just the first error, there might be more.
Bug #2
int z = 1; //This makes the program only create a random number the first pass through the loop. if (z < 2) { //This runs the random number generator function. return randomgen(); //This sets the result of the function as the random number. randnum = randomgen(); //This sets the random number to the current number. num = randnum; } After fixing infinite loop in randomgen(), You are immediately returning...
you have two problems in your code: the first one is that you have infinite loop in randomgen() function the condition in check against y - y != 9 but the value never update
int randomgen() { //This creates a variable to store the random number. int ran; //This creates a LCV for the do loop below and sets it equal to 0. int y = 0; //This do loop generates the random number. do { //This generates a random time seed. srand(time(0)); //This generates a random number between 1 and 99. ran = rand() % 100; //This while controls the do loop. }while (y != 9); //This returns the random number for use in the rest of the program. return ran; } and the second one is even if you pass the loop in the function you exit main right away because the call to the function randomgen() is with return
if (z < 2) { //This runs the random number generator function. return randomgen(); <-- **second problome** //This sets the result of the function as the random number. randnum = randomgen(); //This sets the random number to the current number. num = randnum; } 1