954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

code revise please: 2 numbers & an operand

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;
}
SoftShock
Newbie Poster
15 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

in DisplayResult() , what's the value ofoptr?

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

+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()

jaskij
Junior Poster
105 posts since Oct 2011
Reputation Points: 55
Solved Threads: 18
 

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! :]

SoftShock
Newbie Poster
15 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: