Thread: Looping my program!

  1. #1
    Registered User
    Join Date
    Sep 2019
    Posts
    7

    Looping my program!

    Hi guys,

    Can anyone show me how to integrate a loop in to my program?

    My program will open applications once typed however, i want to remove the need for all the 'if' statements.

    Any help would be amazing.

    Thank you,

    Code:
    #include <stdio.h>
    #include <windows.h>
    int main() {
    char input[100];
    char name[20];
    char option[10];
    {
    printf("Hello User, please enter your name:\n", name);
        scanf("%s", name);
        printf("Thank you, %s\n", name);
        printf("What program would you like to open, %s?\n", name);
        scanf("%s", input);
    }
      if (strcmp(input,"Google") == 0){
         printf("Opening Google Chrome for you!\n");
        ShellExecute(NULL, "open","chrome.exe",NULL,NULL,SW_SHOWNORMAL);
        printf("Done!\n");}
    
    
      if (strcmp(input,"Outlook") == 0){
         printf("Opening Microsoft Outlook for you!\n");
        ShellExecute(NULL, "open","Outlook.exe",NULL,NULL,SW_SHOWNORMAL);
        printf("Done\n");}
    
    
       if (strcmp(input,"Putty") == 0){
         printf("Opening Putty for you!\n");
        ShellExecute(NULL, "open","Putty.exe",NULL,NULL,SW_SHOWNORMAL);
        printf("Done\n");}
    
    
    //system("Start \"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"); This doesnt seem to work? Use shell Execute instead.
    else {
              printf("no program entered, please enter another program\n");
    
    
    }
    printf("Is there another program you would like to open? Yes/No\n");
    scanf("%s", option);
    if (strcmp(option,"Yes")== 0){
        printf("Enter another program.\n");
        scanf("%s",input);}
    
    
        if (strcmp(input,"Google") == 0){
         printf("Opening Google Chrome for you!\n");
        ShellExecute(NULL, "open","chrome.exe",NULL,NULL,SW_SHOWNORMAL);
        printf("Done!\n");}
    
    
      if (strcmp(input,"Outlook") == 0){
         printf("Opening Microsoft Outlook for you!\n");
        ShellExecute(NULL, "open","Outlook.exe",NULL,NULL,SW_SHOWNORMAL);
        printf("Done\n");}
    
    
       if (strcmp(input,"Putty") == 0){
         printf("Opening Putty for you!\n");
        ShellExecute(NULL, "open","Putty.exe",NULL,NULL,SW_SHOWNORMAL);
        printf("Done\n");}
    
    
     else {
            printf("Okay, thank you.\n");
        }
    return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You would need to associate the different strings a user can input with the program names (and anything else, if applicable). For example:
    Code:
    typedef struct {
        const char* name;
        const char* cmd;
    } UserProgram;
    
    // in main
    UserProgram programs[] = {
        { "Google", "chrome.exe" }
        { "Outlook", "Outlook.exe" }
        { "Putty", "Putty.exe" }
    };
    
    for (int i = 0; i < sizeof(programs)/sizeof(programs[0]); ++i) {
        if (strcmp(programs[i].name, option) == 0) {
            ShellExecute(NULL, "open", programs[i].cmd, NULL, NULL, SW_SHOWNORMAL);
        }
    }
    You can of course include much more info, for example any additional arguments you need to pass to the program. You could also make the strings non-constant and load all the programs from an easily formatted file, like XML for instance.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Sep 2019
    Posts
    7
    Quote Originally Posted by GReaper View Post
    You would need to associate the different strings a user can input with the program names (and anything else, if applicable). For example:
    Code:
    typedef struct {
        const char* name;
        const char* cmd;
    } UserProgram;
    
    // in main
    UserProgram programs[] = {
        { "Google", "chrome.exe" }
        { "Outlook", "Outlook.exe" }
        { "Putty", "Putty.exe" }
    };
    
    for (int i = 0; i < sizeof(programs)/sizeof(programs[0]); ++i) {
        if (strcmp(programs[i].name, option) == 0) {
            ShellExecute(NULL, "open", programs[i].cmd, NULL, NULL, SW_SHOWNORMAL);
        }
    }
    You can of course include much more info, for example any additional arguments you need to pass to the program. You could also make the strings non-constant and load all the programs from an easily formatted file, like XML for instance.
    Hey man,

    That looks amazing, thing is, im new to coding and not even sure if thats C? Havent seen it before, im trying to keep to code thats familiar/basic!

    Any other ways that you know of at all?

    Thanks

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Line #13 (declaration inside the for statement) is C99, although all compilers support it as an extension regardless. You should be using C99 anyway, all compilers support it (if yours doesn't, don't use TurboC!).

    I can't make it any more basic than that... Ok, I could use two arrays instead of the struct, but I think that would make things more complicated. You need to keep associated data together to avoid confusion. If that "sizeof" bit confuses you, it just takes advantage of the fact that the compiler knows both the size of the whole array (because it's static) and the size of individual elements of the array.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program not looping
    By abs.emailverify in forum C Programming
    Replies: 9
    Last Post: 11-09-2006, 02:51 AM
  2. Need Help looping my program
    By cchase88 in forum C++ Programming
    Replies: 6
    Last Post: 07-25-2005, 06:32 PM
  3. Program Looping Problem
    By AQWst in forum C++ Programming
    Replies: 27
    Last Post: 12-21-2004, 10:59 AM
  4. Looping Program
    By Melon in forum C Programming
    Replies: 14
    Last Post: 02-02-2003, 10:54 PM
  5. My program won't stop looping
    By davie_scotland in forum C Programming
    Replies: 7
    Last Post: 01-18-2002, 02:34 PM

Tags for this Thread