Creating a program with steps

Posted on 2004-11-06
Last Modified: 2010-04-15


I'm trying to create  a program with a number of steps. The code has a number of IF statements.

If the 1st if statement is met then the program runs all the other steps immediatley without for the user to press enter

What do you have to insert into the code to make this happen.

For example if step one is met proceed to step two and wait for user input, if user input is met then code moves to the next line, etc...

At the moment the code is completing the if statement and the automatically doing entire program.
Question by:andyw27
    LVL 5

    Expert Comment

    if (condition1) {
            // do something

    // get user input and process it

    if (condition2){
              // do something
     ..// rest of your code

    it would be easier to suggest an approach if you
    post your code and tell exactly how would you
    want it to behave
    LVL 1

    Expert Comment

    looks like you're asking for help on the program, and also how to post your code into this question.  Given van_dy's comment above gives you general direction on how to proceed, I agree you should post your code.

    Easiest way to do this, is to just copy and past into the comment block....don't worry about formatting too much.
    LVL 7

    Expert Comment

    Further to what the others have said, if your question is specifically how to ask the user to press Enter, then use the getch() or getchar() functions.
    You can also print a message to the user using the printf() function to let him know that you are waiting for him to press Enter - a very good idea otherwise the user might think the program is just hanging.
    LVL 2

    Expert Comment

    Inside the if statements you write first statement as getch()

    Then it will wait for pressing any key.


    Accepted Solution

    Most of the time people ask for this, they are wanting something to mimic the "pause" command in DOS, where the user sees "Press any key to continue . . .". Unfortunately, there isn't a standard way to do this in either C or C++. Most input streams are buffered, meaning the program will not usually see any data until the user presses [Enter].

    The first few code snippets shown here use standard functions only, making them clean and portable. At the end, you'll find some non-standard implementations, that may or may not work for you. If you can't find something that works, go to the forums and try a search or two.

    A C implementation using getchar()

    #include <stdio.h>

    int main(void)
      int ch;
      printf ("Press [Enter] to continue");
      while ((ch = getchar()) != '\n' && ch != EOF);

    A C++ Implementation using get()

    #include <iostream>

    int main(void)
      std::cout <<"Press [Enter] to continue" <<std::endl;

    A poor-mans method of using system(), OS specific.

    #include <stdlib.h>

    int main(void)
      system ("pause");

    A compiler specific implementation using getch()

    #include <stdio.h> /* for printf() */
    #include <conio.h> /* for getch()  */

    int main(void)
       printf("Press a key to continue...");
       return 0;

    A Unix specific method using terminal modes (Credit vVv)

    This code attempts to mimics the getch() function within DOS. It sets the terminal into non-canonical mode, thus disabling line buffering, reads a character from stdin and then restores the old terminal status. For more info on what else you can do with termios, see "man termios".

    #include <stdio.h>
    #include <termios.h>
    #include <unistd.h>

    int mygetch(void)
      struct termios oldt,
      int            ch;
      tcgetattr( STDIN_FILENO, &oldt );
      newt = oldt;
      newt.c_lflag &= ~( ICANON | ECHO );
      tcsetattr( STDIN_FILENO, TCSANOW, &newt );
      ch = getchar();
      tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
      return ch;

    There's also a getch() function in the curses library, but it is not equivalent to the DOS getch() and may only be used within real curses applications (ie: it only works in curses "WINDOW"s).

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now