// anything to the right of // (on the same line) is a comment, // and is ignored by the compiler |
// include any libraries needed, e.g. #include <iostream> #include <string> // provide the main routine, with any actions to be carried out // "inside" the main - i.e. between the { and } int main() { // most part 1 code will go here: // variable declarations, computation, input/output, etc } |
// variables must be declared inside the main routine (or function) they're used in, // like the example below // either way, you must declare variables before (above) // the point where they are first used // // For variables you specify the type, name, and optionally an initial value. int main() { // user supplied item name (as text) and weight (in kilos) string itemName; double itemWeight; // ... input and output statements will go here ... } |
// print the text Hello there! followed by a newline cout << "Hello there!" << std::endl; // print the current contents of a variable, e.g. itemName std::cout << itemName; // print text, a variable, and a newline in a single cout statement std::cout << "The name of the item is " << itemName << std::endl; |
// use cout to ask the user to enter the desired value std::cout << "Enter the weight of the item in kilograms, e.g. 5.7" << std::endl; // then use cin to read the value they enter and put it into a variable // (the variable must have been declared earlier in the main routine or function) std::cin >> itemWeight; |