The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 07-14-2007, 03:58 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
C++ Use of cin and variables, tutorial for beginners.

Hey,

This is my second tutorial for C++, another simple one like the last but this time i have built it up a little bit, this time i will be using the cin and variables to produce an output that in future you may find useful in your programs.

First of all the code (add this to your .cpp file in your console project):
Code:
//Includes declared here.
#include <iostream>

//Declare namespace for standard library.
using namespace std;

//Declare name as string.
string name;
//Declare age as integer.
int age;

//int used so there can be a value returned near the end, main is the function.
int main()
{
    //Display the welcome message on the screen.
    cout << "Welcome to my very simple program" << endl;
    cout << "This simple program will store a variable" << endl;
    cout << "and then display it in a message" << endl << endl;
    
    //Ask for there name.
    cout << "What is your name?" << endl;
    //Let them input there name and store value in the name string.
    cin >> name;
    //Two endl makes a clear gap between text.
    cout << endl << endl;
    
    //Ask for there age
    cout << "What is your age?" <<endl;
    //Let them input there age and store value in the age integer.
    cin >> age;
    //Two endl makes a clear gap between text.
    cout << endl << endl;
    
    //Display there inputs in a message. 
    cout << "Your name is " << name << ", you are aged " << age << endl << endl;
    
    //Pause the console so the output can be seen.
    system("PAUSE");
    //Return value, if 0 the program has run correctly, ends function main().
    return 0;
}
I have put comments for nearly everything, if you want more detail then you will have to look at my previous tutorial for the basics which can be found Here.

First thing i need to explain is the declaring variables:
Code:
//Declare name as string.
string name;
//Declare age as integer.
int age;
Variable like the above are declared:
Code:
Type variable_name;
As you can see with this code i have declared the variable "name" as a string, strings can hold information with text and numbers in it, in this case we will use it to store the users name.

The other variable i have declared is the "age" variable, i have declared it as an integer, integers can only store numbers, if you try to put text into the variable the variable will be equal to "0".

The next thing i need to explain is:
Code:
    cin >> name;
and
    cin >> age;
At the point "cin" is used the program will pause and wait for the user to input a value, the value that they input if its going to be used, needs to be stored in a variable so to do this we put the arrows ">>" notice how its not like cout where the arrows go in the other direction, the way to remmember it is:

cout << going out to the console.
cin >> coming from the console into a variable, the arrows point wheres its going you see, either out to the console or into the variable.

So we have at one point the user input that is stored in the string name and then we have another point that the user inputs a value into the integer age.

The last thing i need to mention is displaying the variables:
Code:
cout << "Your name is " << name << ", you are aged " << age << endl << endl;
In this line we are displaying are message in the console, you will notice that if you input the name "Lee" and the age "17" the message displayed will be "Your name is Lee, you are aged 17" you can see how the variable is displayed, if you where to use the code " cout << name; " it would just display the name variable and nothing else.

Now you know how to use variables in a simple application

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to Lee For This Useful Post:
HelloWorld (07-14-2007)
  #2 (permalink)  
Old 07-14-2007, 09:17 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
One thing you should note: you should NOT be able to use the string class without including <string>. In fact, when I compile with STLport 5.1.3 (a Standard Template Library (STL) implementation that I use often in C++ to make sure my code is compliant, where gcc/Dev-C++ has a quirk now and then such as the fact that you can use the string class without including <string>).
To fix it, you should be able to simply add
#include <string>

to the list of included headers.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #3 (permalink)  
Old 07-15-2007, 04:33 AM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
I am aware of that include, i did refer people to my first tutorial where i said things maybe different as i am using Dev-C++ .

Just for people with other compilers:
Code:
//Includes declared here.
#include <iostream>
#include <string>
//Declare namespace for standard library.
using namespace std;

//Declare name as string.
string name;
//Declare age as integer.
int age;

//int used so there can be a value returned near the end, main is the function.
int main()
{
    //Display the welcome message on the screen.
    cout << "Welcome to my very simple program" << endl;
    cout << "This simple program will store a variable" << endl;
    cout << "and then display it in a message" << endl << endl;
    
    //Ask for there name.
    cout << "What is your name?" << endl;
    //Let them input there name and store value in the name string.
    cin >> name;
    //Two endl makes a clear gap between text.
    cout << endl << endl;
    
    //Ask for there age
    cout << "What is your age?" <<endl;
    //Let them input there age and store value in the age integer.
    cin >> age;
    //Two endl makes a clear gap between text.
    cout << endl << endl;
    
    //Display there inputs in a message. 
    cout << "Your name is " << name << ", you are aged " << age << endl << endl;
    
    //Pause the console so the output can be seen.
    system("PAUSE");
    //Return value, if 0 the program has run correctly, ends function main().
    return 0;
}

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 07-15-2007, 01:00 PM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 109
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
Quote:
Originally Posted by Lee View Post
I am aware of that include, i did refer people to my first tutorial where i said things maybe different as i am using Dev-C++ .

Just for people with other compilers:
Although, IMHO, if you're really writing a tutorial which you intend to be used by complete novices, then you owe it to your readers to make the extra effort to ensure that your code compiles on more than one compiler. There's nothing more frustrating to a beginner than to find a tutorial with an example that they can't compile, or has mistakes.

If you don't want the hassle of installing another compiler, then try out this website Test Drive Comeau C/C++ Online

Comeau's online compiler is great for testing out code to make sure it complies with the standard (and therefore will work on any other standard-conforming compiler)


Quote:
Code:
//Includes declared here.
#include <iostream>
#include <string>
//Declare namespace for standard library.
using namespace std;

//Declare name as string.
string name;
//Declare age as integer.
int age;
While there's nothing syntactically 'wrong' with global variables, they are one of the great evils when it comes to ugly programming habits. (That, along with the dreaded 'goto')

in this short example, there's no reason to put 'name' or 'age' outside of main(), so i would strongly suggest declaring them inside your main function. Best to develop good habits right from the outset, instead of un-learning them later.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #5 (permalink)  
Old 07-15-2007, 01:03 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Quote:
Although, IMHO, if you're really writing a tutorial which you intend to be used by complete novices, then you owe it to your readers to make the extra effort to ensure that your code compiles on more than one compiler. There's nothing more frustrating to a beginner than to find a tutorial with an example that they can't compile, or has mistakes.
I'm totally agree, I'm frustated if the tutorial that I got does not compile. How can tutorial does not compile!!! XD

Quote:
in this short example, there's no reason to put 'name' or 'age' outside of main(), so i would strongly suggest declaring them inside your main function. Best to develop good habits right from the outset, instead of un-learning them later.
But there would be a time where we're going to declare it outside of our main method right to be used in the other classes? So I think making difference between instance variable and local variable is really important

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #6 (permalink)  
Old 07-15-2007, 01:10 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Quote:
Originally Posted by Bench View Post
Although, IMHO, if you're really writing a tutorial which you intend to be used by complete novices, then you owe it to your readers to make the extra effort to ensure that your code compiles on more than one compiler. There's nothing more frustrating to a beginner than to find a tutorial with an example that they can't compile, or has mistakes.
If you was a novice you would have read my other other tutorial which i refered too if you dont understand the basics, that clearly stated the code was compiled with Dev-C++ and that in other compilers it may not work.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #7 (permalink)  
Old 07-15-2007, 01:19 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Quote:
Originally Posted by Lee View Post
If you was a novice you would have read my other other tutorial which i refered too if you dont understand the basics, that clearly stated the code was compiled with Dev-C++ and that in other compilers it may not work.
Yeah, but I think for a novice person in C++ (like me)
I won't even understand what the hell is Dev-C++ and I probably thought it's just the same compiler as usual...

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #8 (permalink)  
Old 07-15-2007, 01:21 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
That is why tutorials are posted in a thread, people can post back with a question of what something is, a simple google of "Dev-C++" would give you the answer and if you dont know how to google that to find a small description then programming is not for the person.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #9 (permalink)  
Old 07-15-2007, 01:24 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Quote:
Originally Posted by Lee View Post
That is why tutorials are posted in a thread, people can post back with a question of what something is, a simple google of "Dev-C++" would give you the answer and if you dont know how to google that to find a small description then programming is not for the person.
My bad....
Do you think it's a good idea to learn from Dev-C++ which wouldn't compile on the other version? Why do you want to use this compiler instead of others such as VS (which is my favorite for Microsoft stuff..) ???

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #10 (permalink)  
Old 07-15-2007, 01:37 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
I use this compiler as this is one that i like most, others i have used i have not liked that much and have had problems trying to work them.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 06:12 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50