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

Status
Not open for further replies.

sanket_203

Right off the assembly line
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


 
Last edited:

Pragadheesh

In the zone
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

Sami Hyypiä, LFC legend
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.
 
OP
S

sanket_203

Right off the assembly line
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

Sami Hyypiä, LFC legend
^ ^ ^
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;
}
 
OP
S

sanket_203

Right off the assembly line
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

Sami Hyypiä, LFC legend
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.
 
Last edited:

adatapost

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

techfreeksaif

Right off the assembly line
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
 
Status
Not open for further replies.
Top Bottom