Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Dec 7th, 2005, 4:10 PM   #1
ferret
Newbie
 
Join Date: Dec 2005
Posts: 10
Rep Power: 0 ferret is on a distinguished road
Send a message via AIM to ferret
Newbie Help

This program executes halfway and then terminates:
#include <iostream>
using namespace std;

int main()
{
int a, b;

cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";
cin >> b;

if(a < b) {
cout << "First number is less than second.\n";
cout << "Their difference is: " << b-a;

}

return 0;
}
It asks me for the first number and then the second number and when I'm done typing them in I get no console output. The program just terminates. Do you know what I'm doing wrong? I know int a has to be less then int b. It just doesnt work and it should.
__________________
eat, drink and be merry for tomorrow we die.
ferret is offline   Reply With Quote
Old Dec 7th, 2005, 4:22 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You'd like some advice, and we'd like to give it. The first piece is: Read the rules/FAQ for a new community when you visit. Use code tags around your code.

Now, to your problem. You're probably just missing the output because the program fires up, runs, presents the output, and disappears before you see it. This has been covered in the forums twice in the last week alone. Exercise some search/research skills that will stand you in good stead later.

It is also possible that you or your user are breaking the stream so that the program is just zooping straight through to the end. This too is a frequent problem with novices that have failed to cover the documentation for the functions they use.

It is also possible that the test isn't being met and that the program is operating correctly in skipping those statements.

I suggest a search of recent threads using terms like "pause", "cls", "disappear", "getchar", "get". Sure, you're bound to get some junk. On the other hand, junk is relative.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Dec 7th, 2005, 6:41 PM   #3
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Assuming you're using windows, put:
system("pause");
before you return 0.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Dec 8th, 2005, 10:22 AM   #4
zirener
Newbie
 
Join Date: Dec 2005
Location: Space, Tellus, Europe, Norway, Oslo
Posts: 23
Rep Power: 0 zirener is an unknown quantity at this point
Send a message via MSN to zirener
Or, you could do something like this, which probably is more portable:
#include <limits>
//...
cin.ignore(std::numeric_limits<int>::max(), '\n');
cin.get();
//...
zirener is offline   Reply With Quote
Old Dec 8th, 2005, 10:31 AM   #5
UnKnown X
Hobbyist Programmer
 
UnKnown X's Avatar
 
Join Date: Dec 2005
Location: Sandvika, Norway
Posts: 114
Rep Power: 0 UnKnown X is an unknown quantity at this point
Send a message via MSN to UnKnown X
Quote:
Originally Posted by zirener
Or, you could do something like this, which probably is more portable:
#include <limits>
//...
cin.ignore(std::numeric_limits<int>::max(), '\n');
cin.get();
//...
I suggest putting cin.sync() before the long cin.ignore(). That way you'll be almost sure it'll work.
UnKnown X is offline   Reply With Quote
Old Dec 8th, 2005, 10:39 AM   #6
HaCkeR
Hobbyist Programmer
 
HaCkeR's Avatar
 
Join Date: Nov 2005
Location: UK
Posts: 131
Rep Power: 0 HaCkeR is an unknown quantity at this point
Send a message via AIM to HaCkeR Send a message via MSN to HaCkeR
well for starter your IF function only covers a < b so if b < a then it runs the program correctly and ends.

secondly your program runs fine but you need include a WAIT statement or cin.get() so the the program doesnt end immediatly.
here is an example of cin.get() with the if statement complete

#include <iostream>
using namespace std;
 int main()
{
int a, b;
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";
cin >> b;
if(a < b) {
	 cout << "First number is less than second.\n";
	 cout << "Their difference is: " << b-a;
	 cin.get(); // user presses a button to end
	 }
	 
	 else {
		  cout << "First number is bigger than second.\n";
		  cout << "Thier difference is: " << a - b;
		  cin.get(); // user presses a button to end
		  }
cin.get(); // user presses a button to end
return 0;
}
HaCkeR is offline   Reply With Quote
Old Dec 8th, 2005, 11:03 AM   #7
UnKnown X
Hobbyist Programmer
 
UnKnown X's Avatar
 
Join Date: Dec 2005
Location: Sandvika, Norway
Posts: 114
Rep Power: 0 UnKnown X is an unknown quantity at this point
Send a message via MSN to UnKnown X
That indentation is horrible, IMO.

Here's what I suggest. I also added an else if for whenever a == b.

#include <iostream>
using namespace std;

int main()
{
    int a, b;
    cout << "Enter first number: ";
    cin >> a;
    cout << "Enter second number: ";
    cin >> b;
    if(a < b)
    {
        cout << "First number is less than second.\n";
        cout << "Their difference is: " << b-a << endl;
        cin.sync();
        cin.get(); // user presses a button to end
    }
    else if (a == b)
    {
        cout << "The numbers are equal.\n";
        cout << "Their difference is: 0\n";
        cin.sync();
        cin.get();
    }
    else
    {
        cout << "First number is greater than second.\n";
        cout << "Their difference is: " << a - b << endl;
        cin.sync();
        cin.get(); // user presses a button to end
    }

    cin.sync();
    cin.get(); // user presses a button to end
    return 0;
}
UnKnown X is offline   Reply With Quote
Old Dec 8th, 2005, 12:48 PM   #8
HaCkeR
Hobbyist Programmer
 
HaCkeR's Avatar
 
Join Date: Nov 2005
Location: UK
Posts: 131
Rep Power: 0 HaCkeR is an unknown quantity at this point
Send a message via AIM to HaCkeR Send a message via MSN to HaCkeR
Dunno why my indentation is like that it realy is horrible isnt it
HaCkeR is offline   Reply With Quote
Old Dec 8th, 2005, 5:47 PM   #9
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 884
Rep Power: 4 The Dark is on a distinguished road
Quote:
Originally Posted by HaCkeR
Dunno why my indentation is like that it realy is horrible isnt it
It is probably tab characters in your original source not being handled properly in the web form.

Edit: Just tried it from my editor and it worked, so it is not the web form.
The Dark 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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:13 PM.

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