sams teach yourself c++ in an hour a day

I am brand new to any code. Reading Sam's teach yourself C++ I an hour a day.

It only displays 51. What am I doing wrong?

#include <iostream>
using namespace std;

int main()
{
cout << "This program will help you multiply two numbers" << endl;

cout << " 51 ";
int firstNumber = 0;
cin >> firstNumber;

cout << " 24 ";
int secondNumber = 0;
cin >> secondNumber;

// Multiply two numbers, store result in a variable
int multiplicationResult = firstNumber * secondNumber;

// Display result
cout << firstNumber << " x " << secondNumber;
cout << " x " << multiplicationResult << endl;

return 0;
}
Please use code tags.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace 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
I'm still not understanding. Prompt for entry?
Run the program. Easy way is to use the little gear wheel at the top right of the code sample.

When it says "Enter first number:" you .... enter the first number!
i.e. you type a number and PRESS ENTER.

Do the same for the second one et ... voila!

I do hope you aren't using Visual Studio ...


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.
@bradrat12

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.
Topic archived. No new replies allowed.