0
What's wrong with this?
67 Antworten
+ 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
+ 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);
+ 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?
+ 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?
+ 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;
}
+ 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);
}
+ 4
"Dude what the hell are u talking about"
Bad choice of word!
I feel a bit sorry for you, my dear!
+ 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 😉
+ 3
Don't lie to me I have a copy of it before you change anything!
+ 2
And they're not really even global variables since they're declared inside of the main function.
+ 1
Ok thanks ill be changing it.😀
0
But i already declared the type at the top
0
Alright so now i don't get the errors but time gets exceeded . why is that? 😕
0
How do i fix?
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
0
Ok
0
So its because it keeps looping with the same values that were given to the variables
0
Well, you said there "while op != 'q' " ,but you never actually change op inside the loop (only before it)
0
I would have to add the ability for the while loop to take more values instead of using the same ones
0
Yeah, which means what exactly?