Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 1st, 2008, 7:03 PM   #1
wannabe7
Newbie
 
Join Date: Dec 2007
Posts: 16
Rep Power: 0 wannabe7 is on a distinguished road
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.
__________________
Aspiring Game Designer
wannabe7 is offline   Reply With Quote
Old Apr 1st, 2008, 9:54 PM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 742
Rep Power: 3 Jimbo is on a distinguished road
Re: Computations: extra inputs?

Quote:
Originally Posted by wannabe7 View Post
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.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Apr 1st, 2008, 10:05 PM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 8 Ooble is on a distinguished road
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;
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Question on a Homework extra credit. Direth Visual Basic 2 Dec 5th, 2007 2:20 PM
linked list keep getting extra node kruptof C++ 10 Dec 22nd, 2006 10:44 PM
Error handling inputs Konnor Java 4 Mar 11th, 2006 10:43 PM
struct program skipping inputs codylee270 C 7 Dec 5th, 2005 7:02 AM
A standards question, optional inputs into Methods Arla C# 4 Apr 27th, 2005 10:38 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:24 AM.

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