Thread: little help required

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    little help required

    I have more code to this program but I dont want to display it
    basically I create an array for a character to be in it, mainly y or n for yes or no, then i use an If statement to check it but it doesnt seem to be working, i got this method to work before, but I don't remember what I did, the worst part is it is very basic!

    Code:
    #include <stdio.h>
    int main()
    {
        char* ans[12];
        printf("Do you want to continue? [y/n]: ");
        scanf("%c",&ans);
        if( ans == "y")
        {
            printf("Continuing code here...\n");
            return 0;
        } 
        else
        {
            printf("Didn't work did it?\n");
        }
        return 0;
    }
    everything I try to input gives me the same answer "Didn't work did it?"
    I even chaned ans to just
    char ans
    or
    char ans[12];
    but with those I get Warning: Comparing integer to character or something
    so I made it a pointer.
    when I do this:
    Code:
        scanf("%c",&ans);
        printf("%c\n",ans);  //it displays my character fine
        // OR
        scanf("%c",&ans);
        printf("%s\n",ans);  // it still works
    I even changed scanf to %s instead of %c, still didn't work right
    something wrong with my if statement?
    can someone please help me with the solution and an explanation of why my program doesn't work would be appreciated highly.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Mmm, fun with pointers, and other types

    Minimally, this is what you want

    Code:
        char ans;
        printf("Do you want to continue? [y/n]: ");
        scanf("%c",&ans);
        if( ans == 'y')
    Your type was all wrong, and you want single quotes for chars, not double quotes (strings)

    #include <std_disclaimers_about_scanf.h>

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    thanks!
    I think thats what I did before but I can't remember
    Even though I have been enriched in C for 5 years now I still don't get the concept of pointers to regular etc. and it makes even less sense of why because I program in ASM on occasion.

    I guess my stubborn mind is to close minded to see it.

    Thanks anyway!
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try this for a pointer explanation
    http://pw2.netcom.com/~tjensen/ptr/cpoint.htm

  5. #5
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    hey thanks, it makes a little more sense now, but the purpose of pointers eludes me, why not just make regular integers or chars?
    is there a specific purpose to pointers....
    eh, oh well.
    I will figure it out next year
    (I finally got accepted into the computer science department)
    I wrote some term on why counting in Binary with our fingers is better than using the decimal system
    1. We can reach 1023 numbers with both hands
    2. Your left hand alone has 31 digits
    3. If you someone says high five, then you can flip him off.
    reason:
    101 = 5 in Binary
    or
    Middle up
    index down
    thumb up

    I got accepted cuz of that!? That is amazing!
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Lvalue required error
    By eklavya8 in forum C Programming
    Replies: 5
    Last Post: 01-03-2009, 04:47 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  5. is this required in a function body?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2001, 03:20 PM