need some help

This is a discussion on need some help within the C Programming forums, part of the General Programming Boards category; can anyone tell me what am i missing here.. cause it is not working for me.. [code] #include <stdio.h> #include ...

  1. #1
    Unregistered
    Guest

    need some help

    can anyone tell me what am i missing here.. cause it is not working for me..

    [code]

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {

    int c;
    char c1 = 1;
    int x = 729799756911412043;

    printf("Find the numeric value of Helpmebro\n");
    scanf("%d",&c);

    c = getc(stdin);

    if (c == 'x')
    {
    printf("%d Congrats, %d is correct..",c1, x);
    }
    else
    {
    printf("hmm nope that is not it\n");
    return 0;
    }
    }
    [code/]

    what i am tring to do is if you get the correct numeric value it suppose to say printf("%d Congrats, %d is correct..",c1, x);
    can anyone help me fix it..

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    8
    I think you are confused with the data types. Since you declare c as int, why do you put c=='x' ? 'x' is for character. Also for c1, it's declared as char so when you assign 1 to it, you need to put '1'.
    Also when you display, the right format command must be used. e.g. if you want to display for integer, then %d is used; for character then %c is used.

  3. #3
    Unregistered
    Guest
    sorry i am still ignorant at this.. can you please show me an example with my code.. thanx and i also see that i got the [code] wront can you tell me how to do that also so i can better my post.. thanx

  4. #4
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    1.)
    Code:
    int x = 729799756911412043;
    Is it really capable of holding such a large value?

    2.)
    Code:
    scanf("%d",&c);
    c = getc(stdin);
    Why are you reading twice from the stdin?

    Code:
    fflush(stdin);
    c = getc(stdin);
    should do.

    3.) c1 has never been initialised, and you are printing it's value. It's not an error (unless you use it somewhere), but just a point.

    4.) Place the code between tags: [ CODE] and [/CODE] and not [CODE/]

    5.) What is the error you are getting, be a little more descriptive.

    >what i am tring to do is if you get the correct numeric value ...
    Then:
    Code:
    fflush(stdin);
    scanf("%d",&c);
    alone should do.
    Last edited by shaik786; 06-20-2002 at 01:15 AM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Then:
    >fflush(stdin);
    >scanf("%d",&c);
    >alone should do.
    I sure hope not, using fflush on input streams may cause demons to fly out your nose. Since this is an unpleasant experience, you should avoid undefined behavior. To remove the trailing newline from scanf you can use any number of fixes, the most effective and general that I've found is:

    while ( getchar() != '\n' );

    But something as simple as:

    scanf("%d",&c), getchar();

    Will work as long as there is only one character left in the stream.

    As for the original problem, a better description of what the desired input/output combinations should be as well as what the program isn't doing correctly is usually more helpful.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

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