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.
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;
}
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)
@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.
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.
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.
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:
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++.