Trying out functions for the first time, what am I doing wrong?
I appreciate any help that I can get:
Write a program that takes as input an arithmetic expression and verifies if it is correct. The user will input the expression in the following format: operator num1 num2 num3 The program should now check if: num1 operator num2 = num3 If it is correct the program should print: num1 operator num2 = num3 If it is not correct the program should print: num1 operator num2 != num3 I did it without using functions but now I'm trying to test them out What's wrong with the code? Code:
#include <iostream> |
Quote:
Declaring the same variable name in two different scopes gives you two different variables with the same name. Have you learned about structures yet? Have you learned about passing by reference yet? I don't want to do your homework for you. But most methods I could suggest (rather than demonstrate) probably involve techniques you don't know yet. The common way to do the operation you described is to define a structure type that contains all of the results of the user() function, which are also all of the inputs of the output function. Then declare one instance of that structure in the main function and pass it by reference to the other functions. That is, of course, not the only possible way to do it. The simple, working but bad way to do it is to declare the individual variables once outside of all the functions, so they are global and accessible by all functions. That pretty much defeats the point of splitting the work into functions. But it does split the work into functions and it does work. In a sufficiently beginner assignment in a poorly taught programming class, that might be the expected answer. |
So how should it look?
|
Quote:
Quote:
|
this mite help:
http://www.linuxquestions.org/questi...8/#post2155086 |
variables are only accessible within the same context from which they were declared.
so if i have two functions, both these variables actually access a different piece of memory: Code:
int main() In order to have a variable accessible in two functions i would need to declare it in a context that is accessible from both functions, like the following: Code:
int x; This method, however, is not generally a good practice, as it does not create modular code (i.e. code that can easily be re-used in different situations, like with different values). For example, does not allow you to easily use your function with different inputs, because it always acts on the x variable. You want your code to be modular, think of a game cartridge being plugged into a game system. To do something like this, you'd pass the variables by reference as arguments to the function. Although it may be a little confusing at first, requiring an understand of pointers (which can be rather tricky at first) it actually makes a lot more sense than doing it like above, and allows you to use your functions with any input you wish to pass to it: Code:
int main() Code:
int main() Code:
int x[100]; |
Also, instead of using a giant if block, i would use a simple switch-case statement instead:
Code:
char c; |
Quote:
The original question was about C++, not C. Whether "pass by reference" is an appropriate name in C for the technique you showed could be argued either way. But it is certainly not an appropriate name for that technique in C++. C++ has pass by reference as a language feature. So the technique one should call "pass by reference" in C++ is actual pass by reference, not passing the address to a pointer. |
Emotional Response
Hi.
Now that your problem is resolved, in oh so many ways, at oh so many levels, will you permit an emotional response ? Why do programmer have to pound everything with the hammer of arrogance? It seems, by your post and example, that you've got a complete naivete about this programming language (and forums), yet it seems the purpose of your function is to get to this cout: Quote:
Like I said, this is just a emotional response to your query especially contrasted with the gentleness and quality of the help and advice you received. Best to you continuing to sharpen programming skills. |
@LQParsons: when you consider that a program like this will generally only be run by its programmer, it could be an example of humility rather than arrogance.
|
All times are GMT -5. The time now is 08:42 AM. |