trying to create a calculator.

Sorry, pretty new but ambitious. I'm trying to be able to have the user type in "add" and storing this as "add" so the program will proceed to the correct if statement, I plan on just doing subtract divide and multiply and creating an error message. Anyways that's not really what I need help with. I just need help getting this part to function and I can figure out the rest. When I run this while inputting 2 and add, I get

Enter a number: 2
What would you like to do?
ex. add subtract divide or multiply. add
Enter a second number:2 <- I do not enter that.
Press ENTER to continue... <--- does not wait for enter
---------------------------------------------------------------------
Process exited after 5.521 seconds with return value 0
Press any key to continue . . .

So it seems like the if statement is not the problem. Unless for some reason they are getting saved as 0's. Anyways, I'm a little confused haha. Please help!
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
//This program is supposed to be a text based calculator.
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
int main()
{
  double i;
  char e;
  char add;
  char subtract;
  char divide;
  char multiply;
  double f;
  cout << "Enter a number: ";
  std::cin >> i;
  std::cout << "What would you like to do?\n ex. add subtract divide or multiply. ";
  std::cin >> e;
  //I'm sure I'm trying to declare this wrong. I need this to store the user input
  //in text form ie: user types in add and program goes to an if statement and runs
  //if statement.
  if (e= add);
  	{
  		std::cout << "Enter a second number: ";
  		std::cin >> f;
  		std::cout << i+f;
  	}

  std::cout << "\nPress ENTER to continue..." << std::endl;
  	getchar();
  return 0;
}
Last edited on
@sinfulstyle

if (e= add);

Couple of problems with this short line. One, you are using a assigning equals, instead of a comparing equals. That would be two equal signs. eg. == Second, you are using the semi-colon, which terminates the line. Correct those, then see what, if any, problems still arise.
you didnt attribute any value to the variable add, subtract, divide or multiply, so they are an empty variable.
check in my example, it may is not the best way but it works.
I made some changes.
there is a " ; " before "if", you dont need to use that.


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
#include <iostream>
//#include <stdio.h>
//#include <math.h>
#include <string>
using namespace std;
int main()
{
  double i;
  string e;
  //string add;
 // char subtract;
  //char divide;
 // char multiply;
  double f;
  cout << "Enter a number: ";
  cin >> i;
  cout << "What would you like to do?\n ex. add subtract divide or multiply. ";
  cin >> e;
  //I'm sure I'm trying to declare this wrong. I need this to store the user input
  //in text form ie: user types in add and program goes to an if statement and runs
  //if statement.
  if (e=="add")
  	{
  		cout << "Enter a second number: ";
  		cin >> f;
  		cout << i+f;
  	}

  std::cout << "\nPress ENTER to continue..." << std::endl;
  	getchar();

	system("pause");
  return 0;
}


Last edited on
@Whitenite1
Thank you, I had used == first but backspaced it for some reason.
@Wesp
Thank you, I didn't realize that I didn't need to define the strings. Your example program was just what I needed. It works as advertised now! I'll put in the finished program at the end but here is what I have now.
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
//This program is supposed to be a text based calculator.
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
int main()
{
  double i;
  string e;
  double f;
  cout << "Enter a number: ";
  std::cin >> i;
  std::cout << "What would you like to do?\n ex. add subtract divide or multiply. ";
  std::cin >> e;

  if (e=="add")
  	{
  		std::cout << "Enter a second number: ";
  		std::cin >> f;
  		std::cout << i+f;
  	}

  std::cout << "\nPress ENTER to continue..." << std::endl;
  	getchar();
  return 0;
}

Topic archived. No new replies allowed.