Very new to coding, so please be understanding 😉
I’m basically trying to make a calculator using the while loop, and if statements.
#include <iostream>
using namespace std;
int x = 1;
int number;
int total = 0;
int amt = 1;
int a;
int b;
int c;
int d;
string ans;
class OperateClass
I get the error: two or more data types in declaration of ‘main’
Please explain what this means/how to fix it.
I was also wondering if I need to make a new object for each function (Add, subtract, multiply, and divide)
Please help!
int main()
{
cout << "Do you want to add, subtract, multiply or divide? I want to : " << endl;
cin >> ans;
if(ans == "add"){
OperateClass opOper;
opOper.add();
}else{
if(ans == "subtract"){
OperateClass opOper
opOper.subtract();
}
}else{
if(ans == "multiply"){
OperateClass opOper
opOper.multiply();
}
}else{
if(ans == "divide"){
OperateClass opOper
opOper.divide();
}
}
}
class OperateClass{
public:
int add(){
while(x <= 3){
cout << "Enter a number to use to add: " << endl;
cin >> a;
total = total + a;
x++;
amt++;
}
}
int subtract(){
while(x <= 3){
cout << "Enter a number to use to add: " << endl;
cin >> b;
total = total - b;
x++;
amt++;
}
}
int multiply(){
while(x <= 3){
cout << "Enter a number to use to add: " << endl;
cin >> c;
total = total * c;
x++;
amt++;
}
}
int divide(){
while(x <= 3){
cout << "Enter a number to use to add: " << endl;
cin >> d;
total = total / d;
x++;
amt++;
}
}
}
int print(){
cout << "Your total is: " << total << endl;
return 0;
}
None of that code is valid. You begin a class definition with
class OperateClass, but never end it and run right intomain. A (simplified) class definition takes the form of:Next, you declare
main… but it leads to anelsebranch (?).Functions must also terminate via their closing brace, i.e.,
Now it looks like those may just be copy/paste errors. If that’s the case then you probably just forgot the semi-colon at the end of the class definition.