Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Computations: extra inputs? (http://www.programmingforums.org/showthread.php?t=15537)

wannabe7 Apr 1st, 2008 7:03 PM

Computations: extra inputs?
 
My program:
:

#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;
    cin.ignore();
    return 0;
}


The problem with this code is that after the program finishes everything that it needs to, it won't terminate. Instead, it forces me to input two extras times. I suspect that it's probably because of the code
:

cin >> sum >> product;
. The problem is, when/if I remove that code, the program won't execute. I've tried everything I knew but can't a way around it. Maybe something else is wrong with the program?
Thanks in advance.

Jimbo Apr 1st, 2008 9:54 PM

Re: Computations: extra inputs?
 
Quote:

Originally Posted by wannabe7 (Post 143350)
I suspect that it's probably because of the code
:

cin >> sum >> product;
. The problem is, when/if I remove that code, the program won't execute.

What do you mean by "won't execute"? Compiler error? It crashes? Wrong output? The reason for asking for input twice is because that line, well, asks for input twice.

Ooble Apr 1st, 2008 10:05 PM

Re: Computations: extra inputs?
 
:

The problem is, when/if I remove that code, the program won't execute.
Wrong. The program will execute, but if don't you run it from a console, it'll run so quickly and disappear you won't even see it. That line is to prevent it from disappearing by prompting for user input just before it terminates.

I'd recommend doing this instead though:
:

cin.get();
return 0;



All times are GMT -5. The time now is 4:07 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC