Problem comparing Variables

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
#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 num1,num2,;//Integer variables I will be using
char letter;//The variable that will contain the answer choices.

int main()
{
    number1(letter,num1);//This is my first function call
    cin.get();
    return 0;
}

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";
             }
}


This is my current code that i'm working for. But now that I compile it, there is a bit of a problem. There are no errors, and it compiles with no warnings either. But the trip is, that one of the numbers are lost and turned to a 0. Compile the before c++ source file and you'll see what I mean. Thanks in advance.
I've noticed several problems with your code, including syntax errors. What compiler are you using? It should be complaining.

Anyway, from what I can tell your use of global variables is a little weird. Why are you passing a global variable as a function argument? That function can access the variable itself, without being passed it's value. That is most probably where the problem lies.
Last edited on
Well, that's what the tutorial displayed. I simply copied the action it was using. When defining a function, I passed the variable as the argument. And when declaring a function, I told the compiler what variables it would be using. If this is incorrect(By which I think it is by your reply) what would be the correct way of calling functions and using them? Much appreciated for your answer.

PS. I'm using Dev-C++. I'm not getting any errors or of the such. It says it's using the GCC compiler. If that means anything to you.
Topic archived. No new replies allowed.