Help Please

I am trying to make a basic calculator but my else statement is not working.
Please tell me how to fix it.
Here is the code.
|
|
V

#include <iostream>

using namespace std;

int main()
{
int a;
int b;
int sum;
int start;

cout <<"Enter 1 to start calculator\n";
cin >> start;

if(start = 1)
{
cout <<"Starting calculator...";
}
else
{
return 0;
}

cout <<"Enter first number.\n";
cin >> a;

cout <<"Enter another number.\n";
cin >> b;

sum = a + b;
cout << "The sum is: " << sum << endl;

return 0;
}

Thanks
if(start = 1)
= is an assignment statement
== compares the equality of values
So how do I make it so it will run the else statement?
if(start == 1)
oh lol.
Thanks
I tried making a more complicated one...
But I am still getting an error message.
Please Help...
Here is the code

#include <iostream>

using namespace std;

int main()
{
int a;
int b;
int sum;
int start;
int sign;

cout <<"Press 1 to start the calculator\n";
cin >> start;

if(start == 1)
{
cout <<"Starting Calculator...\n";
}
else
{
return 0;
}
cout <<"Enter the first number\n";
cin >> a;

cout <<"Enter the sign ( + , - , * , or /\n";
cin >> sign;

cout <<"Enter another number.\n";
cin >> b;

if(sign == "+")
{
sum = a + b;
cout <<"The answer of" << a << sign << b << "is:" << sum << endl;
}
else
{
return 0;
}
return 0;
}
closed account (j3Rz8vqX)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//...
    if(sign == "+")
    {
        sum = a + b;
        cout <<"The answer of" << a << sign << b << "is:" << sum << endl;
    }
    else if(sign == "-")
    {
        //Do something...
    }
    else if(sign == "*")
    {
        //Do something...
    }
    else if(sign == "/")
    {
        //Do something...
    }
    else
    {
        cout<<"You entered an invalid sign."<<endl;
    }
    return 0;
}
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
#include <iostream>

using namespace std;

int main()
{
int a;
int b;
int sum;
int start;
char sign;//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

cout <<"Press 1 to start the calculator\n";
cin >> start;

if(start == 1)
{
cout <<"Starting Calculator...\n";
}
else
{
return 0;
}
cout <<"Enter the first number\n";
cin >> a;

cout <<"Enter the sign ( + , - , * , or /\n";
cin >> sign;

cout <<"Enter another number.\n";
cin >> b;

if(sign == '+')//<<<<<<<<<<<<<<<<<<
{
sum = a + b;
cout <<"The answer of" << a << sign << b << "is:" << sum << endl;
}
else
{
return 0;
}
return 0;
}
You've declared sign to be an int - seems like you want it to be a char or string.

If you make it a char it would be if(sign == '+')
closed account (j3Rz8vqX)
Good catch!
here is my code

#include <iostream>

using namespace std;

int main()
{
int a;
int b;
int sum;
int start;
int sign;

cout <<"Press 1 to start the calculator\n";
cin >> start;

if(start == 1)
{
cout <<"Starting Calculator...\n";
}
else
{
return 0;
}
cout <<"Enter the first number\n";
cin >> a;

cout <<"Enter the sign ( + , - , * , or /\n";
cin >> sign;

cout <<"Enter another number.\n";
cin >> b;

if(sign == "+")
{
sum = a + b;
cout <<"The answer of" << a << sign << b << "is:" << sum << endl;
}
else if(sign == "-")
{
sum = a - b;
cout <<"The answer of" << a << sign << b << "is:" << sum << endl;
}
else if(sign == "*")
{
sum = a * b;
cout <<"The answer of" << a << sign << b << "is:" << sum << endl;
}
else if(sign == "/")
{
sum = a / b;
cout <<"The answer of" << a << sign << b << "is:" << sum << endl;
}
else
{
cout<<"You entered an invalid sign."<<endl;
}
return 0;
}


One problem...
On lines 33, 38, 43, and 48 I get this error


Final\main.cpp|33|warning: comparison with string literal results in unspecified behaviour [-Waddress]|
C:\Users\Stoltz\Desktop\Codeblock Projects\Calculator Final\main.cpp|33|error: ISO C++ forbids comparison between pointer and integer [-fpermissive]|

Thanks.
you should read the answers in here.
Oops didn't see the answer after dput's first response...
I just started C++. I don't know what char is...
can you explain it to me please?
Thanks alot!
Last edited on
Character types: They can represent a single character, such as 'A' or '$'. The most basic type is char, which is a one-byte character. Other types are also provided for wider characters.


http://www.cplusplus.com/doc/tutorial/variables/
Thanks.
Topic archived. No new replies allowed.