Thread: C Programming Beginner in need of assistance!

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

    Smile C Programming Beginner in need of assistance!

    Essentially, i am trying to create a program that when i type certain applications to open, the program will open that program.

    Please see the below code. (Apologies if its messy its literally my first one).
    Code:
    #include <stdio.h>
    #include <windows.h>
    int main() {
    char input[100];
    char name[20];
    {
    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,"Open Google Chrome") == 0);
    {
         printf("Opening Google Chrome for you!\n");
        ShellExecute(NULL, "open","chrome.exe",NULL,NULL,SW_SHOWNORMAL);
    }
    //system("Start "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"); This doesnt seem to work? Use shell Execute instead.
    {
        printf("done");
    }
    return 0;
    }
    The issue is, if i type literally anything, it will still open Google Chrome. I want to have to type "Google Chrome" in order for it to open.

    Does that make sense?

    If so any help would be greatly appreciated, thanks!

  2. #2
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    147
    if i type literally anything, it will still open Google Chrome.

    Code:
    if(strcmp(input,"Open Google Chrome") == 0);
    First thing i can see... remove the semicolon from the end of this line.
    Your brackets are wacky also...
    Last edited by Structure; 3 Weeks Ago at 09:16 AM.


  3. #3
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    147

    Thumbs up

    This is more towards what it should look like.
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int main() {
      char input[100];
      char name[20];
    
      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, "Open Google Chrome") == 0) {
        printf("Opening Google Chrome for you!\n");
        ShellExecute(NULL, "open", "chrome.exe", NULL, NULL, SW_SHOWNORMAL);
      };
    
      printf("done");
      return 0;
    }
    Last edited by Structure; 3 Weeks Ago at 09:16 AM. Reason: Remove crayola


  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,424
    Also since you're using scanf() with the "%s" format specifier your variable will only contain one "word". If you want multiple words you will need something like fgets(). Also you should be using the correct optional width specifier to avoid possible buffer overflows.

  5. #5
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    147
    this...
    Code:
    printf("Hello User, please enter your name:\n", name);
    should be...
    Code:
    printf("Hello User, please enter your name:\n");


  6. #6
    Registered User
    Join Date
    Sep 2019
    Posts
    7
    Thank you,

    I dont even know why i had that there!

  7. #7
    Registered User
    Join Date
    Sep 2019
    Posts
    7
    Hello,

    Thanks for the reply,

    Whilst i agree it looks a lot better, if i paste that code and run it, it doesnt open chrome, it just returns 0.

    Thank you!

  8. #8
    Registered User
    Join Date
    Sep 2019
    Posts
    7
    Quote Originally Posted by jimblumberg View Post
    Also since you're using scanf() with the "%s" format specifier your variable will only contain one "word". If you want multiple words you will need something like fgets(). Also you should be using the correct optional width specifier to avoid possible buffer overflows.
    Hello,

    Im not sure i understand the difference just yet, but ill look in to this.

    Thank you!

  9. #9
    Registered User
    Join Date
    Sep 2019
    Posts
    1
    I'm also starting to learn to code. I'm stuck on a particular piece of code.
    print " pleathe enter a thtring : "
    user_input= gets.chomp
    user_input.include? "s"
    print " This string has an s . "
    end
    I was informed by the code editor that I was using that it was wrong. What am I doing wrong, I went over it 5 times?

  10. #10
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    147

    Thumbs up

    if i paste that code and run it, it doesnt open chrome
    Here is a working example...
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int main() {
      char input[100];
      char name[20];
      
      printf("Hello User, please enter your name:\n");
      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, "chrome") == 0 ) {
        printf("Opening Google Chrome for you!\n");
        ShellExecute(NULL, "open", "chrome.exe", NULL, NULL, SW_SHOWNORMAL);
      };
      
      printf("done");
      return 0;
    }
    Last edited by Structure; 3 Weeks Ago at 11:18 AM.


  11. #11
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    653
    Quote Originally Posted by j0801 View Post
    I'm also starting to learn to code. I'm stuck on a particular piece of code.
    print " pleathe enter a thtring : "
    user_input= gets.chomp
    user_input.include? "s"
    print " This string has an s . "
    end
    I was informed by the code editor that I was using that it was wrong. What am I doing wrong, I went over it 5 times?
    What language is that? Ruby? I think you took a wrong turn somewhere while you were looking for a programming forum.

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

    More help if possible!

    Please see below code:

    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;
    }
    I am trying to add a loop so the user can enter a program over and over and over again without having to keep repeating the 'if' statements.

    Any ideas on how i can integrate this loop in? I think it will be a while loop with a 'continue' at the bottom, right?

    Any help would be amazing!

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,410
    Something like
    Code:
    do {
        printf("What program would you like to open, %s?\n", name);
        scanf("%s", input);
    
        // your code 
     
        printf("Is there another program you would like to open? Yes/No\n");
        scanf("%s", option);
    } while ( strcmp(option,"Yes") == 0 );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,410
    Oh and don't start multiple threads with basically the same question.
    Looping my program!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner code assistance
    By akust0m in forum C Programming
    Replies: 6
    Last Post: 08-07-2012, 09:29 PM
  2. Not new to programming but need a little assistance
    By sellinhard99 in forum C++ Programming
    Replies: 7
    Last Post: 11-02-2010, 11:27 PM
  3. Replies: 8
    Last Post: 04-03-2009, 10:42 AM
  4. Assistance with C Programming.
    By mintsmike in forum C Programming
    Replies: 10
    Last Post: 03-25-2009, 11:45 PM
  5. Need Programming Assistance for Class Project
    By gbargsley in forum C++ Programming
    Replies: 2
    Last Post: 05-07-2008, 01:43 PM

Tags for this Thread