SolvedFixing Simple Calculator

Posts 1–4 of 4 · Page 1 of 1
Fixing Simple Calculator
Ok, I know almost nothing about C++, so I was wondering if anyone could assist me in fixing my simple learning base program. I mean not to disturb more skilled programmers with my lack of knowledge, but to gain more knowledge of my own.

My current program is just a simple input output command window calculator.

Ok, so my code follows as shown:
Code:
#include "stdafx.h"
#include <iostream>
#include "conio.h"
//using namespace std;

float num1;		// first integer
float num2;		// second integer
int add;		// addition [+]
int sub;		// subtraction [-]
int mul;		// multiplication[*]
int quo;		// answer to division [/]
int sum;		// answer to addition [+]
int difference; // answer to subtraction [-]
int product;	// answer to multiplication[*]
// =
int method;		// method e.g. ( +, -, *, / )

int _tmain(int argc, _TCHAR* argv[])
{
	std::cout << "Enter first integer/float: " << std::endl;
	std::cin >> num1;

	std::cout << "Enter second integer/float value: " << std::endl;
	std::cin >> num2;

	std::cout << "Enter method ( +, -, *, / )" << std::endl;
	std::cin >> method;

	if(method == '+') {
		sum = num1 + num2;
		std::cout << sum << std::endl;
	}
	else if(method == '-') {
		difference = num1 - num2;
		std::cout << difference << std::endl;
	}
	else if(method == '*') {
		product = num1 * num2;
		std::cout << product << std::endl;
	}
	else if(method == '=') {
		quo = num1 / num2;
		std::cout << quo << std::endl;
	}
	else {
		std::cout << "Unknown function, please use [ +, -, *, / ]" << std::cout;
	}
}
If anyone can help, without just doing it for me, I'd appreciate it so much. Any help is greatly appreciated. Thanks in advance!

When I say almost no knowledge of C++, I didn't mean I copied this code, I know Java and am trying to learn C languages now, I am not a code leech so please don't assume I ripped this code off.
When I execute the program, I can enter the two float/integer numbers, but when the program asks for the method e.g. add, subtract etc. And I enter + or add, it closes without considering the if, else if or printing the result (in my code as sum, difference, quo, product).

---------- Post added at 06:07 PM ---------- Previous post was at 06:03 PM ----------

Updated code:

Code:
#include "stdafx.h"
#include <iostream>
#include "conio.h"
//using namespace std;

// num1
int num1;		// first int
// num2
int num2;		// second int

// methods
int add;		// addition [+]
int sub;		// subtraction [-]
int mul;		// multiplication[*]
int divi;			// division [/]

// result types
int quo;		// answer to division [/]
int sum;		// answer to addition [+]
int difference; // answer to subtraction [-]
int product;	// answer to multiplication[*]

int method;		// method e.g. ( +, -, *, / )

int _tmain(int argc, _TCHAR* argv[])
{
	std::cout << "Enter first integer/float: " << std::endl;
	std::cin >> num1;

	std::cout << "Enter second integer/float value: " << std::endl;
	std::cin >> num2;

	std::cout << "Enter method ( +, -, *, / )" << std::endl;
	std::cin >> method;

	if(method == add) {
		sum = num1 + num2;
		std::cout << sum << std::endl;
	}
	else if(method == sub) {
		difference = num1 - num2;
		std::cout << difference << std::endl;
	}
	else if(method == mul) {
		product = num1 * num2;
		std::cout << product << std::endl;
	}
	else if(method == divi) {
		quo = num1 / num2;
		std::cout << quo << std::endl;
	}
	else {
		std::cout << "Unknown function, please use [ +, -, *, / ]" << std::cout;
	}
}


---------- Post added at 06:20 PM ---------- Previous post was at 06:07 PM ----------

Ok, I fixed the code, sorry for wasting time, I messed with it a little and got it work fine on VC++.net

/req close
/Closed .
Posts 1–4 of 4 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

Talk with us