Public Group

# Need yet some more help!

This topic is 4074 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello again! Since last night's project was a complete failure, I've moved down a step: making a basic operations calculator... and... I've seemed to fail again, but something tells me it's close to operational, so I'm not giving up on this one. (wow complete run-on sentence XD) Something's wrong in my code, but since I'm just a beginner, I can't pin-point it. Check it out:
#include <iostream>
using namespace std;

int main()
{
int a;
int b;
char c;
int d;

cout << "\n\n";
cout << "                               - BASIC CALCULATOR -\n\n\n\n\n";
cin >> a;
cout << "\n\n";
cin >> b;
cout << "\n\n";
cout << "                         What operation would you like to use?\n";
cout << "              ('p' for plus,'m' for minus,'x' for multiply,'d' for divide)\n";
cout << "                               > ";
cin >> c;
cout << "\n\n";
cout << "                                    That equals: " << d << "\n\n\n\n\n";

if (c = "p")
{
d = (a + b);
}

else if (c = "m")
{
d = (a - b);
}

else if (c = "x")
{
d = (a * b);
}

else if (c = "d")
{
d = (a / b);
}

system("Pause");
}


Thanks ahead of time for any help given! EDIT*: btw, I did originally have it as +,-,*,/ for input instead of p,m,x,d. however, this seemed simpler in the long run

##### Share on other sites
For conditional statements, you use the == operator. = is only for assigning variables. Also, "Pause" is in all caps I believe. main() is a function with type int, so it must return an int as well, by adding the line 'return 0;' at the very end.

 #include <iostream>using namespace std;int main(){	int a;	int b;	char c;	int d;	cout << "\n\n";	cout << "                               - BASIC CALCULATOR -\n\n\n\n\n";	cout << "                          Please enter your first number: ";	cin >> a;	cout << "\n\n";	cout << "                          Please enter your second number: ";	cin >> b;	cout << "\n\n";	cout << "                         What operation would you like to use?\n";	cout << "              ('p' for plus,'m' for minus,'x' for multiply,'d' for divide)\n";	cout << "                               > ";	cin >> c;	cout << "\n\n";	cout << "                                    That equals: " << d << "\n\n\n\n\n";	if (c == "p")	{		d = (a + b);	}	else if (c == "m")	{		d = (a - b);	}	else if (c == "x")	{		d = (a * b);	}	else if (c == "d")	{		d = (a / b);	}	system("PAUSE");        return 0;}

##### Share on other sites
Also for character literals you need to use single quotes, not double quotes.

So, you'd want:
if(c == 'p')

and such.

Also, your code is out of order. You print out 'd' before you ever calculate it.

And, as an added note, you should check for division by zero in your divide chunk. Not necessary, but it's a good idea.
if(c == 'd'){    if(b == 0)        d = 0;    else        d = (a / b);}

[Edited by - Programmer16 on July 27, 2007 1:22:03 AM]

##### Share on other sites
Also note that "a" and 'a' (Note the quotations!) are different.

"A" is a two byte character string.

'A' is a single character constant.

Because of this, some compiliers will flag this as an error:
if (c == "p")

This should be:
if (c == 'p')

...For all of your if statements.

I also noticed you are using varable d without initializing it.
This is problimatic, as it contains garbage values.

##### Share on other sites
thank you guys SO much. i'll put these revisions into effect immediately

##### Share on other sites
Programmer16:
Shouldn't it be...
if(c == 'd'){    if(b == 0)        d = 0;    else        d = (a / b);}
...? Unless i'm mistaken, even if "b == 0" did evaluate to true, it would still execute "d = (a / b)".

##### Share on other sites
Quote:
 Original post by CrypterI also noticed you are using varable d without initializing it.This is problimatic, as it contains garbage values.

To explain further, you need to do the calculation before you output the result. This is for the same reason that you can't eat a sandwich and *then* make the sandwich you just ate.

##### Share on other sites
Quote:
 Original post by sotsilentProgrammer16:Shouldn't it be...*** Source Snippet Removed ***...? Unless i'm mistaken, even if "b == 0" did evaluate to true, it would still execute "d = (a / b)".

IT WAS TEST! <_< >_>
...
Yea, I missed my else. That's what I get for not sleeping lol. Thanks!

- @ Zahlman:
Your signature is a bit misleading. Some people might start thinking that using character literals is a bad idea.

1. 1
2. 2
Rutin
20
3. 3
4. 4
frob
15
5. 5

• 10
• 9
• 14
• 9
• 33
• ### Forum Statistics

• Total Topics
632592
• Total Posts
3007295

×