Thread: gender program help

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    12

    gender program help

    Hello. Noob here. Started programming yesterday. LEL
    How do I write a program that'll interpret "M" and "F" inputs and print a "you're a guy" or "you're a gurl" message?

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    
    char gender;
    char main(void)
    {
        printf("type M or F\n");
        getchar("M" || "F" , &gender);
    
    
        if (gender == "M")
        {
            printf("you're a guy\n");
        }
        
        else if (gender == "F")
        {
            printf("you're a gurl\n")
        }
    
    
        getch();
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Characters like F or M are surrounded by single quotes (') not double quotes (").

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    you have the wrong idea about the getchar() function, try here.

    you should not use global variables, your line number 5
    Code:
    char gender;
    should be within your main

    You specify the return type of your main as char and return nothing,your main should return an integer in any case

    You should have tried to compile your code before posting here.The errors i see could have been corrected

  4. #4
    Registered User
    Join Date
    Apr 2014
    Posts
    12
    Quote Originally Posted by africanwizz View Post
    You should have tried to compile your code before posting here.The errors i see could have been corrected
    Errors:
    Code:
    mingw32-gcc.exe   -c C:\c\gender.c -o C:\c\gender.oC:\c\gender.c: In function 'main':
    C:\c\gender.c:9:5: error: too many arguments to function 'getchar'
    In file included from C:\c\gender.c:1:0:
    c:\program files (x86)\codeblocks\mingw\bin\../lib/gcc/mingw32/4.7.1/../../../../include/stdio.h:386:42: note: declared here
    C:\c\gender.c:12:16: warning: comparison between pointer and integer [enabled by default]
    C:\c\gender.c:17:21: warning: comparison between pointer and integer [enabled by default]
    C:\c\gender.c:20:5: error: expected ';' before '}' token
    Process terminated with status 1 (0 minute(s), 1 second(s))
    2 error(s), 2 warning(s) (0 minute(s), 1 second(s))

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    Good,
    now the next step is to read those errors, understand them and correct them.

    example:
    C:\c\gender.c:20:5: error: expected ';' before '}' token
    -you forget to put a ';' after your printf() on line number 19 thus your compiler tells you that somethings missing
    Last edited by africanwizz; 04-11-2014 at 05:52 AM. Reason: meh..

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    505
    It looks like you've been exposed to some other language which has higher-level string handling facilities.
    C treats a string as a pointer to the first element. Then it's just an array of chars. You can't use the == operator to compare two strings for equality, it will just compare the addresses for equality. get char also doesn't work as you think, look it up in the manual and do a bit of experimental coding to get the hang of it.
    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.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Gender and Language
    By robatino in forum A Brief History of Cprogramming.com
    Replies: 91
    Last Post: 02-11-2008, 10:38 AM
  2. Do you want gender to be included in our profiles?
    By joshdick in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 12-29-2002, 12:05 AM
  3. Gender
    By joshdick in forum A Brief History of Cprogramming.com
    Replies: 55
    Last Post: 12-28-2002, 10:07 AM
  4. doubleanti's gender poll...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 111
    Last Post: 08-04-2002, 12:15 PM
  5. anything indicative of gender...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 08-14-2001, 11:56 AM

Tags for this Thread