Thread: If else if else if else

  1. #1
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179

    Angry If else if else if else

    Ok, I don't know why but it seems that almost everytime I try and make a program using if and/or else and execute it that it gives me an "error, must shut down" but I don't know why, this one is from the book(I just put in the #include and int main() and the scanf thing, here is the code if it will help:

    Code:
    #include <stdio.h>
    
    int age=0;
    
    int main()
    {
    	printf("To find out if you are a minor, adult, or senior citizen please enter your age: ");
    	scanf("%d", age);
    
    	if( age < 18 )
        printf("Minor");
    
    	else 
    	
    	if( age < 65 )
        printf("Adult");
    
    	else
        printf( "Senior Citizen");
    }
    thanks in advance
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    change

    scanf("%d", age);

    to

    scanf("&d", &age);

    you are undeclaredly using pointers with scanf, you need to pass the adress of variables.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    your scanf should look like this:
    scanf("%d", &age);

  4. #4
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    do you people even try to compile these things?
    Code:
    C:\WINDOWS\Desktop>gcc -c dum.c -o dum.o -Wall
    dum.c: In function `main':
    dum.c:8: warning: format argument is not a pointer (arg 2)
    dum.c:20: warning: control reaches end of non-void function
    fix both of those errors

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    45
    do you people even try to compile these things?
    Compiles fine on Microsoft's Visual C++ compiler

    Doesn't run though

  6. #6
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by gabulldog


    Compiles fine on Microsoft's Visual C++ compiler

    Doesn't run though
    well then i guess gcc > msvc++

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    45
    Smart developers > any compiler

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >do you people even try to compile these things?
    Some compilers don't warn about the 2nd arg on scanf() calls not being pointers, so the newbie wouldn't necessarily know that they'd done wrong.

    [edit]beaten.... doh! Anyways, Borland 5.5 doesn't warn either.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    Compiles fine on Microsoft's Visual C++ compiler
    But doesn't run
    That is what I am using

    I got it to work, It works with the thing being
    scanf("&d", &age);
    and with
    scanf("%d", &age);

    Which one of these is "preferable" or right by a standard, or are they both equal?
    Last edited by CAP; 08-01-2002 at 11:01 PM.
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  10. #10
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    I got it to work, It works with the thing being
    scanf("&d", &age);
    and with
    scanf("%d", &age)
    Ummm, the 2nd one is missing a semicolon. Missing semicolons = BAD

  11. #11
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by CAP


    That is what I am using

    I got it to work, It works with the thing being
    scanf("&d", &age);
    and with
    scanf("%d", &age)

    Which one of these is "preferable" or right by a standard, or are they both equal?
    no semicolon = wtf

  12. #12
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    variable length functions are very difficult to type check. the type of variable can only be determined at run time (i think). scanf, being a generic function, is a special case, so it may be treated specially by gcc.

    i have a somewhat related question...
    functions like
    Code:
    void func1 (int &x ) {
    
    ...
    }
    work in c++, but do they work in c? if they don't that would explain why scanf inputs data that way.

  13. #13
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    sorry for the typo,

    use scanf("%d", &age);
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  14. #14
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    Ya sorry for mine too, it was an accident, but all fixed now, I had it right in the program.

    I am working on learning the language, like the syntax and all but can someone please reccomend a book, website, or something that is a good guide to something like "All things C" because even if you know the language there are so many ways to do things and how to do them better, is there a resource out there on the best techniques and such??
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  15. #15
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    I should really take a class in C, but I hate doing the programs in school since they suck... I had to write five programs in my CPP class, yet neither program taught me anything new. I would suggest looking into lots of souce code. You might not understand everything... lord knows that I don't ... but it's a great way to see what the professional programmers are doing.

    Check out http://www.wrox.com for links to source code, or even sign up for some on-line programming classes at http://www.codewarrioru.com the classes were free the last time I checked into it.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed