Getting input data fram screen with a function ?

This is a discussion on Getting input data fram screen with a function ? within the C Programming forums, part of the General Programming Boards category; How can i use a function for getting input data then putting it back in the main program?(I am new ...

  1. #1
    Unregistered
    Guest

    Unhappy Getting input data fram screen with a function ?

    How can i use a function for getting input data then putting it back in the main program?(I am new with C)
    i tried but it didn't seem to be working
    could u help me with this
    Cheers
    #include <stdio.h>
    #include<string.h>
    char CheChar();
    struct code
    {
    char input[10];
    };
    main(void)
    {
    struct code c;

    CheChar();
    printf(c.input);

    }

    //******function****
    char CheChar( )
    {
    struct code co;
    bool r=true;
    do
    {
    puts("Enter Code:");
    gets(co.input);
    if (strlen(co.input)!=10)
    {
    r=false;
    printf(" ! code must be character and not more than 10 Chars! ");
    }
    }while(!r);

    return co.input;
    }

  2. #2
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72
    You have defined function CheChar to return a char. When you call CheChar from main the value returned by CheChar is discarded. You probably want to save that value in c.input.

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Tip: don't use gets (fgets is more save).
    Tip: input is 10 characters long. that's 9 chars and a null character to terminate the string.
    Tip: find a good C Tutorial site with some examples

    I'm not going to make your code work because I'm too lazy and you don't learn anything from it. Here's a small example.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void get_line(char *line);
    char *get_line2(char *line);
    
    int main( void )
    {
      char line[1024];
      get_line(line);
      printf(line);
      printf(get_line2(line));
      return 0;
    }
    
    void get_line(char *line)
    {
      strcpy(line, "Hello world!\n");
    }
    
    char *get_line2(char *line)
    {
       strcpy(line, "Hello world2!\n");
       return line;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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