Okay, i've done atleast 4 programs. The problem is, how do i combine them using switch? TBH, switch is kinda confusing...

Here's the kind of output i want:
press [1] for program1
press [2] for program2
press [3] for program3
press [4] for program4

Program Here

Do you want to continue? (Y/N)

Recommended Answers

All 3 Replies

What do you mean when you say "i've done atleast 4 programs"?

Do you mean that you have four separate programs, all compiled and linked and reay to run (and you can run each of them separately right now), and you want to write a new program that will start one of the other four depending on the user's choice?

Or do you mean you have four lumps of code, and you want to combine them into a single, new program in which the user can select which of those lumps of code is run?

What Moschops said. In the first case, you would have some code like this (not suitable for class or production):

switch (selection)
{
    case 1: system(program1); break;
    case 2: system(program2); break;
    .
    .
    .
    default: fprintf(stderr, "Invalid selection made (%d)\n", selection);
    break;
}

This should give you an idea to continue with.

Hope the following sample code would be helpful for you to get an idea about switch statement. Then you can modify the code as per your program.

#include <stdio.h>

void playgame()
{
    printf( "Play game called" );
}
void loadgame()
{
    printf( "Load game called" );
}
void playmultiplayer()
{
    printf( "Play multiplayer game called" );
}

int main()
{
    int input;

    printf( "1. Play game\n" );
    printf( "2. Load game\n" );
    printf( "3. Play multiplayer\n" );
    printf( "4. Exit\n" );
    printf( "Selection: " );
    scanf( "%d", &input );
    switch ( input ) {
        case 1:            /* Note the colon, not a semicolon */
            playgame();
            break;
        case 2:          
            loadgame();
            break;
        case 3:         
            playmultiplayer();
            break;
        case 4:        
            printf( "Thanks for playing!\n" );
            break;
        default:            
            printf( "Bad input, quitting!\n" );
            break;
    }
    getchar();

}
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.