C++

Questions about programming languages and debugging
Post Reply
User avatar
Waar
forum buddy
forum buddy
Posts: 13
Joined: 18 Mar 2007, 17:00
17
Contact:

C++

Post by Waar »

I am currently taking computer science, but i have dled dev-C++ for at home coding.I am currently have problems with this code

Code: Select all

#include <iostream>

using namespace std;

int main()
{int num_1; //the first number read in
int num_2;  // the second number read in
int sum_answer;  // answer to the 2 numbers

//asking the user for the numbers
cout<<"what is the first number? ";
cin>>num_1;
cout<<"what is the second number? ";
cin>>num_2;

// calculations
sum_answer = num_1 + num_2;

//display
cout<<"your final answer is"<<sum_answer<<endl;
    
    
return 0;
}
I believe it works, but when i add them together it closes the prompt screen. how would i go about keep it up until i close it?

bubzuru
.net coder
.net coder
Posts: 700
Joined: 17 Apr 2007, 16:00
17
Contact:

Post by bubzuru »

this is easy you can put your program in a loop or you can just use the system ("pause"); command example:

Code: Select all


#include <iostream>

using namespace std;

int main()
{int num_1; //the first number read in
int num_2;  // the second number read in
int sum_answer;  // answer to the 2 numbers

//asking the user for the numbers
cout<<"what is the first number? ";
cin>>num_1;
cout<<"what is the second number? ";
cin>>num_2;

// calculations
sum_answer = num_1 + num_2;

//display
cout<<"your final answer is"<<sum_answer<<endl;
system ("pause");   
   
return 0;
}

User avatar
Waar
forum buddy
forum buddy
Posts: 13
Joined: 18 Mar 2007, 17:00
17
Contact:

Post by Waar »

thanks, have not needed to use that yet.

User avatar
Stavros
ΜΟΛΩΝ ΛΑΒΕ
ΜΟΛΩΝ ΛΑΒΕ
Posts: 1098
Joined: 02 Jan 2006, 17:00
18
Location: Mississippi, U.S.A.

Post by Stavros »

With Dev-C++ you have to put the "system(pause);" at the end (but before "return 0;").

If you were working with Micrsoft's Visual Studios, it automatically compiles "system(pause);". It's just one of the little differences between MVS and Dev-C++.

User avatar
toao
Newbie
Newbie
Posts: 1
Joined: 17 Oct 2007, 16:00
16

Post by toao »

ermm may i say its bad to put

system("PAUSE");
return 0;

its better to put hmmm

cin.get();
return 0;

??

or even put something like

int response;
cin>>response

the last one will make it think your entering something else and stop wont it.

hope it helps bro.

User avatar
Stavros
ΜΟΛΩΝ ΛΑΒΕ
ΜΟΛΩΝ ΛΑΒΕ
Posts: 1098
Joined: 02 Jan 2006, 17:00
18
Location: Mississippi, U.S.A.

Post by Stavros »

No, it's not. All "system("pause");" does is tell the command prompt to stop but do not exit.

That is assuming that you're using a Windows or DOS program. If you are using, say, Linux, then yes "system("PAUSE") is not the answer since the "system()" function is not native to Linux. And assuming you're using Linux, then the best option (so says this article) is cin.get().

In the end I guess it really comes down to habit and the Operating System.

Edit: Or you could do something clever like this

Code: Select all

cout << "\nDone\n";

User avatar
Nerdz
The Architect
The Architect
Posts: 1127
Joined: 15 Jun 2005, 16:00
18
Location: #db_error in: select usr.location from sucko_member where usr.id=63;
Contact:

Post by Nerdz »

Guys, toao is right. NEVER use system pause...

What system pause do:
Hold the control of your program
Give the control of the operating system
Decode the command
Execute the command
Give the control back to the program

That's why you need to use one of the toao solution... it's clean, fast and elegant.
Give a man a fish, you feed him for one day.
Learn a man to fish, you feed him for life.

bubzuru
.net coder
.net coder
Posts: 700
Joined: 17 Apr 2007, 16:00
17
Contact:

Post by bubzuru »

or you can put your program in a loop
well thats how i do it for the psp :lol:
example :

Code: Select all

#include <oslib/oslib.h>

PSP_MODULE_INFO("OSLib Sample", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);

int main() {
    
    oslInit(0);
    oslInitGfx(OSL_PF_8888, 1);
    oslInitConsole();
    oslReadKeys();
    oslStartDrawing();

    oslPrintf("Welcome to Bubzurus joystick tester\n\nPress any button to continue");
    oslSyncFrame();
    oslWaitKey();
    
    
    while (!osl_quit)
    {
        
        oslStartDrawing();
        oslReadKeys();
        oslCls();
   
         if(osl_keys->pressed.start){
         oslEndGfx();
         oslQuit();
         }
        
        
        oslPrintf_xy(10,10,"this Program will Check To See If Your joystick is Broke :D");
        oslPrintf_xy(10,30,"You Can Press Start At Any Time To Exit");
        
        oslPrintf_xy(70,100,"Your joystick's X : %d",osl_keys->analogX);
        oslPrintf_xy(70,110,"Your joystick's Y : %d",osl_keys->analogY);
        
        
        if (osl_keys->analogX == 0 && osl_keys->analogY == 0){
        oslPrintf_xy(70,140,":D Your joystick Is Fine !!");
        }else{
        oslPrintf_xy(70,140,"Sorry Your joystick Is Broke");
        }
        
        oslEndDrawing();
        oslSyncFrame();       
    }
    
    
    oslEndGfx();
    oslQuit();
    return 0;
    }

that code works just fine and its in a loop
duno if you code for the pc like that tho

User avatar
Waar
forum buddy
forum buddy
Posts: 13
Joined: 18 Mar 2007, 17:00
17
Contact:

Post by Waar »

All this input is amazing thanks everyone :D

Post Reply