Thread: Couple questions... HELP MEEEEE

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    25

    Couple questions... HELP MEEEEE

    I'll start by giving the code

    Code:
    #include <stdio.h>
    
    int main()
    {
            char user[10];
            char enter[10];
            
            
    
            printf("             `.      .   .'\n");
            printf("               )     )  (\n");
            printf("               `.  .'    `.\n");
            printf("               .'   )     )\n");
            printf("              (    .`   .'\n");
            printf("                           \n");
            printf("               .-~~~`-.'~~-.\n");
            printf("             -'    `   `  ` `-\n");
            printf("            `---....,,,,,...--'\n");
            printf("              `--.........--'\n");
    
            printf("\n\n           Can you feel the pie?\n");
    
            scanf("%c %c", &user, &enter);
    
            if (user == "yes")
                    printf("You are worthy\n");
            else if (user == "no")
                    printf("You are not worthy fool\n");
            else
                    printf("What the hell...?\n");
    
            
            return 0;
    
    }
    Ok. First problem. When I run the program, everything goes fine untill it goes through the IF statements. No matter what you type, it outputs random characters. Is there a step i missed? Function i need? I tried declaring char variables yes and no, but result is the same.

    And the other problem i have, is actually just a question. All those printf statements create a pie image. Is there a way i can shorten all those steps to get the same result?

    Help will be appreciated.

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    385
    Instead of this:
    Code:
    scanf("%c %c", &user, &enter);
    Try this:
    Code:
    scanf("%s%s", user, enter);
    You don't need to pass the address of (&) with arrays. Specifying an array with it's name (user) is the address of for arrays.
    Wandering aimlessly through C.....

    http://dbrink.phpwebhosting.com

  3. #3
    tocornottoc
    Guest
    your scanf function stores the character entered, but you check the variable against "yes" and "no". try checking for just a 'y' or 'n'.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    25
    Ok, i could use just y and n, but Id prefer using yes and no.

    So what do i have to do to be able to use yes and no for input.

    And the %s doesnt work.

    Another note, i thought the problem could be the scanf picking up the ENTER key as input, which explains the ("%c %c", &user, &enter).

    So, maybe that gives you guys more insight to the problem.

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Code:
    scanf("%s%s", &user, &enter);
    
    if (strcmp(user, "yes") == 0)
            printf("You are worthy\n");
    else if (strcmp(user, "no") == 0)
            printf("You are not worthy fool\n");
    else
            printf("What the hell...?\n");
    b.t.w. scanf is no a good function to read strings from input

    and scanf doesn't read the newline characters, they stay in the input buffer

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    25
    Oh yea, forgot to mention something. When i refered to the output being random characters, i meant that when i ran a printf function to print the variable "user", thats when it came with random characters.

    It always produces the else statement (What the hell....).

    Maybe this info helps...

    Also, the strcmp function suggestion isnt working, when i compile the program, it doesn't recognize "strcmp".

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Try: #include <string.h> at the beginning of your program...

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    25
    ohhhhhhhhhhh, oh yea. Didn't think of libraries.

    That fixed the problem Monster.

    Thanks all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Couple of Questions
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 12-03-2005, 06:47 PM
  2. Couple of simple directdraw questions.
    By Deo in forum Game Programming
    Replies: 3
    Last Post: 05-25-2005, 07:55 AM
  3. A couple of questions that someone might know the answer to...
    By Finchie_88 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-15-2005, 08:26 AM
  4. Studying for Final, Couple Questions...
    By stewade in forum C Programming
    Replies: 4
    Last Post: 05-10-2004, 05:02 PM
  5. A couple of Questions
    By johnnabn in forum C++ Programming
    Replies: 4
    Last Post: 02-24-2003, 10:10 PM