• 0

Newbie C Help-command prompt


Question

Hey guys I just got my first homework assignment from my C class and in school we use the GCC compiler on unix but I don't want to logon everytime to the school's computers to do a project. I'm trying to just program on windows XP. I download Miracle C, which seems like a simple complier, and did the Hello World code. But when I get to step 8 as listed here the command prompt windows in windows XP just closes really quick and I don't get to see my output. Any solutionms to this, I'm pretty literate when it comps to computers but this is my first time programming. Thanks in advance.

Link to comment
Share on other sites

12 answers to this question

Recommended Posts

  • 0

You could run the program like normal in the command prompt and make the output save to a text file instead of displaying onscreen by using redirection, >

C:\MyApp.exe > output.txt

And you could also use system("PAUSE") to make the command prompt wait to close until you press a key

Link to comment
Share on other sites

  • 0

Simple Hello World using pause:

#include <stdio.h>

int main( )
{
    /* Display 'Hello, world.' on the screen */
    printf("Hello, world.\n");

    /* Pause command prompt until a key is pressed */
    system("PAUSE");

    return 0;
}

Oh, and they do have a windows port of GCC if you want to use that like you are at school....MinGW ;)

Link to comment
Share on other sites

  • 0

getc gives me an error while comipling. And with the pause statement, depending if I put the statement before or after the return 0, it will either close on me fast again or just display the Hello World statement like 50 times. :(

If I switch to windows 2000 would it be better and would command prompt not close immediately?

Link to comment
Share on other sites

  • 0

The return 0; should only be placed at the very end of the program at main because it tells the compiler to stop the main function there, so if you put the system("PAUSE") after that the compiler will ignore that and just end the program, closing the command prompt. And about Hello, World displaying 50 times, it should not unless you put the printf("Hello, world."); into a loop or you put more than one printf( ) in the code.

And I'm not sure about Windows 2000 since I don't use it...the command prompt should stay open even after the program is done if you open up the command prompt separately by going to Start --> Run... and typing in "cmd.exe" and hitting Return. Then from there you can compile your program through the command line instead of using Miracle C and its IDE.

Example (using gcc):

C:\>gcc HelloWorld.c -o HelloWorld
C:\>HelloWorld.exe
Hello, World.

After the program is done, the command prompt should stay on the screen showing the output with the command prompt at C:\> or whichever directory you're in on the line below.

Link to comment
Share on other sites

  • 0

Ok guys thanks so much for the help. I downloaded and installed gcc, but I'm not sure where to locate it in relation to my coding projects fodler. Let's say the folder I want to store my coding projects in C:\projects, then which folders from the MinGW folder must I transfer to that folder. (i'm assuminf not only gcc.exe, cause although it worked for this project it may not work for the next, I'm guessing I also have to transfer the libraries, but which fodlers are those).

Link to comment
Share on other sites

  • 0

U got much to learn.

1. Always use search engines to find answers.

2. getc() help page: http://www.cplusplus.com/ref/cstdio/getc.html (found by searching "getc" on google. 1st result)

At the top of that page, u see <stdio.h>. This means u must import that file in order to use that function.

This is how it works in C; u need to import the header file that describe the function(s) that you want to use.

Put #include <stdio.h> at the top of your program.

3. The 'command prompt' closes because the program closes. This is the actual application that appears in a command prompt style window. So when the application teminates, the command prompt window disappear. If you want to see your program execution, open a command prompt by executing 'cmd.exe' like drekon_v1 mentionned, and start your executable through that. U'll nee to 'cd' to the right directory first.

4. U shouldn't have to move *any* part of Mingw to be able to use it. All u should need is either to call the gcc executable using it's full path (ie c:\mingw\bin\gcc.exe HelloWorld.c -o HelloWorld) *or* add the directory in which gcc.exe is into your PATH environment variable.

Hit WinKey-Pause (this shows the System Properties dialog; u can show it by using your control panel if u prefer). Click the Advanced tab. Environment Variables button. Scroll the System Variables menulist at the bottom to find the 'Path' variable. Select it then click Edit.

At the end of the Variable value, add a semi-colon followed by the complete path to the directory that contains gcc.exe (ex: add ";c:\mingw\bin"). You don't have to use double-quotes in path values, even if the path has spaces in it. After u added the gcc directory to your path, start a new cmd.exe and try it: gcc [ENTER].

5. Point 4 is probably explained very well in Mingw documentation / howto / readme / website.

Link to comment
Share on other sites

  • 0

Thanks guys again, and I'll try to use the otehr sources more next time, just that I think this is more specific problem.

One last Q (hopefully). How can I make cmd.exe start with the directory C:\projects instead of the default one: C:\Documenets and Settings\...

Link to comment
Share on other sites

  • 0

One way could be to make a batch file that would change the directory into C:\projects then open up cmd.exe and it would work on from there.

CD C:\projects
cmd.exe

You could save that as a batch file like cmd_prompt.bat on your desktop, and everytime you double click on it, it will open up a command prompt ready to go at C:\projects.

Link to comment
Share on other sites

  • 0

Well back to youg guys for help. I'm trying to program this (you guys should knwo by teh code what I'm trying to do):

int input, x, count;
	printf("Enter a positive integer: ");
	scanf("%d", &amp;input);
	printf("The factors of %d are: \n", input);
	for (x = 1, count = 0; x &lt;= input; x++)
  {if (input % x == 0)
  printf("%d is a factor of %d\n", x, input);
  count++;
  }
	printf("# of factors is %d\n", count);

The problem is when I run this the "count" vcariable always equals "input". Why is it so, I mean I did the if statement so that the count increases only if input % x == 0. A bit confusing

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.