I am trying to create a simple Math Solver Function.

The goal of this program is to allow the user to enter 3 numbers, and respectively carry out either Subtraction, Addition, Multiplication, or Division.


I'm fairly new. I think I've got the program down for the most part, but

inside my if statements like "Addnumbers(x,y,z);"

I get an error saying the identifiers are undefined.
How can I fix this??



#include <iostream>
#include <iomanip>

using namespace std;


int Addnumbers(int x, int y, int z)
{
int S;
S = x + y + z;

cout << "Please input 3 numbers" << endl;
cin >> x >> y >> z;

cout << "The three numbers added were " << x << " " << y <<" "<< z;

cout << "The solution to the addition problem is " << S;

return S;


}

int Subtraction(int x, int y, int z)
{
int S;
S = x - y - z;

cout << "Please input 3 numbers to subtract" << endl;
cin >> x >> y >> z;

cout << "The three numbers subtracted were " << x << y << z;

cout << "The solution to the subtraction problem is " << S;




return S;
}

int Division(int x, int y, int z)
{
int S;
S = x / y / z;

cout << "Please input 3 numbers to divide" << endl;
cin >> x >> y >> z;

cout << "The three numbers divided were " << x << y << z;

cout << "The solution to the division problem is " << S;

return S;



}
int Multiply(int x, int y, int z)
{
int S;
S = x * y * z;

cout << "Please input 3 numbers to divide" << endl;
cin >> x >> y >> z;

cout << "The three numbers multiplied were " << x << y << z;

cout << "The solution to the multiplication problem is " << S;

return S;
}
int main()
{


int submission;
cout << " Welcome to my math solver program! " << endl;

cout << " Please enter 1 for Addition, 2 for subtraction, 3 for Division, and 4 for multiplication " << endl;
cin >> submission;

if (submission == 1)
{

Addnumbers(x,y,z);




}
if (submission == 2)
{

Subtraction(x,y,z);




}
if (submission == 3)
{

Division(x, y, z);




}
if (submission == 4)
{

Multiply(x, y, z);




}



system("PAUSE");
return 0;
}
First, please use code tags.
1
2
3
4
5
6
7
8
9
10
int Subtraction(int x, int y, int z)
{
  int S;
  S = x - y - z;
  cout << "Please input 3 numbers to subtract" << endl;
  cin >> x >> y >> z;
  cout << "The three numbers subtracted were " << x << y << z;
  cout << "The solution to the subtraction problem is " << S;
  return S;
}

Easier to read, isn't it?

This function has some issues.
It is called with three values. That is ok.
You compute the value of S. That is ok, but we'll return to it.

You replace the values of x, y, z within the function. That affects only the following
cout << "The three numbers subtracted were " << x << y << z;

Statements are executed in order:
1
2
foo = bar;
bar = 42;

The value of bar is copied to foo before the value 42 is copied to bar.


You show the value of S (in itself ok), which has nothing to do with the values that you did show on the previous line. That will confuse the user.

You do return the value of S. That is ok. (However, your main() does nothing with the value.)

Why does the function show or ask anything? It has one task already: to compute. I/O with user is separate.


What does "number to subtract" mean? Subtract from what?
Is it intuitive that "subtraction of three numbers" means subtraction of two values from a third?


I get an error saying the identifiers are undefined.

This is your code:
1
2
3
4
5
6
7
8
9
10
int main()
{
  int submission;
  cout << " Welcome to my math solver program! " << endl;
  cout << " Please enter 1 for Addition, 2 for subtraction, 3 for Division, and 4 for multiplication " << endl;
  cin >> submission;

  if (submission == 1)
  {
    Addnumbers(x,y,z); // error: what is x? what is y? what is z? 

You use x, y and z, but you have not declared, nor set their values.
How is the compiler supposed to know what to do?
Essentially what I want to do is have the user choose whether they want to
Add, Subtract, Multiply, or Divide 3 numbers.

Once they pick which operation they prefer, the function would take place, allowing them to input 3, and see the result.
1
2
3
4
5
6
7
void foo()
{
  double a {};
  std::cin >> a;  // allow input
  auto b = a;     // do math
  std::cout << b; // see result
}
Last edited on
Topic archived. No new replies allowed.