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



## sanket_203 (May 27, 2009)

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



```
#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)

hmm I am also interested in reply to this problem..


----------



## Pragadheesh (May 28, 2009)

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)

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)

^^ thnx for throwing light on things to avoid in C++.


----------



## sanket_203 (May 28, 2009)

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)

^ ^ ^

```
#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;
}
```


----------



## sanket_203 (May 29, 2009)

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?



Liverpool_fan said:


> ^ ^ ^
> 
> ```
> #include<iostream>
> ...


----------



## Liverpool_fan (May 29, 2009)

sanket_203 said:


> 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).


```
#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.


----------



## sanket_203 (May 30, 2009)

thanks a lot liverpool_fan and Pragadheesh...! My probs now solved..


----------



## Pragadheesh (Jun 1, 2009)

you r welcome..


----------



## adatapost (Jul 5, 2009)

use cin.get() instead of system("pause") or getch(). After all there was no problem in your program. A problem is with you.


----------



## techfreeksaif (Jul 25, 2009)

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


----------

