SkidForums


Where skids go hackers
 
HomeFAQSearchMemberlistUsergroupsRegisterLog in

Share | 
 

 Simple calculator in C++

View previous topic View next topic Go down 
AuthorMessage
woefie
Owner
Owner
avatar

Awesome
Posts : 52
Points : 97
Reputation : 0
Join date : 2012-04-24

PostSubject: Simple calculator in C++   Wed Apr 25, 2012 4:43 am

explainations each code, hope u learn something from this
Code:

#include <iostream>
using namespace std;
int main()
{
    // One and two are the numbers the user selects
    double one, two;
   
    cout << "Please input the first number:"; // C-out GIVES YOU information (information comes out of computer)
    cin >> one; // Cin takes the users first number, and assosicates it to one // C-IN takes information to the computer ( you put info in comp)
    cout << "Please input the second number:";
    cin >> two; // Here cin takes the second number and assisgns it to two
    cout << "\nThe answers are:\nAddition:" << one + two; // All the math happens here
    cout << "\nSubtraction:" << one - two;
    cout << "\nMultiplication:" << one * two;
    cout << "\nDivision:" << one / two;
    cout << "\nCALCULATE SOME MORE PLZZZ SUMS ARE MY DRUGGGG.";
    cout << "\n";
    main(); // This starts the main function again, so you can do another calculation
   
    return 0; // return zero, means ur progam ran succesfully from top to the line with return 0;
}
Back to top Go down
View user profile http://skid.forum-leaks.com
Nikolai
C++ Master
C++ Master


Awesome k
Posts : 24
Points : 43
Reputation : 5
Join date : 2012-04-26

PostSubject: Re: Simple calculator in C++   Thu Apr 26, 2012 3:19 am

The problem with your calculator is, you shouldn't be calling main recursively, since there is no end.
Here is a basic example using a loop that will run the program five times.
Note this isn't the best way to do it and can get much more efficiant. But since you'll have trouble understanding it, I'll take your calculator and throw it in a loop.
Code:

#include <iostream>
using namespace std;
int main()
{
    // One and two are the numbers the user selects
    double one, two;
   
for (int i = 0; i < 6; ++i) //Begins the loop. Will keep running while i is less than 6.
{
    cout << "Please input the first number:"; // C-out GIVES YOU information (information comes out of computer)
    cin >> one; // Cin takes the users first number, and assosicates it to one // C-IN takes information to the computer ( you put info in comp)
    cout << "Please input the second number:";
    cin >> two; // Here cin takes the second number and assisgns it to two
    cout << "\nThe answers are:\nAddition:" << one + two; // All the math happens here
    cout << "\nSubtraction:" << one - two;
    cout << "\nMultiplication:" << one * two;
    cout << "\nDivision:" << one / two;
    cout << "\nCALCULATE SOME MORE PLZZZ SUMS ARE MY DRUGGGG.";
    cout << "\n";
}
    return 0; // return zero, means ur progam ran succesfully from top to the line with return 0;
}
Back to top Go down
View user profile
woefie
Owner
Owner
avatar

Awesome
Posts : 52
Points : 97
Reputation : 0
Join date : 2012-04-24

PostSubject: Re: Simple calculator in C++   Thu Apr 26, 2012 4:42 am

ah lol thanks for feed back Very Happy
Back to top Go down
View user profile http://skid.forum-leaks.com
Nikolai
C++ Master
C++ Master


Awesome k
Posts : 24
Points : 43
Reputation : 5
Join date : 2012-04-26

PostSubject: Re: Simple calculator in C++   Thu Apr 26, 2012 12:23 pm

Another thing you should remember is not to infect the global namespace.
Code:

using namespace std;
What do I mean by that?
When you call the namespace std; by default you're allowing every object from the class to enter your source.
Meaning you don't have to declare each object seperately.
Code:

std::cout << "here you go\n";
The downside to that would be if you had another namespace you wanted in your source.
Code:

using namespace example;
Now every object is loaded from the example namespace.
Code:

using namespace std;
using namespace example;
So let's say your namespace involves the user of strings.
Code:

example::string;
//Well, std also has a string class.
std::string;
Now if I were to say string.
Code:

string SzString = "Hello world";
That could result in a problem where the compiler thinks you're using your example::string which
could possibly involve parameters, thus your code won't compile.
Also if you were to share the source with others, they might not know which string you are talking about.
So the best practice is to call each object you want to use in your source, seperately.
Code:

using std::cout;
using std::cin;
using std::endl;
using example::string;
using example::bin;
Hope it made some sense.
Back to top Go down
View user profile
woefie
Owner
Owner
avatar

Awesome
Posts : 52
Points : 97
Reputation : 0
Join date : 2012-04-24

PostSubject: Re: Simple calculator in C++   Fri Apr 27, 2012 2:03 am

and again, thanks lol affraid
hope ill remember this Razz
so if im gonna use cout somewhere i add using std::cout;?
Back to top Go down
View user profile http://skid.forum-leaks.com
Nikolai
C++ Master
C++ Master


Awesome k
Posts : 24
Points : 43
Reputation : 5
Join date : 2012-04-26

PostSubject: Re: Simple calculator in C++   Fri Apr 27, 2012 4:54 am

Well the point of placing
Code:

using std::cout;
Is so you won't have to use std::cout <<"Anywhere" ; in your source.
Code:

#include <windows.h>
#include <iostream>
#include <vector>
usint std::cout;
using std::cin;
using std::vector;
DWORD WINAPI hThread(LPVOID)
{
cout << "Running this thread simultaneously\n";
}
int main()
{
DWORD dwThreadID;
HANDLE hHandle = CreateThread(NULL,NULL,hThread,NULL,dwThreadID);
int x;
vector<int> vArray;
while(int i !=10)
{
cin >> x;
vArray.push_back(x);
++i;
}
vArray.clear();
return 0;
}
Back to top Go down
View user profile
woefie
Owner
Owner
avatar

Awesome
Posts : 52
Points : 97
Reputation : 0
Join date : 2012-04-24

PostSubject: Re: Simple calculator in C++   Fri Apr 27, 2012 5:03 am

aha......
Back to top Go down
View user profile http://skid.forum-leaks.com
Nikolai
C++ Master
C++ Master


Awesome k
Posts : 24
Points : 43
Reputation : 5
Join date : 2012-04-26

PostSubject: Re: Simple calculator in C++   Fri Apr 27, 2012 9:08 am

Sorry to cause confusion. But the whole point is.
Only use call the namespace objects which you are planning to use, nothing more.
And that could be achieved as I have shown in the above example.
Code:

using std::cout;
using std::cin;
Back to top Go down
View user profile
woefie
Owner
Owner
avatar

Awesome
Posts : 52
Points : 97
Reputation : 0
Join date : 2012-04-24

PostSubject: Re: Simple calculator in C++   Fri Apr 27, 2012 9:20 am

but why not,
using namespace std seems so much easier Very Happy?
Back to top Go down
View user profile http://skid.forum-leaks.com
Nikolai
C++ Master
C++ Master


Awesome k
Posts : 24
Points : 43
Reputation : 5
Join date : 2012-04-26

PostSubject: Re: Simple calculator in C++   Fri Apr 27, 2012 1:04 pm

I explained above why it wasn't a great idea. And why it's not great practice.
Back to top Go down
View user profile
woefie
Owner
Owner
avatar

Awesome
Posts : 52
Points : 97
Reputation : 0
Join date : 2012-04-24

PostSubject: Re: Simple calculator in C++   Sat May 05, 2012 9:02 am

If i run ur code above.
it adds 1001034013403140 of codes o-o
and gives a error
is it in C or C++?
Back to top Go down
View user profile http://skid.forum-leaks.com
Nikolai
C++ Master
C++ Master


Awesome k
Posts : 24
Points : 43
Reputation : 5
Join date : 2012-04-26

PostSubject: Re: Simple calculator in C++   Tue May 08, 2012 2:21 am

I'm sorry about that. When I was writing that out, I wasn't using an IDE nor was I checking for errors. This should compile just fin though.
Code:

#include <windows.h>
#include <iostream>
#include <vector>
using std::cout;
using std::cin;
using std::vector;
DWORD WINAPI hThread(LPVOID)
{
cout << "Running this thread simultaneously\n";
return 0;
}
int main()
{
DWORD dwThreadID;
HANDLE hHandle = CreateThread(NULL,NULL,hThread,NULL,NULL,&dwThreadID);
int x;
vector<int> vArray;
int i;
cout << "Enter 10 numbers->";
while(i < 10)
{
cin >> x;
vArray.push_back(x);
++i;
}
vector<int>::iterator Display;
cout << "Your vector contains ";
for (Display = vArray.begin(); Display<vArray.end();++Display)
{
   cout << " " << *Display;
}
vArray.clear();
return 0;
}
That should compile.
Back to top Go down
View user profile
woefie
Owner
Owner
avatar

Awesome
Posts : 52
Points : 97
Reputation : 0
Join date : 2012-04-24

PostSubject: Re: Simple calculator in C++   Tue May 08, 2012 3:33 am

okay lol Razz
Back to top Go down
View user profile http://skid.forum-leaks.com
Nikolai
C++ Master
C++ Master


Awesome k
Posts : 24
Points : 43
Reputation : 5
Join date : 2012-04-26

PostSubject: Re: Simple calculator in C++   Tue May 08, 2012 12:15 pm

You should build a new calculator. One that incorperates switches and functions. Use my the tutorials.
Back to top Go down
View user profile
woefie
Owner
Owner
avatar

Awesome
Posts : 52
Points : 97
Reputation : 0
Join date : 2012-04-24

PostSubject: Re: Simple calculator in C++   Wed May 09, 2012 12:27 am

i've made a new one.
but still isn´t pro xD
Back to top Go down
View user profile http://skid.forum-leaks.com
Nikolai
C++ Master
C++ Master


Awesome k
Posts : 24
Points : 43
Reputation : 5
Join date : 2012-04-26

PostSubject: Re: Simple calculator in C++   Wed May 09, 2012 9:50 am

Let me see it, and see if we can fix it up.
Back to top Go down
View user profile
woefie
Owner
Owner
avatar

Awesome
Posts : 52
Points : 97
Reputation : 0
Join date : 2012-04-24

PostSubject: Re: Simple calculator in C++   Thu May 10, 2012 6:38 am

But i been bad boy and used namespace std ;c
well... ill write ANOTHER one Very Happy
without using codeblocks :3 just in here ;o
Code:

#include <iostream>
#include <cmath>
using std::cout;
using std::cin;
using namespace std;

int main()
{

float a;
float b;
int chosen;
float sum;
int loopieloop;

while(loopieloop <= 50){
cout  << "What kind of calculation u wanna do" << endl;
cout  << "Put number one if u wanna do PLUS" << endl;
cout  << "Put TWO if u wanna do minus" << endl;
cout  << "Put THREE if u wanna do Multiplication" << endl;
cout  << "put five if u wanna do Division YEAH I GOOGLED THAT WORD JAJA" << endl;
cin >> chosen;
// ok so.... now i make a if statement right?
if(chosen==1){
cout << "Giv me a number!" << endl;
cin >> a;
cout << "Giv me a number again!" << endl;
cin >> b;
sum = a+b;
cout << "that gives you" << sum << endl; // i hope this is right
loopieloop++;
}
if(chosen==2){
cout << "Giv me a number!" << endl;
cin >> a;
cout << "Giv me a number again!" << endl;
cin >> b;
sum = a-b;
cout << "that gives you" << sum << endl; // i hope this is right
loopieloop++;
}
if(chosen==3){
cout << "Giv me a number!" << endl;
cin >> a;
cout << "Giv me a number again!" << endl;
cin >> b;
sum = a*b;
cout << "that gives you" << sum << endl; // i hope this is right
loopieloop++;
}
if(chosen==4){
cout << "Giv me a number!" << endl;
cin >> a;
cout << "Giv me a number again!" << endl;
cin >> b;
sum = a/b;
cout << "that gives you" << sum << endl; // i hope this is right
loopieloop++;
}
}
}
}
look even with a loop :3
Back to top Go down
View user profile http://skid.forum-leaks.com
Nikolai
C++ Master
C++ Master


Awesome k
Posts : 24
Points : 43
Reputation : 5
Join date : 2012-04-26

PostSubject: Re: Simple calculator in C++   Thu May 10, 2012 7:10 am

Since you all ready did.
Code:

using std::cout;
using std::cin;
the using namespace std; wasn't necessary.
And why didn't you incorporate the use of functions?
Back to top Go down
View user profile
woefie
Owner
Owner
avatar

Awesome
Posts : 52
Points : 97
Reputation : 0
Join date : 2012-04-24

PostSubject: Re: Simple calculator in C++   Thu May 10, 2012 7:28 am

becuz me stuuupid '--'?
oh laaal i shoul've used cases :p
Back to top Go down
View user profile http://skid.forum-leaks.com
Nikolai
C++ Master
C++ Master


Awesome k
Posts : 24
Points : 43
Reputation : 5
Join date : 2012-04-26

PostSubject: Re: Simple calculator in C++   Sat May 12, 2012 12:32 pm

Don't worry I'll post a calculator you can learn from.
Back to top Go down
View user profile
Rzdude
Beginner
Beginner


Posts : 78
Points : 110
Reputation : 0
Join date : 2014-06-27

PostSubject: Re: Simple calculator in C++   Fri Jun 27, 2014 5:41 am

Back to top Go down
View user profile
Sponsored content




PostSubject: Re: Simple calculator in C++   

Back to top Go down
 
Simple calculator in C++
View previous topic View next topic Back to top 
Page 1 of 1
 Similar topics
-
» Heresy Combat Calculator
» Simple Proclub custom HUD and CS:S main menu #2
» Simple-c-chess-engine
» Simple hello message.
» speed calculator in java

Permissions in this forum:You cannot reply to topics in this forum
SkidForums ::   :: Coding :: Coding :: C/C++-
Jump to: