#include <iostream>
usingnamespace std;
int main()
{
cout << "This program will help you multiply two numbers" << endl;
cout << "Enter first number: "; // Prompt for entry, not just write a number
int firstNumber = 0;
cin >> firstNumber; // You are being asked to enter a number!
cout << "Enter second number: "; // Ditto
int secondNumber = 0;
cin >> secondNumber; //
// Multiply two numbers, store result in a variable
int multiplicationResult = firstNumber * secondNumber;
// Display result
cout << firstNumber << " x " << secondNumber;
cout << " = " << multiplicationResult << endl; // " = ", not " x "
}
This program will help you multiply two numbers
Enter first number: 1234
Enter second number: 5678
1234 x 5678 = 7006652
yes I am using visual studio. So when I type out this code from the book I don't actually enter the numbers into the code itself, I put the numbers in after I compile and run the program?
I put the numbers in after I compile and run the program?
That is correct.
The line cout << "Enter first number: ";
will write the text between quotes to your console (prompting you).
The line cin >> firstNumber;
will take any whole number that you type at the keyboard after you press enter and store it in computer memory as the variable firstNumber.
Similarly with your second number.
After your program has finished (by writing out the product of your two numbers) it will terminate. If you are using Visual Studio then it may shut your console window before you have a chance to look at the answer. (Daft IDE if you ask me.) If that happens then please read http://www.cplusplus.com/forum/beginner/1988/#msg7262
for a few(!) options.
thanks dude! thanks for being patient with me. I'm very green to programming and only started reading this book a week ago. And I don't know anybody personally that could help me. Also thanks for being very professional.
You are welcome. It sounds as if you are at that initial stage where there is a steep hurdle to get over the whole process of inputting, compiling, running code and, in the case of Visual Studio, recovering the output. If you are teaching yourself then that is already a good indicator of effort.
Your first posted code has variables, operations and input-output in, so it does something useful and, better, extensible. You can start playing with other operations on numbers for yourself - not just following the book.
Having a single reference source is not always a good idea. I would thoroughly recommend the tutorial on this site (one web page at a time) ... and also reading some of the other posts in the forum. It's an educational resource.