Modifying a program to use a function

This is a discussion on Modifying a program to use a function within the C Programming forums, part of the General Programming Boards category; Hey guys and gals, I just started to use functions and I'm stuck on a question, the question is as ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    1

    Modifying a program to use a function

    Hey guys and gals, I just started to use functions and I'm stuck on a question, the question is as follows,
    Q. Based on program a (guess the word program),modify the program so that it uses a function to wait and read the user's input.

    here is the code for a, this program works fine

    Code:
    #include <string.h>
    #include <stdio.h>
    
    int main(void)
    {
        const char SECRET[]="pink";
        char guess[20];
        int x;
        
        for (x = 0; ((guess != "pink") && (x < 3)); x = x + 1)
        {
        printf("Guess the secret word\n");
        scanf("%s",guess);
        printf("%s\n Is incorrect, please try again\n");
        
        if(strcmp(SECRET,guess)==0)
        {
            printf("Correct guess - well done\n");
            break;
            
        }
        }    
    return 0;
    }

    And here is my attempt

    Code:
    #include <string.h>
    #include <stdio.h>
    
    void get_user_input(char*);
    int main(void)
    {
        const char SECRET[]="pink";
        int x;
        get_user_input(&SECRET);
        return 0;
    }
        get_user_input(char* guess)
    {    
        
        for (x = 0; ((guess != "pink") && (x < 3)); x = x + 1)
        {
        printf("Guess the secret word\n");
        scanf("%s",guess);
        printf("%s\n Is incorrect, please try again\n");
        
        if(strcmp(SECRET,guess)==0)
        {
            printf("Correct guess - well done\n");
            break;
            
        }
    }

    This doesn't work for me and I'm not really sure why, can anyone give me some pointers, thanks in advance

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,655
    Maybe you should start learning to read what the compiler says to you

    I guess it will say that x is undeclared in your function. So you have to declare it. The x in the main function is out of scope for your function.
    Also the call from the main should be without &, since SECRET is an array
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    331
    You haven't really understood what the question is getting at.

    get_user_input() should prompt the user for input, then return it. It shouldn't do anything else. The loop and the test for accuracy should still be in main().
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. K&R Exercise 1-16: Modifying a getline() function
    By edw211 in forum C Programming
    Replies: 5
    Last Post: 06-15-2011, 03:58 PM
  2. modifying bubblesort function
    By tenhavenator in forum C Programming
    Replies: 3
    Last Post: 11-13-2009, 11:54 PM
  3. Need help modifying my program
    By Northstar in forum C Programming
    Replies: 8
    Last Post: 11-11-2007, 09:25 PM
  4. help modifying a program...
    By sweetly in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2003, 03:57 PM
  5. Help with Modifying Program
    By omalleys in forum C Programming
    Replies: 0
    Last Post: 06-19-2002, 09:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21