Hello,
it the same old program calculator
i have corrected errors that were preventing me from running the program. Now that i have done it i am facing another issue; It doesnt perform needed computations such as +-* and etc. It must be my switch fn. Any help will be appreciated

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


// Functions prototypes
void instruct();
char displayMenu(char);
float do_next_op(char, float, float);




int main()
{
	char Q, q;
	char choice;				// option to the user to quit or to continue
	char oper;
	float accumVal = 0;;
	float num;
    string input;

	 // Instructions function

	instruct();					// Greetings and Instructions


	do
	{

	    cout << "Please enter the operator first and then the number or type q or Q to exit     the program: " << endl;
        cin >> input;
        
	    oper= input.at(0);
	    accumVal = do_next_op(oper, num, accumVal);
	    cout << accumVal << endl;

	} while (oper != 'q' || oper != 'Q');


	cout << "Your Final Result is: " << accumVal << endl;

	//cout << "Please type in your choice: " << endl;
	//cin >> choice;		//choice = displayMenu(choice);			// Menu function call


	system("pause");
	return 0;
}

// Functions defenitions
float do_next_op(char operat, float num, float accumVal)/*The function below deteremines  the
														 user input and does the appropriate calculations*/
{

	accumVal = 0;
	cout << "Please choose your command: " << operat << endl;
	cin >> operat;

	switch(operat)
	{
		case 'q': case 'Q':
			cout << "Your Final Result is: " << accumVal << "Thank you for using this calculator." << endl;
			break;
		case '+':
			accumVal += num;
			cout << "Result so far: " << accumVal << endl;
			break;
		case '-':
			accumVal -= num;
			cout << "Result so far: " << accumVal << endl;
			break;
		case '*':
			accumVal *= num;
			cout << "Result so far: " << accumVal << endl;
			break;
		case '/':
			accumVal /= num;
			cout << "Result so far: " << accumVal << endl;
			break;
		case '^':
			accumVal = pow(accumVal, num);
			cout << "Result so far: " << accumVal << endl;
			break;
		default:
			cout << "You have entered an INVALID OPERATOR! " << endl;
	}
	return accumVal;

}
void instruct()			// Instructions
{
	cout << "Welcome to this program - Calculator. This calculator was designed to ";
	cout << "assist user with basic calculations. You will have a choice an option exit";
	cout << "this program by simply typing q when prompted after each computation";
	cout << "Thank you again and good luck in your calculations!" << endl;

}

/*bool displayMenu(bool choice)		// Menu function
{
	float accumVal;

	switch(choice)
	{
		case 'q': case 'Q':
			cout << "Your Final Result is: " << accumVal << "Thank you for using this calculator." << endl;
			break;


	}
	return choice;

}*/

Recommended Answers

All 2 Replies

Where did you assign the value for variable num?

You don't need to declare Q and q as chars to use 'Q' and 'q'. You also have an unreferenced variable (ie, a variable you declared but never used).

Take a closer look at the logic in the while loop. It won't execute as you intended. It will always be true, so the program will never break out of it.

Take a look at first line of code in do_next_op(); does it make sense for it to be there?

You also forgot to get some additional input from the user, which is why the program prints out garbage data (if you're using VC++, you'll get a run-time error).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.