Post by SYTRIX on Aug 27, 2007 23:30:23 GMT -5
Welcome to chapter 2 in this tutorial i will be showing you how to declare variables in your C++ programs so lets get started as normal i will post the entire source code and then break it down step by step and explain what each line dose
//include iostream file to be able to use
//cout and cin
#include <iostream>
int main()//start main function
{
//declare the 3 variables
unsigned short quantity;
float price;
float taxRate;
//Print name of program
std::cout << "Sweet Cost Calculator \n";
//let user know what to do next
std::cout << "Press enter or return to exit ";
//wait for user to press enter or return
std::cin.get();
//return 0 to indicate know problems found
return 0;
}//end of main function
This is the souce for for the program now ill will brake it down for you and explain each line
Line 1 - #include <iostream> this is the include for C++ so we are able to use cout and cin, cout means console out so cin must mean console in we always add the iostream header when we know we are going to use cout and cin
Line 2 - int main{} this is the main body of you program all your code for your programs will be put between the 2 curly brackets
Lines 3, 4, 5, - these are the variables used in this program as you see one variable is the type of unsigned short and the other 2 variables are type floats floats are used for when the out put of the program will use a decimal point value eg 1.05
Line 6 - all this is on this line is to let the user know what the program is in this case the name or the program is sweet cost calculator it will calculate how much the sweets cost
Line 7 - this line will prompt the user to press enter or return
Line 8 - std::cin.get(); we use this because with out it you would not be bale to see the result of what you have done a simple explanation of it is that it holds the console window open
Line 9 - we use return 0 because it will indicate to the compiler if any problems found if it is found to have problems then you code will not be complied to a exe file
This is what your program should look like ScreenShot
If you have any questions or need any help then please post in this thread
//include iostream file to be able to use
//cout and cin
#include <iostream>
int main()//start main function
{
//declare the 3 variables
unsigned short quantity;
float price;
float taxRate;
//Print name of program
std::cout << "Sweet Cost Calculator \n";
//let user know what to do next
std::cout << "Press enter or return to exit ";
//wait for user to press enter or return
std::cin.get();
//return 0 to indicate know problems found
return 0;
}//end of main function
This is the souce for for the program now ill will brake it down for you and explain each line
Line 1 - #include <iostream> this is the include for C++ so we are able to use cout and cin, cout means console out so cin must mean console in we always add the iostream header when we know we are going to use cout and cin
Line 2 - int main{} this is the main body of you program all your code for your programs will be put between the 2 curly brackets
Lines 3, 4, 5, - these are the variables used in this program as you see one variable is the type of unsigned short and the other 2 variables are type floats floats are used for when the out put of the program will use a decimal point value eg 1.05
Line 6 - all this is on this line is to let the user know what the program is in this case the name or the program is sweet cost calculator it will calculate how much the sweets cost
Line 7 - this line will prompt the user to press enter or return
Line 8 - std::cin.get(); we use this because with out it you would not be bale to see the result of what you have done a simple explanation of it is that it holds the console window open
Line 9 - we use return 0 because it will indicate to the compiler if any problems found if it is found to have problems then you code will not be complied to a exe file
This is what your program should look like ScreenShot
If you have any questions or need any help then please post in this thread