Varible looses it's content

I'm using the program Dev-C++ to code my program. I've just touched up on loops so I've been testing out every single loop there is in this example program I coded to see if I had the hang of them before moving on. But oddly enough, after implementing the the while loop, my result variable would seem to "lose" it's content, replacing it with a zero. Now I KNOW IT DOESN'T ACTUALLY LOSE IT'S CONTENT, but it replaces it with a zero as said before. My question is, WHY does it do this? What in my code is bugged in doing this? Please remember I'm new at this, and any code that's passed my comprehension will not work at all for me if I can't understand what it means. A simple explination why it's doing what it's doing, and if need be, a bit of code that I could possibly understand that could fix my problem would be greatly appreciated. If the only way to do this is code I might not understand, a link explaining the code or a lesson of the such explaining what it means would also help. Here's 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
#include <iostream>
using namespace std;

int number1(char,int); //My first function
int number2(char,int);//My second function
int result(int,int);//My Third function
int loop(int);//My fourth function
int num1,num2;//Integer variables I will be using
char letter;//The variable that will contain the answer choices
int x;//The variable that will contain the count for the amount of loops
int main()
{
    loop(x);//This is my first function call
    cin.get();
    return 0;
}
int loop(int x){
    
    for (x = 0; x < 2; x++){
          number1(letter,num1);
          }
          }
int number1(char letter, int num1)//This is the function being called and working in action.
{
    cout<<"Please enter a number for num1.\n";
    cin>> num1;
    cin.ignore();
    cout<<"You've entered "<< num1 <<" for num1. 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(letter,num2);//This is the continuation of the function calls.
                  }
                  else
                  {
                      number1(letter,num1);//This is redirecting itself back to the function
                      }                    //so the number variable can be edited by the user.
                      }
int number2(char letter, int num2)//This is my function that has been called by the previous 
{                                 //function. 
    cout<<"Please enter a number for num2.\n";
    cin>> num2;
    cin.ignore();
    cout<<"You've entered "<< num2 <<" for num2. 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(letter,num2);//Function calling itself to be edited by user.
                      }
                      }
int result(int num1, int num2)//Last function comparing to two integers.
{
    if (num1 < num2)//I don't know where in the program exactly, but the integer for num1
    {               //is somehow lost before here and turned into a zero.
             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";
             }
             
             
}





Thank you in advance.
Lines 38 and 57: No. NEVER. Don't recurse a function just to take user input. Put it in a loop.

You're making a horrible mess with local and global scope. Start by removing line 8 and work from there.
So code it like this:

1
2
3
4
5
6
7
8
else
                  {
                     while (letter == 'n')
                 {
number2(letter,num2);
                      }
                      }


instead of how I had it in a else just by itself?
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.
That is the best fugging answer I have ever seen and I thank you greatly for it! I'll go over your post and learn from it. I totally forgot that the variables take global and local scopes. I'll code from now on knowing this. Thanks again. :D
That is the best fugging answer I have ever seen and I thank you greatly for it!

He does put a lot of effort into his answers, doesn't he?
Topic archived. No new replies allowed.