Jump to content
  • Advertisement
Sign in to follow this  
IN1L8

Need yet some more help!

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

If you intended to correct an error in the post then please contact us.

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



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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Crypter
I 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 this post


Link to post
Share on other sites
Quote:
Original post by sotsilent
Programmer16:
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!