![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
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. |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Assuming you're using windows, put:
system("pause");
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#4 |
|
Newbie
|
Or, you could do something like this, which probably is more portable:
#include <limits> //... cin.ignore(std::numeric_limits<int>::max(), '\n'); cin.get(); //... |
|
|
|
|
|
#5 | |
|
Hobbyist Programmer
|
Quote:
|
|
|
|
|
|
|
#6 |
|
Hobbyist Programmer
|
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;
} |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
|
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;
} |
|
|
|
|
|
#8 |
|
Hobbyist Programmer
|
Dunno why my indentation is like that it realy is horrible isnt it
|
|
|
|
|
|
#9 | |
|
Expert Programmer
Join Date: Jun 2005
Posts: 884
Rep Power: 4
![]() |
Quote:
Edit: Just tried it from my editor and it worked, so it is not the web form. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|