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

## All 3 Replies

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, learning, and sharing knowledge.
{"ft_lang_label":"__label__en","ft_lang_prob":0.5756567,"math_prob":0.9662502,"size":1542,"snap":"2022-27-2022-33","text_gpt3_token_len":456,"char_repetition_ratio":0.15474643,"word_repetition_ratio":0.05498282,"special_character_ratio":0.36446175,"punctuation_ratio":0.2019544,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98938257,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-19T23:04:24Z\",\"WARC-Record-ID\":\"<urn:uuid:6ca5cbbf-7a96-4ee5-ae53-e9137b1c931d>\",\"Content-Length\":\"74922\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7c60e326-0881-478a-910e-fd06cbd514d1>\",\"WARC-Concurrent-To\":\"<urn:uuid:d1b06055-3b5e-4b66-8db7-14e3a6052518>\",\"WARC-IP-Address\":\"172.66.42.251\",\"WARC-Target-URI\":\"https://www.daniweb.com/programming/software-development/threads/409109/code-revise-please-2-numbers-an-operand\",\"WARC-Payload-Digest\":\"sha1:M4YDM3ZUSKOHC5YDT724YL5PPLPKVVIO\",\"WARC-Block-Digest\":\"sha1:OUKP4RQUUPCAKQBZYK4YPZR273A54RBI\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882573849.97_warc_CC-MAIN-20220819222115-20220820012115-00614.warc.gz\"}"}