Quote:
Originally Posted by sanket_203
Liverpool_fan i tried using cin.get(), the same problem occurs, stopping halfway. What should I do?
|
Oh dear! Screw me for not foreseeing a buffer problem. Since you are using cin, it is leaving a stray '\n' in the buffer and as a result the cin.get is receiving '\n' from the buffer and is not trapping the program for input.
You can keep the buffer clean by using cin.ignore() after each cin>> operation or Just before cin.get() use
cin.ignore(80, '\n'). (which will ignore 80 characters or '\n' in the buffer).
Code:
#include <iostream>
using namespace std;
class Addition
{
public:
int a, b, total;
void sum()
{
cout << "Enter integers to be added:" << endl;
cin >> a >> b;
total = a + b;
cout << "The sum is " << total << endl;
}
};
int main()
{
Addition C1;
C1.sum();
cin.ignore(80, '\n');
cin.get();
return 0;
Keep in mind the problem is not with your program but with the IDE. THe program is working all fine. Only the IDE is not freezing the Window for you to see the ouput. Oh Well...
I say dump the IDE, you a standard editor like Notepad++ or Crimson Editor and use the terminal/command prompt for compiling the programs.
http://lambdacore.wikidot.com/set-mingw
[just use g++ instead of gcc]
For IDE I will recommend you to set up Geany. IIRC Dev C++ installs MinGW already, so you need only geany.
http://lambdacore.wikidot.com/geany
Quote:
|
And why using system("PAUSE") is recommended to be avoided?
|
Well I posted the link about that in the previous post. Anyway it should be avoided because it's highly resource intensive and non-portable i.e only restricted to DOS (the pause command that is). It will not work in Linux, etc.
However if you want just to practice C++ Programming in Windows platform. Use it, but keep in mind of its deficiencies and don't depend on it.