Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 27-05-2009, 10:13 PM   #1 (permalink)
Right Off the Assembly Line
 
Join Date: Apr 2009
Posts: 6
Default Need help in C++ progrramming...please...


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.

http://sanketchauhan.co.cc/cpp_program.JPG





Last edited by sanket_203; 27-05-2009 at 10:24 PM.
sanket_203 is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 28-05-2009, 02:53 AM   #2 (permalink)
AMD user for 9 yrs!!
 
rollcage's Avatar
 
Join Date: Jan 2005
Location: New Delhi
Posts: 944
Default Re: Need help in C++ progrramming...please...

hmm I am also interested in reply to this problem..
rollcage is offline  
Old 28-05-2009, 02:33 PM   #3 (permalink)
In The Zone
 
Pragadheesh's Avatar
 
Join Date: Jul 2006
Location: Coimbatore
Posts: 403
Default Re: Need help in C++ progrramming...please...

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.
Pragadheesh is offline  
Old 28-05-2009, 05:11 PM   #4 (permalink)
Sami Hyypiä, LFC legend
 
Liverpool_fan's Avatar
 
Join Date: Jun 2007
Location: Нью-Дели
Posts: 2,138
Default Re: Need help in C++ progrramming...please...

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 http://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.
__________________
Experience true education in Computer Science - http://www.udacity.com | http://www.coursera.org

Spoiler:
Read before asking / messaging any moderator for any query: FAQ + answers for new members

Read all the sticky threads before asking any type of query. Most basic questions are answered in those.
Don't use forum for chatting. Visit http://webchat.freenode.net/?channels=krow, enter nick and connect.
Liverpool_fan is offline  
Old 28-05-2009, 05:25 PM   #5 (permalink)
In The Zone
 
Pragadheesh's Avatar
 
Join Date: Jul 2006
Location: Coimbatore
Posts: 403
Default Re: Need help in C++ progrramming...please...

^^ thnx for throwing light on things to avoid in C++.
Pragadheesh is offline  
Old 28-05-2009, 09:46 PM   #6 (permalink)
Right Off the Assembly Line
 
Join Date: Apr 2009
Posts: 6
Default Re: Need help in C++ progrramming...please...

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...
sanket_203 is offline  
Old 29-05-2009, 01:45 PM   #7 (permalink)
Sami Hyypiä, LFC legend
 
Liverpool_fan's Avatar
 
Join Date: Jun 2007
Location: Нью-Дели
Posts: 2,138
Default Re: Need help in C++ progrramming...please...

^ ^ ^
Code:
#include<iostream>

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

int main()
{
    /*Insert code here*/
    
    std::cin.get();    /* Note that if you use namespace std, you can simply use cin.get() */
    return 0;
}
__________________
Experience true education in Computer Science - http://www.udacity.com | http://www.coursera.org

Spoiler:
Read before asking / messaging any moderator for any query: FAQ + answers for new members

Read all the sticky threads before asking any type of query. Most basic questions are answered in those.
Don't use forum for chatting. Visit http://webchat.freenode.net/?channels=krow, enter nick and connect.
Liverpool_fan is offline  
Old 29-05-2009, 04:50 PM   #8 (permalink)
Right Off the Assembly Line
 
Join Date: Apr 2009
Posts: 6
Default Re: Need help in C++ progrramming...please...

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?

Quote:
Originally Posted by Liverpool_fan View Post
^ ^ ^
Code:
#include<iostream>

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

int main()
{
    /*Insert code here*/
    
    std::cin.get();    /* Note that if you use namespace std, you can simply use cin.get() */
    return 0;
}
sanket_203 is offline  
Old 29-05-2009, 06:12 PM   #9 (permalink)
Sami Hyypiä, LFC legend
 
Liverpool_fan's Avatar
 
Join Date: Jun 2007
Location: Нью-Дели
Posts: 2,138
Default Re: Need help in C++ progrramming...please...

Quote:
Originally Posted by sanket_203 View Post
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.
__________________
Experience true education in Computer Science - http://www.udacity.com | http://www.coursera.org

Spoiler:
Read before asking / messaging any moderator for any query: FAQ + answers for new members

Read all the sticky threads before asking any type of query. Most basic questions are answered in those.
Don't use forum for chatting. Visit http://webchat.freenode.net/?channels=krow, enter nick and connect.

Last edited by Liverpool_fan; 29-05-2009 at 06:42 PM.
Liverpool_fan is offline  
Old 30-05-2009, 10:18 PM   #10 (permalink)
Right Off the Assembly Line
 
Join Date: Apr 2009
Posts: 6
Default Re: Need help in C++ progrramming...please...

thanks a lot liverpool_fan and Pragadheesh...! My probs now solved..
sanket_203 is offline  
Old 01-06-2009, 11:03 PM   #11 (permalink)
In The Zone
 
Pragadheesh's Avatar
 
Join Date: Jul 2006
Location: Coimbatore
Posts: 403
Default Re: Need help in C++ progrramming...please...

you r welcome..
Pragadheesh is offline  
Old 05-07-2009, 01:26 PM   #12 (permalink)
Right Off the Assembly Line
 
adatapost's Avatar
 
Join Date: Jul 2009
Location: India/Gujarat/Mehsana
Posts: 14
Default Re: Need help in C++ progrramming...please...

use cin.get() instead of system("pause") or getch(). After all there was no problem in your program. A problem is with you.
adatapost is offline  
Old 25-07-2009, 12:12 PM   #13 (permalink)
Right Off the Assembly Line
 
Join Date: Jun 2009
Posts: 1
Default Re: Need help in C++ progrramming...please...

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
techfreeksaif is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Sujeet
- by gforz
- by soumya

Advertisement




All times are GMT +5.5. The time now is 03:13 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2