Bytes Tech Community, We heart tech!

Register Now


 
Reply to this topicStart new topicStart Poll

Outline · [ Standard ] · Linear+

 Simple Console Calculator, C++

Weebs
post Jun 5 2005, 05:04 AM
Post #1


Advanced Member
Group Icon

Group: Members
Posts: 75
------------------
Donate CFX Creds
CFX Credits: 75
------------------
Member No.: 31

Reputation: 1 pts




Ok first I'm going to teach you how to create a console based calculator :blink:

First your going to need Dev-C++ which you can get here

Now that you've gotten that installed you will now be able to create your handy-dandy-programs.

Now click File > New > Project. Then select Console Application and name it "Calculator" and click ok.

Now when you start off you will see this

CODE
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   system("PAUSE");
   return EXIT_SUCCESS;
}

Right now that looks like probably complete jibberish and it pretty much is, but I'm going to explain part of it.

#include <cstdlib> and #include <iostream> are "includes" which are used to include files in the program so you can use commands, and you can create your own includes to create your includes.

I myself have absoloutly no idea what using namespace std; does. :P

int main(int argc, char * argv[]) tells the compiler that it is the main part of the program, but I don't know what the int argc stuff is... Arent I such a great teacher? Also, the { basicly tells thats were the int main starts, and the } tells were it ends.

system("PAUSE"); and return EXIT_SUCCESS; tell the program to pause after everything is done and displays "Press any key to continue...".

So now you probably don't get a word of anything I'm saying because I am a horrid teacher, but if you did get any of that then YAYYY! Ok, now this is a pretty stupid program because it does nothing right? Well by the end of this tutorial we will have made a calculator!

Ok the first command i must show you is cout. It is used to display messeges in the console.

So now change your program to this

CODE
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
cout<<"Welcome to my uber l33t calculator";
   system("PAUSE");
   return EXIT_SUCCESS;
}

You may have noticed that all of the lines in the main part of the program have a ; after everything, this is so because it tells the compiler that a new line is being started. So now your program will display "Welcome to my uber l33t calculator" Also, you can create a new line using "\n" You can try it out and watch it for yourself. I also highly reccomend that you manually type the commands in rather than copy + paste because it helps you remember the commands. Also don't forget the ; your program will get errors if you forget it!

The next thing I'm going to show you is how to declar a integer. A integer is a variable that holds the value of a number.

Now change your program to this

CODE
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   int num;
   cout<<"Welcome to my uber l33t calculator";
   system("PAUSE");
   return EXIT_SUCCESS;
}

Now we have declared num, but it doesn't equal anything so now we must give it a value. To do this we do the following

CODE
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   int num;
   num = 7;
   cout<<"Welcome to my uber l33t calculator";
   system("PAUSE");
   return EXIT_SUCCESS;
}


Now num equals 7 :), now I will show you how to display it. When you display variables with the cout command you do not need quotes. So type the following.

CODE
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   int num;
   num = 7;
   cout<<"Welcome to my uber l33t calculator\n";
   cout<<num;
   cout<<"\n";
   system("PAUSE");
   return EXIT_SUCCESS;
}


This will now display num and the \n will make some spaces so it looks less confusing :)

They're is also anather way to give num a value, but asking the user for an input. We do this by using the cin command. So change your program to this.

CODE
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   int num;
   cout<<"Welcome to my uber l33t calculator\n";
   cout<<"Enter a number\n";
   cin>>num;
   cout<<"You entered:\n";
   cout<<num;
   cout<<"\n";
   system("PAUSE");
   return EXIT_SUCCESS;
}

This will allow the user to enter a number and then be displayed. Notice that in cin you use >> unlike in cout you use << .

Now I will teach you how to use the mathmatical operator addition. Now change your program to this.

CODE
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   int num;
   int numtwo;
   cout<<"Welcome to my uber l33t calculator\n";
   cout<<"Enter a number\n";
   cin>>num;
   cout<<"You entered:\n";
   cout<<num;
   cout<<"\n";
   numtwo = num + 7;
   cout<<num;
   cout<<" + ";
   cout<<" 7 ";
   cout<<" = ";
   cout<<numtwo;
   system("PAUSE");
   return EXIT_SUCCESS;
}


And now you auctually know anough to make a addition calculator. In this tutorial I will not show you how to do this because I think the best way for you to learn is just to experiment :D

This post has been edited by Weebs: Jun 14 2005, 03:07 AM


User is offlineProfile CardPM
+Quote PostGo to the top of the page
CryptWizard
post Jun 5 2005, 10:03 AM
Post #2


Loyal Member
Group Icon

Group: Members
Posts: 232
------------------
Donate CFX Creds
CFX Credits: 232
------------------
Member No.: 10

Reputation: 3 pts




OK.

I'll explain somthing about namespaces in C++.

It's basically a "space of names". LOL

If you didn't have "using namespace std;" you would have to type something like std::cout<<"someshit";


Now:
int main(int argc, char * argv[])
int argc is the ARGument Count
char* argv[]/char** argv/char argv[][] is the ARGument Vector.
argc is self explanatory
argv is an array of strings provided in the command line.

eg. If it was invoked with "someprog a b c"

argv[0] would be "someprog"
argv[1] would be "a"
argv[2] would be "b"
argv[3] would be "c"

Enjoy.


User is offlineProfile CardPM
+Quote PostGo to the top of the page
Gareth
post Jun 5 2005, 11:22 AM
Post #3


Mr. Bling Bling :P
Group Icon

Group: Members
Posts: 7,555
Gender: Male
------------------
Donate CFX Creds
CFX Credits: 10,736
------------------
Member No.: 1

Reputation: 184 pts




Very nice weebs, great work ;)


User is offlineProfile CardPM
+Quote PostGo to the top of the page
WinSrev
post Jun 5 2005, 12:06 PM
Post #4


CodedFx Legend!
Group Icon

Group: Coders
Posts: 1,420
Gender: Male
------------------
Donate CFX Creds
CFX Credits: 9,260
------------------
Member No.: 2

Reputation: 31 pts




Yeh, Biggest coding tutorial so far and first of C++ i think. :D


User is offlineProfile CardPM
+Quote PostGo to the top of the page
Weebs
post Jun 5 2005, 02:26 PM
Post #5


Advanced Member
Group Icon

Group: Members
Posts: 75
------------------
Donate CFX Creds
CFX Credits: 75
------------------
Member No.: 31

Reputation: 1 pts




Thanks :) I just started C++, so I really don't know much beond integers and strings :(


User is offlineProfile CardPM
+Quote PostGo to the top of the page
WinSrev
post Jun 5 2005, 02:30 PM
Post #6


CodedFx Legend!
Group Icon

Group: Coders
Posts: 1,420
Gender: Male
------------------
Donate CFX Creds
CFX Credits: 9,260
------------------
Member No.: 2

Reputation: 31 pts




Keep looking at thoose tutorials, soon you'll know the whole language. :D


User is offlineProfile CardPM
+Quote PostGo to the top of the page
CryptWizard
post Jun 5 2005, 10:30 PM
Post #7


Loyal Member
Group Icon

Group: Members
Posts: 232
------------------
Donate CFX Creds
CFX Credits: 232
------------------
Member No.: 10

Reputation: 3 pts




Google Teach yourself C++ in 21 days.
The first result


User is offlineProfile CardPM
+Quote PostGo to the top of the page
Pixelated
post Jun 7 2005, 02:32 PM
Post #8


Member
Group Icon

Group: Members
Posts: 39
------------------
Donate CFX Creds
CFX Credits: 59
------------------
Member No.: 5

Reputation: none




didnt turn out right for me,but then again i have no knowlege of c++ :P im gonna learn it now though :)


User is offlineProfile CardPM
+Quote PostGo to the top of the page
Weebs
post Jun 9 2005, 04:06 PM
Post #9


Advanced Member
Group Icon

Group: Members
Posts: 75
------------------
Donate CFX Creds
CFX Credits: 75
------------------
Member No.: 31

Reputation: 1 pts




I also need to edit some of the code in the turoial, I messed up somewere in the coding, but I can't figure out what is improper in the code...


User is offlineProfile CardPM
+Quote PostGo to the top of the page
Gareth
post Jun 9 2005, 05:47 PM
Post #10


Mr. Bling Bling :P
Group Icon

Group: Members
Posts: 7,555
Gender: Male
------------------
Donate CFX Creds
CFX Credits: 10,736
------------------
Member No.: 1

Reputation: 184 pts




QUOTE(Weebs @ Jun 9 2005, 04:06 PM)
I also need to edit some of the code in the turoial, I messed up somewere in the coding, but I can't figure out what is improper in the code...
*



CryptWizard will help you, he will kno what the problem is for sure, if you pm him im fairly sure he'll help you :)


User is offlineProfile CardPM
+Quote PostGo to the top of the page
Mc2
post Jun 12 2005, 10:53 AM
Post #11


Member
Group Icon

Group: Members
Posts: 36
------------------
Donate CFX Creds
CFX Credits: 40
------------------
Member No.: 35

Reputation: none




Found out, you need to compile it? when i try to, it comes out as a error.

Compiler: Default compiler
Building Makefile: "C:\Documents and Settings\James\Desktop\Makefile.win"
Executing make...
make.exe -f "C:\Documents and Settings\James\Desktop\Makefile.win" all
make.exe: *** No rule to make target `main.exe', needed by `main.o'. Stop.

Execution terminated

QUOTE(Weebs @ Jun 9 2005, 04:06 PM)
I also need to edit some of the code in the turoial, I messed up somewere in the coding, but I can't figure out what is improper in the code...
*



I think its
CODE
  int numtwo = num + 7;
cuz it wont let me compile it cuz of that.

This post has been edited by Brooksie: Jun 12 2005, 11:03 AM


User is offlineProfile CardPM
+Quote PostGo to the top of the page
Weebs
post Jun 13 2005, 03:35 AM
Post #12


Advanced Member
Group Icon

Group: Members
Posts: 75
------------------
Donate CFX Creds
CFX Credits: 75
------------------
Member No.: 31

Reputation: 1 pts




Hey, your right it is lol :P

change int numtwo = num + 7; into

numtwo = num + 7;


User is offlineProfile CardPM
+Quote PostGo to the top of the page
Mc2
post Jun 13 2005, 06:52 AM
Post #13


Member
Group Icon

Group: Members
Posts: 36
------------------
Donate CFX Creds
CFX Credits: 40
------------------
Member No.: 35

Reputation: none




Yay it works now!


User is offlineProfile CardPM
+Quote PostGo to the top of the page
syntax-error
post Jun 20 2005, 08:36 PM
Post #14


Dedicated Member
Group Icon

Group: Members
Posts: 515
------------------
Donate CFX Creds
CFX Credits: 515
------------------
Member No.: 116

Reputation: 9 pts




QUOTE
I myself have absoloutly no idea what using namespace std; does.


It imports the std library namespace...


User is offlineProfile CardPM
+Quote PostGo to the top of the page

Reply to this topicTopic OptionsStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members: