I was in the process of making a basic calculator, when my friend mentioned that using int addition(), int subtraction, etc... to list the different functions, would be a much cleaner and easier way to go about this. I had the calculator working, but when I try this format it doesn't seem to turn out any results. Can someone shed light on why this won't run?
Well, first of all. In all your functions you are returning a value, when you still have instructions. The last two lines of all your computation functions are never even read. This is why results are never shown.
Why on earth are you using goto?? Especially so often. Do you have a problem with calling functions normally?
System calls are bad practice. I'm not sure who always tells people to use there....? Get rid of that and your error will be gone. Just out of curiosity though, what OS are you on?
Your last if, could just be changed to an else. Would be much cleaner and a little better performance wise.
Hey ResidentBiscuit,
I'm very new. Like, a week into self-teaching myself. Thus the reason I'm so behind in progression. And the reason I have a bunch of questions on what you said.
"In all your functions you are returning a value, when you still have instructions. The last two lines of all your computation functions are never even read. This is why results are never shown."
- This isn't true... I don't think. It worked for me when I had it in the old format.
"Why on earth are you using goto?? Especially so often. Do you have a problem with calling functions normally?"
- I didn't know there was another way :/ Can you explain? I'm gonna be honest, I just got to the functions portion of cplusplus.com's tutorial.
"System calls are bad practice. I'm not sure who always tells people to use there....? Get rid of that and your error will be gone. Just out of curiosity though, what OS are you on?"
- I've heard that about system calls, but besides "system('cls')" do I use it a lot? :/ Sorry. And operating system? Windows 7. CodeBlocks for my compiler.
And the last part about the if to else makes sense! Much obliged if you could answer these!
Oh sorry that says "result", not return. Unless you changed it? I don't remember at this point. I can guarantee that a function is done once it hits a return, if its a non void function that is. Return is the exit point of any function. Main() for example, that is the entry point to your program. And once main hits a return, your program is done.
Anyways, I think you changed your functions. Cause now there is no return statement at all. If you have a function of any data type (ie not void), you have to have a return in there. The compiler is expecting a value to be kicked out of there.
In regards to goto, I've never actually seen it used as a function call before. I wonder if someone with more experience could explain why this is a bad idea?
sampleFunction(); This is how you call a function. All you do is write it. So just delete your all of your goto. If your function ever has any parameters (Stuff in parentheses), then you have to send arguments with matching data type.
No, that's the only system call I see in there. There's many articles on why it's bad to use, and you can look it up yourself if you're curious. There's other ways to clear the screen, that are much safer and efficient.
Code::Blocks is your IDE, MingW is your compiler. There is a difference.
You have a few mistakes in there, which is normal for being a beginner. So if you fix your function calls, and put in proper returns, this will look much nicer. You have a goto main() in there. I never use goto, and wouldn't even think about using it to goto main(), can you tell me if your invalid() function actually works? I know you can't call main from anywhere in your program, but I dont know about using goto. But, thats just my curiosity, I can test it myself actually. Get rid of all goto's. This will have you rethink your logic a bit, because you are no longer allowed to just restart main(). Look into while loops.
Something like goto addition(); shouldn't compile since goto needs a label not a function. Remove all goto expressions.
This if (a != 1, 2, 3, 4) doesn't do what you think it does. Each expression before the comma is evaluated and the last one is used for the operation itself so at the end it's just if (a != 4)
Don't call main(). The task of main is to initialize global variables and anything that's needed to start the program.
This int division() is a function that returns an int. Therefore somewhere (preferable at the end) there must be a return statement.
it doesn't seem to turn out any results
This is due to line 103. if you clear the screen (on line 64) you won't see anything and (like stated above) you call invalid() whenever a != 4
Really the goto thing shouldn't compile. My code::blocks gives me errors like
"I was in the process of making a basic calculator, when my friend mentioned that using int addition(), int subtraction, etc... to list the different functions, would be a much cleaner and easier way to go about this." Using the switch statement would actually be easier, here is my calculator, it does everything yours does :)
Thanks guys! I finally got it working the way I wanted, but I got one more little bug that's killing me. This is the basis of what my functions look like now:
1 2 3 4 5 6 7 8 9 10 11 12 13
int division()
{
double a, b;
double result;
cout << "\nInput your 1st number: ";
cin >> a;
cout << "\nInput your 2nd number: ";
cin >> b;
result = a / b;
cout << "\n\nResult: ";
return (result);
}
But when it returns the value, (to be printed back in the main()) it won't show decimals even though I'm using double as the variable. Is there a problem with transitioning from function to function with decimals? Or am I missing something?
Same with the square root function I just included since I found the <math.h> library,