hi everybody, im kind of a beginner to c++ but i have been doing java for a while, im trying to make a basic all function calculator with c++ (add, subtract, multiply divide)... i made it but i keep getting errors, can anybody help?? heres the code:
#include <iostream>
using namespace std;
int main()
string type = add,subtract,multiply,divide;
int an1;
int an2;
int as = an1 + an2;
int sn1;
int sn2;
int ss = sn1 - sn2;
int mn1;
int mn2;
int ms = mn1 * mn2;
int dn1;
int dn2;
int ds = dn1/dn2;
cout<< "choose a math type: \n";
cout<< "add, subtract, multiply or divide? \n";
cin >> type;
if(type = add){
cout << "choose your first number: \n";
cin >> an1;
cout << "choose your second number : \n";
cin >> an2;
cout << "your sum is : \n" << as;
}else if (type = "subtract"){
cout << "choose your first number :\n";
cin >> sn1;
cout << "choose your second number \n";
cin >> sn2;
cout << "your difference is: \n" << ss;
}else if (type = "multiply"){
cout << "choose your first number :\n";
cin >> mn1;
cout << "choose your second number \n";
cin >> mn2;
cout << "your product is: \n" << ms;
}else {
cout << "choose your first number :\n";
cin >> dn1;
cout << "choose your second number \n";
cin >> dn2;
cout << "your quotient is: \n" << ds;
}
cout << "enter a number: \n";
cin >> a;
cout << "enter another number: \n";
cin >> b;
cout << "your sum is: \n" << c;
return 0;
}
int subtract(){
int t;
int u;
int v = t - u;
cout << "enter a number: \n";
cin >> t;
cout << "enter another number: \n";
cin >> u;
cout << "your difference is: \n" << v;
return 0;
}
int multiply(){
int d;
int e;
int f = d * e;
cout << "enter a number: \n";
cin >> d;
cout << "enter another number; \n";
cin >> e;
cout << "your product is: \n" << f;
}
int divide(){
int x;
int y;
int z = x/y;
cout << "enter a number: \n";
cin >> x;
cout << "enter another number: \n";
cin >> y;
cout << "your quotient is: \n" <<z;
}
int main(){
char type = 'add', 'subtract','multiply','divide';
cout << "pick a math type: \n" <<type;
cin >> type;
if (type = 'add'){
adding();
}else if (type = 'subtract'){
subtract();
}else if (type = 'multiply'){
multiply();
}else{
divide();
}
return 0;
}
the error is :
line 56-- error: expected unqualified-id before '\x72616374'
#include <iostream>
usingnamespace std;
int adding(){
int a;
int b;
int c = a + b;
cout << "enter a number: \n";
cin >> a;
cout << "enter another number: \n";
cin >> b;
cout << "your sum is: \n" << c;
return 0;
}
int subtract(){
int t;
int u;
int v = t - u;
cout << "enter a number: \n";
cin >> t;
cout << "enter another number: \n";
cin >> u;
cout << "your difference is: \n" << v;
return 0;
}
int multiply(){
int d;
int e;
int f = d * e;
cout << "enter a number: \n";
cin >> d;
cout << "enter another number; \n";
cin >> e;
cout << "your product is: \n" << f;
}
int divide(){
int x;
int y;
int z = x/y;
cout << "enter a number: \n";
cin >> x;
cout << "enter another number: \n";
cin >> y;
cout << "your quotient is: \n" <<z;
}
int main(){
char type = 'add', 'subtract','multiply','divide';
cout << "pick a math type: \n" <<type;
cin >> type;
if (type = 'add'){
adding();
}elseif (type = 'subtract'){
subtract();
}elseif (type = 'multiply'){
multiply();
}else{
divide();
}
return 0;
}
what data type do i use for array? i want "type" to = those 4 things, and when i do make it an array how do i call upon the individual entities inside it, is it the same as im doing now?
okay, i have no more errors... however, when i type in "/" for the divide, i get this "Floating point exception"
whenever i do anything the result is "0", like if i do 10 + 10 i get "0" 10 * 10 is 0 and 5-2 is 0.
help?
heres my code as of right now:
#include <iostream>
#include <string>
using namespace std;
int adding(){
int a;
int b;
int c = a + b;
cout << "enter a number: \n";
cin >> a;
cout << "enter another number: \n";
cin >> b;
cout << "your sum is: \n\n" << c;
}
int subtract(){
int t;
int u;
int v = t - u;
cout << "enter a number: \n";
cin >> t;
cout << "enter another number: \n";
cin >> u;
cout << "your difference is: \n\n" << v;
}
int multiply(){
int d;
int e;
int f = d * e;
cout << "enter a number: \n";
cin >> d;
cout << "enter another number; \n";
cin >> e;
cout << "your product is: \n\n" << f;
}
int divide(){
int x;
int y;
int z = x / y;
cout << "enter a number: \n";
cin >> x;
cout << "enter another number: \n";
cin >> y;
cout << "your quotient is: \n\n" <<z;
}
int main(){
const char PLUS = '+' ;
const char MINUS = '-' ;
const char TIMES = '*';
const char DIVIDE = '/';
cout << "pick a math type + - / *: \n";
char type;
cin >> type;
if (type == PLUS){
adding();
}else if (type == MINUS){
subtract();
}else if (type == TIMES){
multiply();
}else{
divide();
}
int subtract()
{
int t;
cout << "enter a number: \n";
cin >> t;
int u;
cout << "enter another number: \n";
cin >> u;
int v = t - u;
cout << "your difference is: " << v << "\n\n" ;
return v ;
}