![]() |
|
|
|
| ||||||
|
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. |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| ||||
| 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;
} First thing i need to explain is the declaring variables: Code: //Declare name as string. string name; //Declare age as integer. int age; Code: Type variable_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; 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; Now you know how to use variables in a simple application ![]() |
| The Following User Says Thank You to Lee For This Useful Post: | ||
HelloWorld (07-14-2007) | ||
| |
| |||
| 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. |
| |||
| Quote:
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:
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. ![]() |
| ||||
| Quote:
Quote:
![]() |
| ||||
| Quote:
|
| ||||
| Quote:
I won't even understand what the hell is Dev-C++ and I probably thought it's just the same compiler as usual... ![]() |
| ||||
| Quote:
![]() 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..) ??? |
![]() |
| Thread Tools | |
| Display Modes | |
| |