I don't understand why this program is making me input two more times (after the program is finished) before it terminates. All other programs that I do usually terminates while this code idles. For those who are confused, I have to enter extra characters and hit "enter," twice before the program terminates.

#include <iostream>
using namespace std;
int main()
{
    //I'm declaring some variables.
    int number1, number2, product, sum;
    cout << "Please enter an integar (whole number): \n";
    cin >> number1;
    cout << "Please enter a another integar: \n";
    cin >> number2;
    //Computing the numbers.
    product = number1 * number2;
    sum = number1 + number2;   
    //This will reveal the final computations.
    cout << "The sum of " << number1 << " and " << number2 << " equals: " << sum << "\n";
    cout << "The product of " << number1 << " and " << number2 << " equals: " << product << "\n";
    cin >> sum >> product;
    return 0;
}

Also, I don't understand this program:

#include <iostream>
using namespace std;
int main()
{
    char text[10];
    cout << "Please enter a word: \n";
    cin.getline(text, 10);
    cout << text << endl;
    return 0;
}

I can't seem to figure out what cin.getline() means. The book that I'm learning from explains it very vague. And it doesn't do anything special. Should it? When I run the program, it terminates after I input something.

In a typical program when all calculations are done and no further input is expected the program will terminate and the console screen will disappear. That's what happens in program #2. To have the program pause so you can see what's sent to the screen you can place a cin.get(); just before return 0; or you can do a cin >> x; where x is a valid variable. The latter is what's happening in program 2. You actually have 2 calls to >> just before the return 0; line so you need to enter two pieces of information before the program will close.

I'll probably gain you some corrections to my post that will give you insight, but as a new guy I can tell you a couple things.

I'm not sure why you chose int for main, you could use void as you are only defining and outputting character strings

the first line,

char text [10]

is defining a character array with 10 elements. In other words, a limit of 10 characters.

the next line outputs your plea for data to the screen.

The getline is used to enter a string of (usually) text. In yours, you're telling it to put that string into the text array...be advised you need to leave a space for the "null terminator" or you'll become very familiar with a problem deemed the "off by one" error

The next line outputs the text to the screen

The return line is what is causing your program to exit upon entry. A return code of 0 means the program did what it was supposed to do and everything worked right so it terminated. You should be able to take that out once you replace

int main ()

with

void main ()

Others please post more details as you wish

use system("pause")
or use getch(); before return so it will wait for carriage return and you can see the output .

In your program# 1,after computing sum and product and displaying them to th user , what's the use of

cin >> sum >> product;

This is again demanding that you enter two more values for sum and product.

Also , in your program # 2 , cin.getline is used to input a string as follows:

cin.getline(<destination string pointer>,<lenght of string to be entered>);

Also ,as henpecked1 advised you , you need to have one location extra in the string for the null ('\0') character that serves as end of string.

The see the output of a program you could include
#include<conio.h>

and use the function getch() just before return 0;
This will make the run time environment wait for the user to enter a character after displaying the results.
Using getche() will also display the character you entered

first off, thanks for all the replies.

On the first program, the reason I added the cin >> sum >> product; was because, before I included that, the program would not calculate the numbers that I inputed. I followed the same code that the book had me do, but for some reason it didn't work. So, I worked with the things I knew and came up with adding the cin >> sum >> product; (along with some other adjustments). After this, the program now did the calculations but it also required me to input 2 more times. I'm reading the replies, but it seems like there isn't any clear thing to do...

The second program is confusing because when I enter in a word, the program just terminates. Is that the effect of adding cin.getline()? Because it doesn't seem like anything out of the ordinary.

Thanks for all the help so far.

I'll probably gain you some corrections to my post that will give you insight, but as a new guy I can tell you a couple things.

I'm not sure why you chose int for main, you could use void as you are only defining and outputting character strings

Because the int main() is a standard. Main function must return a value.

>>Is that the effect of adding cin.getline()?

No. getline() has nothing to do with it. The console doesn't know you want to look at the output sent to the screen so as soon as it outputs the text variable to the screen it returns 0 to the operating system which causes the console screen to either go blank or disappear completely. If you want to view text after it's been output to the screen you have to tell the program to not do anything until you tell it to. You do that by having it wait indefinetly until it receives some input from a call to one of the istream methods (>>, get(), getch(), getche(), whatever----though some methods are preferred over others) or by using system("Pause"), which isn't a good choice; or by having the program do nothing useful, ie have it count to itself or sleep or whatever you want ot call it, for a given period of time until it moves on by calling the sleep() function (for example, if you want to have the output screen remain open for 10 seconds before it closes, but not stay open indefinetly waiting for input or until another system function is called).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.