Hi, I'm new to the forum but I'm a computer engineering student at an engineering school so I will probably frequent here. I'm having a problem with creating a class based calculator. Our TA wanted us to have a calculator work that based on two things: 1. The variables for the input numbers must be floats or doubles.

2. You must be able to type in the operation you want after inputting the variables with a string. I.e, "summation" for addition and "subtraction" for subtraction. I'm going to copy-paste my visual studio code, and would love if someone could look at it and tell me why I'm getting extraneous results in the console.

Code:

#include <iostream>

#include <string>

using namespace std;

class Calculator{

float firstVariable;

float secondVariable;

double result;

string option;

public:

void Summation(){

result = firstVariable + secondVariable;

cout << "Result of summation: " << result << endl;

}

void Subtraction(){

result = firstVariable - secondVariable;

cout <<"Result of subtraction: " << result << endl;

}

};

int main() {

Calculator myCalculator;

float firstVariable(0);

float secondVariable(0);

string option("");

cout << "Give two values for your variables first." << endl;

cout <<"Type 'summation' for addition of variables, 'subtraction' for subtraction of variables." << endl;

while(1){

cout << endl;

cin >> firstVariable;

cin >> secondVariable;

cin >> option;

if (option == "summation"){

myCalculator.Summation();

}

else if (option == "subtraction"){

myCalculator.Subtraction();

}

cin.get();

cin.get();

}

return 1;

}

Thanks again!

2. You must be able to type in the operation you want after inputting the variables with a string. I.e, "summation" for addition and "subtraction" for subtraction. I'm going to copy-paste my visual studio code, and would love if someone could look at it and tell me why I'm getting extraneous results in the console.

Code:

#include <iostream>

#include <string>

using namespace std;

class Calculator{

float firstVariable;

float secondVariable;

double result;

string option;

public:

void Summation(){

result = firstVariable + secondVariable;

cout << "Result of summation: " << result << endl;

}

void Subtraction(){

result = firstVariable - secondVariable;

cout <<"Result of subtraction: " << result << endl;

}

};

int main() {

Calculator myCalculator;

float firstVariable(0);

float secondVariable(0);

string option("");

cout << "Give two values for your variables first." << endl;

cout <<"Type 'summation' for addition of variables, 'subtraction' for subtraction of variables." << endl;

while(1){

cout << endl;

cin >> firstVariable;

cin >> secondVariable;

cin >> option;

if (option == "summation"){

myCalculator.Summation();

}

else if (option == "subtraction"){

myCalculator.Subtraction();

}

cin.get();

cin.get();

}

return 1;

}

Thanks again!

The variables firstVariable and secondVariable in main are completely unrelated to and separate from the variables of the same name in class Calculator.

Topic archived. No new replies allowed.