Hi
I've just picked up a copy of SAMS teach yourself C in 21 days (I dont think Ill finish it that quickly) because I wanted to get into programming. I've worked through the first chapter, but keep enountering a problem: my programs exit before I can see what they are. The first program (Hello world!) compiles fine, but just flickers up then quits.

#include <stdio.h>

int main(void)
{
    printf("hello, world\n");

    return 0;
}

The same happenes with other programs in the chapter. I found in a forum that replacing

return 0;

with

fflush(stdout);
(void)getchar();

to look like

#include <stdio.h>

int main(void)
{
    printf("hello, world\n");

    fflush(stdout);
    (void)getchar();
}

fixes this - which it did, but still exits when I press enter; which is not ideal for programs which require it. I assume there is something obvious which i am missing, but for the life of me cant see what. I have tried searching for answers but havn't turned up and clues, not that I even know what to search for.
Any help would be appreciated.

PC

If you don't want it to exit then what do you want it to do ? you can create a loop as below, but the problem with this is that there is no way to quit the program when you want it to.

int main(void)
{
   while(1)
   {
        printf("hello, world\n");

        fflush(stdout);
        getchar();
    }
}

Another method is to ask to enter 'Q' to quit

int main(void)
{
   int key;
   while(1)
   {
        printf("Enter 'Q' to quit");
        printf("hello, world\n");

        fflush(stdout);
        key = getchar();
        if(key == 'Q')
            break;
    }
}

Hi
which it did, but still exits when I press enter; which is not ideal for programs which require it.
PC

I have a concern that you are "placing the carriage in front of the horses". I would encourage to keep learning and you will learn about flow control top-down. Then you will know how to ask the program to
stop when it needs to and when to loop, etc...

Right now you are experiencing the flickering in the screen because that's what you are telling it to do. Nothing else.

The getchar() function, all that is suppost to do is to read a character from the standard input buffer.
By using it you take avantage of that, "making it to wait" to let you see what the program has input to screen.

The time will come when you will have to do more "productive things", that to display text in the screen. Then what Ancient Dragon told you would be part of the ways you'd have control over the program behavior.

How about just running the code from the command line?

How about just running the code from the command line?

Most people have no idea what the command line is, unfortunately. And doesn't it defeat the purpose of using an IDE that allows editing, compiling and running the program to pop out of it after editing and compiling just to run the program?

And doesn't it defeat the purpose of using an IDE that allows editing, compiling and running the program to pop out of it after editing and compiling just to run the program?

That's what I used to do until I discovered there was a way to pause the program before it exited. :D

Most people have no idea what the command line is, unfortunately. And doesn't it defeat the purpose of using an IDE that allows editing, compiling and running the program to pop out of it after editing and compiling just to run the program?

Quite frankly, the notion of a person trying to learn C++ and not knowing what the command line is disturbs me...I concede I've been using command line compilers since I started and thus have no experience with such IDE compilers...However after using one program to write my code and another to compile and run code I am quite proficient with alt-tab...

Thanks for the code Ancient Dragon, although it looks a bit complex for me at the moment! :eek:

Right now you are experiencing the flickering in the screen because that's what you are telling it to do. Nothing else.

I'm still getting to grips with what I am actually doing when making a program. Looks like I've been getting the wrong end of the stick! I'm glad I wasn't missing something major though; it was kinda disheartening when my first program didn't work as planned.

I ran the programs using cmd.exe and they worked great. :)

Thanks for the help everyone!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.