i need some help please!!

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;
}


return 0;
}
i fixed everything but 1 error:


#include <iostream>

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" << 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'



[b]PLEASE HELP!!![/b]
Code tags so can see what is line 56.

char's must be single characters between single quotes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>

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" << 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;
}
Last edited on
How do you assign this ?
char type = 'add', 'subtract','multiply','divide';

type is a char variable which holds only one value not 4 values.
make it array and then try .
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?
> what data type do i use for array?
> i want "type" to = those 4 things

Just make it a char; you don't need an array for this. Something like:

1
2
3
4
5
6
7
8
9
10
11
const char PLUS = '+' ;
const char MINUS = '-' ;
// etc

std::cout << "pick a math operation + - / * " ;
char operation ;
std::cin >> operation ;

if( operation == PLUS ) { /* ... */ }
else if( operation == MINUS ) { /* ... */ }
// else if ... 
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();
}





}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 ;
}


Likewise, for the other functions.
Topic archived. No new replies allowed.