Thread: Functions

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    7

    Functions

    Hello everybody!
    I need to create a function from
    Code:
    #include <stdio.h>
    main()
    {
    int i;
    char s[10];
    gets(s);
    for(i=0;s[i]!='\0';i++)
    {
    if (s[i]==' ')
    s[i]='_';
    }
    printf("%s",s);
    getch();
    return 0;
    }
    And I wrote this
    Code:
    #include <stdio.h>
    	int zm(char *s)
    {
    	int i;
    	gets(s);
    	for(i=0;s[i]!='\0';i++)
    	{
    	if (s[i]==' ')
    	s[i]='_';
    	return *s;
    }
    }
    main()
    {
    printf("%s",s);
    getch();
    return 0;
    }
    As a result, It's not right.
    Could anybody write how to do it correctly

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1) Never use gets(). Read the FAQ as to why that is.

    2) main() should be int main(void)

    3) you are never calling your function in main.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ScariuM View Post
    Hello everybody!
    I need to create a function from
    Code:
    #include <stdio.h>
    main()
    {
    int i;
    char s[10];
    gets(s);
    for(i=0;s[i]!='\0';i++)
    {
    if (s[i]==' ')
    s[i]='_';
    }
    printf("%s",s);
    getch();
    return 0;
    }
    And I wrote this
    Code:
    #include <stdio.h>
    	int zm(char *s)
    {
    	int i;
    	gets(s);
    	for(i=0;s[i]!='\0';i++)
    	{
    	if (s[i]==' ')
    	s[i]='_';
    	return *s;
    }
    }
    main()
    {
    printf("%s",s);
    getch();
    return 0;
    }
    As a result, It's not right.
    Could anybody write how to do it correctly
    Well, it might help if you actually called the function...

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    What claudiu said, plus, you need to define s. Your code shouldn't even compile as it stands.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    Hm... Lots to say here...
    It looks like you're trying to read in a string and change all the spaces to underscores. First of all, gets() is a dangerous function. Everyone around here will recommend fgets(). Strongly. But it looks like you want basically to:
    Code:
    int main(){
        //get string
        fgets(s, 10, stdin);  //nevermind the magic numbers 
        //change spaces to underscores
        somefunc(s);
        //print string
        printf("%s",s);
    
        return(0);
    }
    so you'll have to define your function as:

    Code:
    void somefunc(char *s){
       //this is where you do your string transformation.
    }
    Last edited by FrankTheKneeMan; 04-06-2011 at 11:08 AM. Reason: end tag

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    Code:
    #include <stdio.h>
    	int zm(char *s)
    {
    	int i;
    	for(i=0;s[i]!='\0';i++)
    	{
    	if (s[i]==' ')
    	s[i]='_';
    	return *s;
    }
    }
    main(void)
    {
    char s[10]
    gets(s);
    zm(s);
    printf("%s",s);
    return 0;
    }
    Now, compiler write: "Declaration syntax error in function main" and show to gets(s)

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Your indentation is all wacky. Proper indentation makes things much easier to see, things like your return *s, which should be outside the for loop. Also, look one line above the gets call. What are you missing there?

    EDIT: And claudiu already told you, it's int main(void). You need that int in front of main.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    i missed ";"

    Thanks a lot to everybody!!!!!!!
    Last edited by ScariuM; 04-06-2011 at 11:50 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ScariuM
    i missed ";"
    You missed more than that:
    • zm is not a descriptive name.
    • Your poor indentation hides a logic error with your loop, assuming that you are trying to replace every space with an underscore.
    • Why is the return type of zm int? Is the function supposed to return the number of characters replaced?
    • You should explicitly state the return type of main as int.
    • You missed the warnings not to use gets.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of functions
    By frktons in forum C Programming
    Replies: 29
    Last Post: 06-30-2010, 09:51 AM
  2. Need help with functions
    By jusfry01 in forum C Programming
    Replies: 2
    Last Post: 05-22-2010, 06:25 PM
  3. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  4. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  5. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM