I don't know why this isn't working?

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

#include <iostream>
#include <string>
using namespace std;

// Write performOperation() function declaration here
double performOperation(string operation, double numberOne, double numberTwo)
{
    // you need to provide the implementation here.
    return 0.0;
}

int main() 
{
   double numberOne, numberTwo;              	
   string operation;
   double result; 	
						
   cout << "Enter the first number: ";
   cin >> numberOne; 
   cout << "Enter the second number: ";
   cin >> numberTwo; 
   cout << "Enter an operator (+.-.*,/): ";
   cin >> operation;
		
   // Call performOperation method here		
  result = performOperation(operation, numberOne, numberTwo);

   cout << numberOne;
   cout << " " << operation << " ";
   cout << numberTwo;
   cout << " = ";
   cout << result << endl;
	
   return 0;

} // End of main() function
	
	
// Write performOperation function here
int performOperation(double numberOne, double numberTwo,std::string(op)){
double result;
if (op == "+")
result = numberOne+numberTwo;
else if (op == "-")
result = numberOne-numberTwo;
else if (op == "*")
result = numberOne*numberTwo;
else if (op == "/")
result = numberOne/numberTwo;
else
cout<<"Wrong input. Please try again.";
return result;
}
You've written two different versions of performOperation, but you only call the one which does nothing.

Zero effort copy/paste from some other crap.
https://www.chegg.com/homework-help/questions-and-answers/please-fix-code-questions-1-writing-functions-return-value-summary-lab-complete-partially--q47950228

Cross-posted elsewhere
https://www.reddit.com/r/CodingHelp/comments/wgj12j/ive_been_trying_to_make_this_work_and_i_just_cant/

Copy/paste + begging isn't going to teach you anything.
Last edited on
As stated, you have two different functions with the same name. You're calling the function that returns 0.0.

To call the other function you want to give it the two doubles THEN the string. Notice the different parameters of your two functions - which is the only reason this code would compile.
To call the performOperation(double, double, string), you'll need to have a forward declaration before main(). Also the op parameter is weirdly specified...

Consider as a first change:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>

double performOperation(double numberOne, double numberTwo, const std::string& op);

int main() {
	double numberOne {}, numberTwo {};
	std::string operation;

	std::cout << "Enter the first number: ";
	std::cin >> numberOne;

	std::cout << "Enter the second number: ";
	std::cin >> numberTwo;

	std::cout << "Enter an operator (+.-.*,/): ";
	std::cin >> operation;

	double result { performOperation(numberOne, numberTwo, operation) };

	std::cout << numberOne << " " << operation << " " << numberTwo << " = " << result << '\n';
}

double performOperation(double numberOne, double numberTwo, const std::string& op) {
	if (op == "+")
		return numberOne + numberTwo;

	if (op == "-")
		return numberOne - numberTwo;

	if (op == "*")
		return numberOne * numberTwo;

	if (op == "/")
		return numberOne / numberTwo;

	std::cout << "Wrong input. Please try again.";
	return 0;
}


If switch() has been covered, then this could be used in performOperation() rather than multiple if statements.
Last edited on
I fixed the issue but I do agree with seeplus that harder the way it wants me to do it. I didn't realize I had it return to 0,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
using namespace std;

// Write performOperation() function declaration here
double performOperation(double, double, string);
int main() 
{
   double numberOne, numberTwo;              	
   string operation;
   double result; 	
						
   cout << "Enter the first number: ";
   cin >> numberOne; 
   cout << "Enter the second number: ";
   cin >> numberTwo; 
   cout << "Enter an operator (+.-.*,/): ";
   cin >> operation;
		
   // Call performOperation method here		
   result = performOperation(numberOne, numberTwo, operation);

   cout << numberOne;
   cout << " " << operation << " ";
   cout << numberTwo;
   cout << " = ";
   cout << result << endl;
	
   return 0;

} // End of main() function
	
	
// Write performOperation function here
double performOperation(double numOne, double numTwo, string oper)
{
  double result = 0;
   if(oper == "+")
      result = numOne + numTwo;
   else if(oper == "-")
      result = numOne - numTwo;
   else if(oper == "*")
      result = numOne * numTwo;
   else if(oper == "/")
      if(numTwo == 0)
         cout << "Division by 0 not allowed." << endl;
      else
         result = numOne / numTwo;
   else
      cout << "Error. Not a valid operator." << endl;
   return result;
} 
its fine, but as shown above, the result variable is not needed and the elses are also not needed because return ends the function (so to get to the next condition, not returning is sufficient, it has an implied else because of how return works). The compiler will probably do the exact same thing anyway, so this is just decluttering.

also consider braces on all compound statements (loops, conditions, etc). Its a little bit of clutter for one liners but it will save the day when it stops a bug where you added a 2nd line in a loop or whatnot. Here, it lets you do this:

1
2
3
4
5
6
7
8
9
if(oper == "/")
{
      if(numTwo == 0)
         {
            cout << "Division by 0 not allowed." << endl;
            return = 0.0;
          }        
         result = numOne / numTwo;
}


Last edited on
1
2
if (op == "/")
		return numberTwo ? numberOne / numberTwo : !(std::cout << "Division by 0 not allowed.\n");

Registered users can post here. Sign in or register to post.