Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help me (c++ calculator) 1

Status
Not open for further replies.

denisfinn

Programmer
Jun 7, 2003
49
US
I am trying to make a calculator!
I have been doing c++ for like a week and I thought I could do it.
Appartently I cannot.
Here is the source I wrote for the whole thing:

#include <iostream>

int multiply (int firstn, int secondn);
int divide (int firstn, int secondn);
int add (int firstn, int secondn);
int subtraction (int firstn, int secondn);

int main()
{
int choice;
int firstn;
int secondn;
int answer;

std::cout << &quot;Choose which you wish to do: [multiply (1), divide (2), add (3), or subtract (4)]&quot;;
std::cin >> choice;

if (choice == 1)
std::cout << &quot;\nEnter two numbers to multiply: &quot;;
std::cin >> firstn;
std::cout << &quot;\nSecond number: &quot;;
std::cin >> secondn;

answer = multiply(firstn, secondn);
std::cout << &quot;\n\nWOOOOLAHHHH! And the answer is: &quot; << answer << &quot;\n&quot;;



if (choice == 2)
std::cout << &quot;\nEnter two numbers to divide(it will be rounded): &quot;;
std::cin >> firstn;
std::cout << &quot;\nSecond number: &quot;;
std::cin >> secondn;

answer = divide (firstn, secondn);
std::cout << &quot;\n\nWOOOOLAHHHH! And the answer is: &quot; << answer << &quot;\n&quot;;



if (choice == 3)
std::cout << &quot;\nEnter two numbers to add: &quot;;
std::cin >> firstn;
std::cout << &quot;\nSecond number: &quot;;
std::cin >> secondn;

answer = subtraction (firstn, secondn);
std::cout << &quot;\n\nWOOOOLAHHHH! And the answer is: &quot; << answer << &quot;\n&quot;;


if (choice == 4)
std::cout << &quot;\nEnter two numbers to subtract: &quot;;
std::cin >> firstn;
std::cout << &quot;\nSecond number: &quot;;
std::cin >> secondn;

answer = add(firstn, secondn);
std::cout << &quot;\n\nWOOOOLAHHHH! And the answer is: &quot; << answer << &quot;\n&quot;;

return 0;
}
int multiply (int firstn, int secondn)
{
return (firstn * secondn);
}

int divide (int firstn, int secondn)
{
return (firstn / secondn);
}

int add (int firstn, int secondn)
{
return (firstn + secondn);
}

int subtraction (int firstn, int secondn)
{
return (firstn - secondn);
}

If you could figure what I am doing wrong it would be very appreciated :)!

Thanks,
Denis

Ps: I DO NOT use Visual C++ so if you could help me in normal C++ it would be even cooler!
 
hi, Great effort for just 1 week of study :) There are a few things you can do to improve your code though. First, to get it to compile, you'll need to place opening and closing parenthesis around the code in your if statement. This is necessary whenever you have more than one line of code to execute in your if statement.

if (choice == 1)
{
std::cout << &quot;\nEnter two numbers to multiply: &quot;;
std::cin >> firstn;
std::cout << &quot;\nSecond number: &quot;;
std::cin >> secondn;

answer = multiply(firstn, secondn);
std::cout << &quot;\n\nWOOOOLAHHHH! And the answer is: &quot; << answer << &quot;\n&quot;;
}

To save you some time and effort, open up namespace std before you use declarations from it. This will allow the compiler to see the declarations without you having to explicitly tell it what namespace they belong to. You do this by using the following syntax &quot;using namespace std;&quot; (for the std namespace). So, for every instance of std::cin or std::cout, you would just have cin or cout. Heres what the code would look like after you do this:

using namespace std;

int main()
{
int choice;
int firstn;
int secondn;
int answer;

cout << &quot;Choose which you wish to do: [multiply (1), divide (2), add (3), or subtract (4)]&quot;;
cin >> choice;
//and so on.....

Also, be sure to check the value of secondn before you perform a division. Don't want a divide by zero error afterall... Well, hope you found my post helpful :)

 
thanks man,

you're really helpful!

and it works now!
 
By the way...
using namespace std ...
how can I create my own namespace for my classes,
how can I reset a namespace, from std to MyNamespace?

thanx

Greetings Andreas
 
Nothing to it. Suppose you wanted to create a namespace called &quot;MyNamespace&quot;, this is all you would have to do:

//In some header file, maybe &quot;MyNamespace.h&quot;
namespace MyNamespace
{
//Declarations go here
}

And when one of your .cpp files needs to see the declarations in that namespace, you would include the following line in that .cpp file like so:

//.cpp file that needs to see the declarations in MyNamespace
using namespace MyNamespace;

As for resetting std, std is the namespace used to contain the standard C++ library declarations. Thats why you needed to use &quot;using namespace std;&quot; before you could use cout and cin, since they are part of that library. So you wouldn't want to rename std to something else. If your still not clear on something, let me know...

 
thx, fine to know how to create an own namespace.
In the actual work I dont have the problem, but some weeks ago we had the problem that stream objects needed a namespace and CStrings did not work with that. This seems clear to me, because &quot;using namespace std&quot; tells the compiler that the names it has to interpret are called like std::..., but CString doesn't.
As far as I remember we changed the sorting of includes until it matched.

But this was a very annoying work. Thats why it would be interesting for me, whether you can switch between namespaces in one file, and how to.

Do you know?


Greetings Andreas
 
hi, I've never worked with MFC before so I don't know the specifics of using CString. Still, I don't see why there would be a problem unless some of the declarations in the std namespace were identical to those in the CString header (the whole point of using a namespace). Its possible that the problem was due to something else. Do you remember what kind of errors you received?
 
no sorry, dont remember.
but do you know how to switch between namespaces, something like

use namespace std;

.. some code ..

use namespace MyNameSpace;

.. some code ..

use no namspace;

.. some code ..

Could this work?

Greetings Andreas
 
As far as I know, there is no way to stop using a particular namespace once you start using it with the &quot;using namespace&quot; statement. Also, when you use multiple &quot;using namespace&quot; statements, you will be using all the namespaces simultaneously. Maybe an example will help, lets say you have the following namespaces:

namespace ns1
{
void ns1_funct(void);
}

namespace ns2
{
void ns2_funct(void);
}


The code to use them would then be:

//Open namespace ns1
using namespace ns1;
//Valid since namespace ns1 was opened
ns1_funct();

//Invalid since namespace ns2 is closed - will generate a compiler error if compiled as is
ns2_funct();


//Open namespace ns2
using namespace ns2;
//Valid now that namespace ns2 has been opened
ns2_funct();

//Still valid since namespace ns1 was opened previously
ns1_funct();


Hope this helps more than it confuses...


 
I would not recommend anyone to use &quot;using namespace&quot; - since would never do it myself.

Sure you will have to type a bunch of std::'s (or whatever namespace) but the code get clearer (in my opinion) since you can by a glance see exactly what function you are calling and don't have to worry about stuff like &quot;is-it-really-what-I-think-it-is?&quot;.

And the whole problem of using stuff from multiple namespaces simply disappears...



Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top