You are trying to use a "functional" way of thinking, like from Scheme. However, even in Scheme you must expect that the variables you use to only bind in the current context (unless you use
set!).
You have more than one object named "num1". The same for your other variables.
On line 8 you declare a
global value named "
num1". But on line 23 you have a
local value named "
num1", which 'hides' the
global num1. Hence, anything you do to
num1 on lines 24 through 40 only affects the
local num1 and the
global num1 is not touched.
So on line 34, when you call
number2(), the value of the
local num1 is entirely forgotten. Later on, on line 53, you call
result() with the
global num1 as argument...
By the way, the
global num1 is never initialized, so its value could be anything, not just zero.
Also, your functions never return value, so they should be
void.
There are several ways to fix this.
The first is to make all the function arguments references.
1 2 3 4
|
void number1(char&,int&); //My first function
void number2(char&,int&);//My second function
void result(int&,int&);//My Third function
void loop(int&);//My fourth function
|
This is a mistake, however. The same could be more easily accomplished (and much more understandable) by simply making all your functions parameterless, and just directly refer to the global values (num1, num2, letter, and x):
1 2 3 4
|
void number1(); //My first function
void number2();//My second function
void result();//My Third function
void loop();//My fourth function
|
The second is to only pass on state information needed for the next part of the program. So, if you need to remember
num1s value, you should pass it as argument to
number2().
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
|
#include <iostream>
using namespace std;
void number1(); //My first function
void number2(int);//My second function
void result(int,int);//My Third function
void loop();//My fourth function
int main()
{
loop();//This is my first function call
cin.get();
return 0;
}
void loop()
{
int x;
for (x = 0; x < 2; x++)
{
number1();
}
}
void number1()//This is the function being called and working in action.
{
char letter;
int num;
cout<<"Please enter a number.\n";
cin>> num;
cin.ignore();
cout<<"You've entered "<< num <<" as your first number. Is this correct?\n";
cout<<"Type'y' then enter for yes; 'n' then enter for no.\n";
cin>> letter;
cin.ignore();
if (letter == 'y')
{
number2(num);//This is the continuation of the function calls.
}
else
{
number1();//This is redirecting itself back to the function
} //so the number variable can be edited by the user.
}
void number2(int num1)//This is my function that has been called by the previous
{ //function.
char letter;
int num2;
cout<<"Please enter another number.\n";
cin>> num2;
cin.ignore();
cout<<"You've entered "<< num2 <<" as your second number. Is this correct?\n";
cout<<"Type 'y' then enter for yes; 'n' then enter for no.\n";
cin>> letter;
cin.ignore();
if (letter == 'y')
{
cout<<"Calculating results, please wait....\n";
result(num1,num2);//The final and last function.
}
else
{
number2(num1);//Function calling itself to be edited by user.
}
}
void result(int num1, int num2)//Last function comparing to two integers.
{
if (num1 < num2)
{
cout<< num1 <<" is less than "<< num2 << ".\n";
}
else if (num1 == num2)
{
cout<< num1 <<" is equal to "<< num2<<".\n";
}
else
{
cout<< num1 <<" is greater than "<< num2<<".\n";
}
}
|
(You'll also notice that I improved your language a bit with the user. The user doesn't know or care that he is supplying a value for "num1" or "num2". Just ask him to enter two numbers. And so on.)
However, this still has that "functional" flavor that obfuscates the meaning and flow of the program. Ideally, your program should be understandable simply by looking at it. For example, here is a main function that you can use:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <iostream>
#include <string>
using namespace std;
int get_number( const string& numbers_name );
void print_results( int num1, int num2 );
int main()
{
for (int x = 0; x < 2; x++)
{
int num1, num2;
num1 = get_number( "first" );
num2 = get_number( "second" );
print_results( num1, num2 );
}
cout << "Press ENTER to quit." << flush;
cin.get();
return 0;
}
|
All the additional variables (like
letter and
x) are declared locally and discarded when no longer needed; that is, they don't appear anywhere but where used.
I'm off to a family reunion. Hope this helps.