Hello, I have been working on a program that I got to work successfully, but when I transferred it to having only function calls in my main(), I started to get run errors telling me that something is being used without being initialized after I enter the second number.


Basically the user inputs two numbers and an operator of their choice. The output on the screen should be something like 23*2=46.

Am I missing something? :[

Thanks.

#include <iostream>;
using namespace std;

void GetData(int& num1, int& num2, char& optr);
void Calc(int num1, int num2, char& optr);
void DisplayResult(int num1, int num2, int);

int main()
{
	int   result, total;		
	char  optr;		
	int   num1, num2;    


	GetData(num1, num2, optr);

	Calc(num1, num2, optr);
	
	DisplayResult(num1, num2, result);

	return 0;

}
void GetData(int& num1, int& num2, char& optr)
{
	  
        cout << "Enter the operator: ";
		cin >> optr;
		cout << "Enter two numbers (largest number first): ";
	    cin >>  num1;
		cout << "Enter the second number: ";
		cin >>  num2;
}
void Calc(int num1, int num2, char& optr)
{
	int total;
      if (optr == '+') {
            total = num1 + num2;
        } else if (optr == '-') {
            total = num1 - num2;
        } else if (optr == '*') {
            total = num1 * num2;
        } else if (optr == '/') {
            if (num1 == 0) {
                cout << "Error: Divide by zero\n";
                cout << "   operation is ignored\n";
            } else
                total = num1 / num2;
        } else {
            cout << "An unknown operator " << optr << '\n';
		}
}
void DisplayResult(int num1, int num2, int result)
{
	char optr;
	result = 0; 
		cout << "the result is " << num1 << " " << optr 
		 << " " << num2 << " = " << result << endl;
}

in DisplayResult() , what's the value of optr?

commented: ty! +1

+1 to WaltP

Actually, did you read the error(shouldn't it be a warning?)? It should clearly state the line where this uninitialized variables is used.

Other than that, you never set result inside main()

For some reason I was thinking that there could only be three variables. I typed my code up again and added optr:

void DisplayResult(int, int, char, int);

and I also added "char optr"

void DisplayResult(int num1, int num2, char optr, int result)
{
	cout << "the result is " << num1 << " " << optr
			<< " " << num2 << " = " << result << endl;
}

It ran correctly. Thank you so much! :]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.