DIGIT-ARC read-only archive · snapshot: July 23, 2026

Need help in C++ progrramming...please...

13 posts · originally from https://geek.digit.in/community/threads/need-help-in-c-progrramming-please.116938/

S
sanket_203 Original post #1
May 27, 2009 at 10:13 PM
I recently installed Dev C++ (ver. 4.9.9.2) on my PC to complie and run C++ programs. I am new to programming field. When ever i run C++ programs in Dev C++ the programs compiles and runs with no errors . But it runs only halfway.(i.e it doesn't execute the whole program).

For eg. Please consider the following simple C++ program


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();
return 0;   
}
When I run this program, it ask for the "Enter integers to be added."
After I enter the two nos. and then press enter, the program automatically closes and returs to Dev C++ environment. What should I do to solve this problem. This happens with all the C++ programs. C programs are running fine with Dev C++. Please Help.

I have an AMD Athlon 3200 2.2 GHz System with 512 RAM running on Windows XP service Pack 2. I am using the Zune Theme from Microsoft.

I am also attaching a JPEG screen shot of the Program.

*sanketchauhan.co.cc/cpp_program.JPG

*www.flickr.com/photos/7747269@N08/3570789586/*sanketchauhan.co.cc/cpp_program.JPG


rollcage
May 28, 2009 at 2:53 AM
hmm I am also interested in reply to this problem..
Pragadheesh
May 28, 2009 at 2:33 PM
give system("PAUSE"); before return 0;

actually cmd prompt get closed after doing the execution. so you need to PAUSE the screen to view your output. In cases like Turbo C ,VisualStudio etc we use getch() or Console.ReadKey() etc to pause the screen. :)
Liverpool_fan
May 28, 2009 at 5:11 PM
Use getchar() or std::cin.get() (recommended in C++) to trap keyboard input. Do NOT use non standard library functions like getch() or Console.ReadKey(). They are non-portable as well.
Don't use system("PAUSE") either. It's NOT portable and *www.gidnetwork.com/b-61.html

Anyway as always I recommend Geany as IDE. Look at my sig for that. It automatically traps your keyboard, you'll not need these functions.
Pragadheesh
May 28, 2009 at 5:25 PM
^^ thnx for throwing light on things to avoid in C++.
S
May 28, 2009 at 9:46 PM
thanks pragadheesh...using system pause it worked fine...

But liverpool_fan is saying to avoid it......like so which is more useful..

can u re-frame the program using cin.get() ...please...
Liverpool_fan
May 29, 2009 at 1:45 PM
^ ^ ^
Code:
#include<iostream>

/*Insert Code Here for classes, structures and unions*/

int main()
{
    /*Insert code here*/
    
    [B]std::cin.get()[/B];    /* Note that if you use namespace std, you can simply use cin.get() */
    return 0;
}
S
May 29, 2009 at 4:50 PM
Liverpool_fan i tried using cin.get(), the same problem occurs, stopping halfway. What should I do?
And why using system("PAUSE") is recommended to be avoided?

^ ^ ^
Code:
#include<iostream>

/*Insert Code Here for classes, structures and unions*/

int main()
{
    /*Insert code here*/
    
    [B]std::cin.get()[/B];    /* Note that if you use namespace std, you can simply use cin.get() */
    return 0;
}
Liverpool_fan
May 29, 2009 at 6:12 PM
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();

[B]
cin.ignore(80, '\n')[/B];
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.
*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.
*lambdacore.wikidot.com/geany


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.
S
May 30, 2009 at 10:18 PM
thanks a lot liverpool_fan and Pragadheesh...! My probs now solved..
Pragadheesh
Jun 1, 2009 at 11:03 PM
you r welcome..:)
adatapost
adatapost #12
Jul 5, 2009 at 1:26 PM
use cin.get() instead of system("pause") or getch(). After all there was no problem in your program. A problem is with you.
T
Jul 25, 2009 at 12:12 PM
use borland compiler 5.5 free to get gid of this problem send me your e-mail address and i'll send you the cmpiler