Sunday, July 12, 2009

Help needed in c language?

am a begginner in c language ...


when i finish writing a program and execute it ...the execution screen appears and then disappears immediately ....


can anyone help me how to hold on to the screenn???

Help needed in c language?
This will hold on to the screen by waiting for the user to press a key and prompting them:





#include %26lt;iostream.h%26gt;


#include %26lt;stdio.h%26gt;





main()


{


int sum;





sum = 500 + 15;


printf("The sum of 500 and 15 is %d\n", sum);


system("PAUSE");


}





Your problem is that you're running this from outside the command prompt (from windows) so the program terminates straight after your printf statement and windows shuts down the program. If you run this from the MS DOS prompt then the program does terminate but will not close the window down and you can see the printf.





So you can always use the above method if you don't want to run from DOS.
Reply:Uhmm, Im going to go out on a branch here and ask the inevitable question of all time... Did you write your program in the MS-DOS command prompt of your Windows Program. Or do you have a dedicated computer strictly for C++ only? Otherwise I would check for the correct spelling. I can see a few errors in your code as mis-spelled. You write code- It HAS to BE .. How should I put it? Exact/Precise/Flawless
Reply:Your program runs from the part called main to the end. and when it ends it quits unless you give it reason not to.


you see in c, a program is made up of functions. for example printf(), scanf() etc. these functions each have something specific they do. Now to stop your program from exiting. add a funtion that stops it from doing so. there are many fucntions. but i will give two





1. getch();


2. getchar();





now change your program to look like this





#include %26lt;stdio.h%26gt;





main()


{


int sum;





sum = 500 + 15;


printf("The sum of 500 and 15 is %d\n", sum);





getchar();


}





or change it to look like this








#include %26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


main()


{


int sum;





sum = 500 + 15;


printf("The sum of 500 and 15 is %d\n", sum);





getch();


}
Reply:It might be because of the IDE you are using to run your code. Which one do you use? Dev-C++?





There are a few ways to keep your execution command prompt -- depending on the IDE you use.





You can put cin statement at the end of your main class, just so the comman prompt won't just shut off.





Or you can use some system command to pause your program.





Or you can set a breakpoint in your program and run it in debug mode.
Reply:Use getch(); function after the last statement..... This allows you to hit any key from ur Keyboard until the screen is disappeared..... The screen waits with the output for you to enter something to get disappear...
Reply:There doesn't seem to be a problem in the code, it may be in the compiler.
Reply:Show us the code.





If all you want to do is make the screen stay in the program then add this as the last statement inside





main ()


{


.


.


.


getc();


)





Then to exit the program press the Enter key.
Reply:char c;


cin %26gt;%26gt; c;

brenda song

No comments:

Post a Comment