Calling a function and Passing Data

I'm having some trouble retrieving the data from my void function calculations. When the program asks the user to enter the calculation type (switch function)in the main function it errors out saying that addSum has not been initialized. I was under the impression that through completing the void function calculation it would assign a value to addSum...? I'm not sure exactly how to fix this problem. I understand what it the error means, but it must be my order of operation that is incorrect. Also, I've tried removing the addSum from the function prototype, but it says that there are too few arguments. Any help would greatly be appreciated! Thank you.



/*Write a C++ program that asks users for two numbers and a choice of the following operations: addition, subtraction, multiplication, division or exit the program.
The program then displays the result of chosen operations on the two numbers entered or terminates. Use one function to get two numbers, separate functions for each
arithmetic operations (+, -, *, /),and one function to display the output. The program should continue running until the user chooses to exit.


Assignment 3*/

#include <iostream>
#include<iomanip>

using namespace std;

void Values(double num1, double num2)
{
cout << "Enter two values to calculate: " << setw(5) << "Num 1: " << setw(15);
cin >> num1;
cout << "Num 2: " << setw(5);
cin >> num2;
}

void Addition(double num1, double num2, double sumAdd) // Function used to calculate sum
{
sumAdd = num1 + num2;
}

void Subtraction(double num1, double num2, double sumAdd)
{
sumAdd = num1 - num2;
}

void Multiplication(double num1, double num2, double sumAdd)
{
sumAdd= num1 +num2;
}

void Division(double num1, double num2, double sumAdd)
{
sumAdd = num1 / num2;
}


int main()
{
char choice;
double num1,num2, sumAdd;

Values(num1, num2);

cout << endl << endl << "Table of Operations" << endl;
cout << "A -- Addition" << endl;
cout << "B -- Subtraction" << endl;
cout << "C -- Multiplication" << endl;
cout << "D -- Division" << endl << endl;
cout << "Enter choice for calculation: " ;
cin >> choice;

switch(choice) // Switch function allows user to choose the calculation to execute
{
case 'A':
case 'a': Addition(num1, num2, sumAdd);
break;
case 'B':
case 'b': Subtraction(num1, num2, sumAdd);
break;
case 'C':
case 'c': Multiplication(num1, num2, sumAdd);
break;
case 'D':
case 'd': Division(num1, num2, sumAdd);
break;
}



system("PAUSE");
return 0;
}
I believe you want to pass your values by reference. Currently, when you call a function, you're only creating copies of each variable, modifying those copies, and then disposing of them once the function terminates. To modify the variables and to "save" the modifications, you need to pass the variables by reference. This is really simple. You simply append an &(reference operator) to the front of the parameters and call your functions the same way you are now. An example would be: void Values(double &num1, double &num2)
Last edited on
@Volatile --- It worked! thank you for your help. You rock man!

Now that is working, I'm getting a #INF (i assume infinity) when I divide the values. I changed the data type to float and setprecision(5). Also, I've set a statement to exclude 0 from calculations. Any idea how I could fix this? Greatly appreciated.
I don't believe you've excluded 0 correctly. Can you paste your code for just the division?

(Please remember to use the [code][/code] tags. They're the <> button to the right. )
void Division(float &num1, float &num2, float &sumAdd) // Function used to calculate quotient
{

if(num2 = 0)
cout << "Please enter a number other than 0 as denominator";
else
setprecision(5);
sumAdd = num1 / num2;
}
Even though I'm not dividing by 0, it should calculate a value, but I get the #INF.

I don't understand tags you mentioned. It requires some sort of bracket or brace in the equation?? :)
Your issue is the first line in your function. You're assigning num2 to 0, so you're always printing out that message, and you're always going to be dividing by zero. Your else statement also only set's the precision to 5, the sumAdd = num1 / num2 (which is always 0) happens every time.

As for the code tags, you would type it like this:
[code]void Division(float &num1, float &num2, float &sumAdd) // Function used to calculate quotient
{
if(num2 = 0)
cout << "Please enter a number other than 0 as denominator";
else
setprecision(5);
sumAdd = num1 / num2;
}[/code]

And it would show up like this:
1
2
3
4
5
6
7
8
void Division(float &num1, float &num2, float &sumAdd)	 // Function used to calculate quotient
{
   if(num2 = 0) 
      cout << "Please enter a number other than 0 as denominator";
   else
      setprecision(5);
   sumAdd = num1 / num2;
}


It makes it much easier to read.

Edit: Another alternative, paste your code, highlight it, and then press the <> button on the right of text input box.
Last edited on
void Division(float &num1, float &num2, float &sumAdd)			// Function used to calculate quotient
{
	if(num2 != 0) 
		{
			setprecision(5);
			sumAdd = num1 / num2;
		}
	else
		cout << "Please enter a number other than 0 as denominator";
}



This did it. I still don't see how using an if statement ( if (num2=0) ) assigned num2 to 0. The if statement is just a check statement, to which the function will/will not perform, correct? I do catch what your saying about the else statement only setting the precision to 5 -- needed brackets.

One other question, if you would be so kind, is there a way to break from the entire do/while loop, if the user chooses? Right now, I'm using a do/while to create a loop, until the user enter 01 as num1 -- I just don't like the flow of doing it this way; but say, for instance, the user choose '99' in switch(choice), and it breaks from the loop completely, maybe displaying a "Have a nice day" message or some asinine message ...then exit.

Thanks for all your help again.
Last edited on
You got close, you used the output tags, but it's still easier to read.

As for the if statement, it checks the statement for true/false, or a boolean value, so the statement must first be evaluated. Your previous example, num2 = 0 assigns num2 equal to 0. This is always the case. If you want to check equality, you need to use conditional operators, == (equal to), != (not equal to), < (less than), etc. I noticed you switched it to !=, which works, but with your last code, it could have simply been switched to == to check equality.

As for breaking out of the do/while loop, in your switch statement, you have the option of using the "default" label which is just like an else condition. If the switch doesn't equal any of the cases you chose, the default case will be used. An example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
switch(choice)	 // Switch function allows user to choose the calculation to execute
{
   case 'A':
   case 'a':
      Addition(num1, num2, sumAdd);
      break;
   case 'B':
   case 'b':
      Subtraction(num1, num2, sumAdd);
      break;
   case 'C':
   case 'c':
      Multiplication(num1, num2, sumAdd);
      break;
   case 'D':
   case 'd':
      Division(num1, num2, sumAdd);
      break;
   default:
      choice = 'E'; // Assign choice to E to exit
      cout << "Display your exit message here" << endl;
}

And then in the while portion of the loop, you will need to loop while choice doesn't equal 'E'.

That should get you onto the right track.
Fantastic! You've been a great help. I see what you were saying now, I forgot all about the conditional operators!! Thanks for giving me your time. I never thought c++ would be much fun, but in the process of 'crafting' our programs and solving problems its quite entertaining.

Do you find that you use C++ a lot in technical/programming world?
Programming is a hobby of mine, but it acts as entertainment, and stress relief (sometimes) but is quite fun. I like reading on the forums to find new ideas, creating them from scratch and looking at the different ways the same thing can be accomplished.

C++ is a lot of fun, and learning is interesting too. I found that you need a fairly large mental capacity or you need to learn how to use Google. Otherwise, there isn't many limits on what you can do with C++.
Topic archived. No new replies allowed.