29th Dec 2018, 6:15 AM
Jeremy Cruz
Jeremy Cruz - avatar
67 Respostas
+ 1
Well, the time limit gets exceeded because you coded an infinite loop there :) the variables involved in the condition of the while statement never change inside its body
29th Dec 2018, 7:16 AM
selwyn406
selwyn406 - avatar
+ 4
You need to type your function arguments. You can't just use n1, n2, op; it needs to be: (double n1, double n2, char op);
29th Dec 2018, 6:21 AM
Zeke Williams
Zeke Williams - avatar
+ 4
If it's not too much to ask, where is the return value of calcFunc?! return result; And why `calcFunc(n1, n2, op);` return value hasn't been assigned to something or printed to output?
29th Dec 2018, 6:48 AM
Babak
Babak - avatar
+ 4
You added now to the wrong place! And why `calcFunc(n1, n2, op);` return value hasn't been assigned to something or printed to output?
29th Dec 2018, 6:53 AM
Babak
Babak - avatar
+ 4
double calcFunc(double n1, double n2, char op) { double result; if(op == '+') { result = n1 + n2; } else if(op == '-') { result = n1 - n2; } else if(op == '*') { result = n1 * n2; } else if(op == '/') { result = n1 / n2; } else if(op == '^') { result = pow(n1, n2); } return result; }
29th Dec 2018, 6:57 AM
Babak
Babak - avatar
+ 4
int main() { double n1, n2; char op; cout << "Calculator- Please enter a digit!" << endl; cin >> n1; cout << "Please enter the operator(+, -, *, /, ^)" << endl; cin >> op; cout << "Please enter the last digit" << endl; cin >> n2; cout << "Result is: " << calcFunc(n1, n2, op); }
29th Dec 2018, 7:02 AM
Babak
Babak - avatar
+ 4
"Dude what the hell are u talking about" Bad choice of word! I feel a bit sorry for you, my dear!
29th Dec 2018, 7:03 AM
Babak
Babak - avatar
+ 3
Those are global variables. You NEED to type function parameters. Think of the variables in the function declaration, and definition, as temporary variables. You may have named them the same, but they have different addresses in memory and are therefore different variables. You are declaring n1, n2, and op as global variables at the top and then passing those into the function as COPIES, which are then named the same inside the function. Just type the arguments and see what happens. It will work, I promise šŸ˜‰
29th Dec 2018, 6:33 AM
Zeke Williams
Zeke Williams - avatar
+ 3
Don't lie to me I have a copy of it before you change anything!
29th Dec 2018, 7:01 AM
Babak
Babak - avatar
+ 2
And they're not really even global variables since they're declared inside of the main function.
29th Dec 2018, 6:33 AM
Zeke Williams
Zeke Williams - avatar
+ 1
Ok thanks ill be changing it.šŸ˜€
29th Dec 2018, 6:34 AM
Jeremy Cruz
Jeremy Cruz - avatar
0
But i already declared the type at the top
29th Dec 2018, 6:26 AM
Jeremy Cruz
Jeremy Cruz - avatar
0
Alright so now i don't get the errors but time gets exceeded . why is that? šŸ˜•
29th Dec 2018, 6:46 AM
Jeremy Cruz
Jeremy Cruz - avatar
0
How do i fix?
29th Dec 2018, 7:17 AM
Jeremy Cruz
Jeremy Cruz - avatar
0
If I made you understand what you did wrong, then I suppose you can figure out the fix. Extend the body of the while statement so that what I said happens no longer happens. Give it a shot and let me know if you need any more help
29th Dec 2018, 7:19 AM
selwyn406
selwyn406 - avatar
0
Ok
29th Dec 2018, 7:20 AM
Jeremy Cruz
Jeremy Cruz - avatar
0
So its because it keeps looping with the same values that were given to the variables
29th Dec 2018, 7:26 AM
Jeremy Cruz
Jeremy Cruz - avatar
0
Well, you said there "while op != 'q' " ,but you never actually change op inside the loop (only before it)
29th Dec 2018, 7:27 AM
selwyn406
selwyn406 - avatar
0
I would have to add the ability for the while loop to take more values instead of using the same ones
29th Dec 2018, 7:27 AM
Jeremy Cruz
Jeremy Cruz - avatar
0
Yeah, which means what exactly?
29th Dec 2018, 7:28 AM
selwyn406
selwyn406 - avatar