Society of Robots - Robot Forum

Software => Software => Topic started by: garriwilson on July 12, 2008, 06:16:04 PM

Title: Problem in Dev-C++
Post by: garriwilson on July 12, 2008, 06:16:04 PM
I started learning programming, and I downloaded a recommended Dev-C++ compiler and editor.

This is what I put in for Hello World program:

Code: [Select]
#include <stdio.h>

main()
{
      printf("Hello, World!\n");
      return 0;
}

All compiles correctly and then I press Run and it just pops up in the black cmd window for a fraction of a second and disappears. ??? Anybody have this problem how do I fix it?

Then the book has an exercise program so i thought I'd try it and see if that worked:

Code: [Select]
#include <stdio.h>

int x,y;

main()
{
      for ( x = 0; x < 10; x++, printf( "\n" ) )
          for ( y = 0; y < 10; y++ )
              printf( "X" );
             
      return 0;
}

same thing happens... pops up in a cmd window for less than  second, i cant catch if it says what it's supposed to

I couldn't find this anywhere on google

EDIT:
lol i managed to take a screenshot after like 100 tries ::)
but it's too big to attach here :(

thank you
Title: Re: Problem in Dev-C++
Post by: Cotowar on July 12, 2008, 10:08:20 PM
if that happens its because the program is running correctly. you started it, but you didnt tell it to wait for you to close it manually, so it just closes itself when its done. Look farther ahead in your book and you'll see something about a pause statement or break or something like that. I cant exactly remember the syntax of it, because i havent used C++ in a long long time, but yea, just look up break statement or pause statement.

That should solve your problem.

Cheers mate.
Title: Re: Problem in Dev-C++
Post by: izua on July 13, 2008, 07:48:34 AM
a common thing to do in windows is to invoke the pause program, by using: system("pause");
i'd recommend you switch to code::blocks. really. it has templates for both PC and microcontroller developement (so you can manage a project on both side from one software), and it adresses this problem of disappearing consoles with custom data about program results.

plus, it's so much better as an editor: code folding, zoom, code completion that works.
Title: Re: Problem in Dev-C++
Post by: garriwilson on July 13, 2008, 02:56:26 PM
Thanks, so now I know this isn't an error.

I'll look up the pause command now.
Title: Re: Problem in Dev-C++
Post by: mohamed on July 09, 2009, 03:17:40 AM
i used the Dev C++ also and about the pause comand u have to include a header file name <conio.h> before the main function and at the end of the main function you type getch(); this means  dont close the window until i type any character so the screen will be open until u press any key
Title: Re: Problem in Dev-C++
Post by: jamort on July 09, 2009, 05:12:27 AM
Code: [Select]
#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
      printf("Hello, World!\n");
      cin.get();

}

try that... note iostream is a c++ preprocessor an i think cin.get() is in that preprocessor or maybe std im not to sure though.... this works
the reason it would pop up and exit was because the number returned was o
Title: Re: Problem in Dev-C++
Post by: sonictj on July 09, 2009, 01:17:00 PM
another way is system("PAUSE") and then return 0;

Code: [Select]
#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
      printf("Hello, World!\n");

     system("PAUSE");
     return 0 ;
}

system("PAUSE"); causes the command to ask you to press any key to continue.
Title: Re: Problem in Dev-C++
Post by: Luke on July 09, 2009, 02:57:21 PM
Another possibility is to just run your program from a cmd prompt. I'm a *nix guy, but I'm guessing in Windows you can just open up a cmd window, navigate to your program's directory, and execute it.
Title: Re: Problem in Dev-C++
Post by: jamort on July 09, 2009, 06:40:04 PM
another way is system("PAUSE") and then return 0;

Code: [Select]
#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
      printf("Hello, World!\n");

     system("PAUSE");
     return 0 ;
}

system("PAUSE"); causes the command to ask you to press any key to continue.
Yeah but most people suggest not to use that because its not mobile.... I admit Ive done a lot of batch file coding in some of my files but should you do it not really BUT there are times that you dont have a choice....
wheres hes using dev c++ he may as well learn c++ too... lots of stuff that c doesnt have classes etc...