Advertisements

Blog Archives

C++ Recursion

In this code I’m not using the proper recursion method, or I’m not fully understanding what “recursion” is actually about.  Some help on this matter would be much appreciated.

#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
using namespace std;

// declare global variables
int sum, num1, num2, result, result1;

// function prototypes
void functionRecursion(int, int);
void functionCall(int);
void functionExit();
void functionLessThan(int);
void functionGreaterThan(int);
int functionCounter(int); // recursion function

int main()
{

functionRecursion(num1, num2);
functionCall(result);
functionCounter(result);
system(“pause”);
return 0;
}

void functionRecursion(int num1, int num2)
{

cout <<“please enter two numbers:  “<<endl;
cin >> num1 >> num2;

result1 = num1 * num2;
result = num1 * 5;
cout << result <<endl;
cout << result1 <<endl;
}
void functionCall(int result)
{
if(result < 500)
{
functionLessThan(result);
}
else;
{
functionGreaterThan(result);
}
}
void functionLessThan(int result)
{
cout << “Your answer is less then 500” <<endl;
int functionCounter(result);
system (“pause”);
exit (0);
}
void functionGreaterThan(int result)
{
cout << “Your answer is greater than 500” <<endl;
system (“pause”);
exit (0);
}

int functionCounter(int result)
{
int result2 = result;  //setting result2 to equal result for the function counter (switch)
for(int result2 = 0; result2 < 500 ; result2++);
{
cout <<“your answer is:   ” << result2 <<endl;
return result2;  //returning result2
}
}

Advertisements
%d bloggers like this: