Thread: new to c, need help

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    3

    Question new to c, need help

    Hi, second attempt at a program, need a litle help. The source below is just one part of what i need to do but can't get it to work.

    My Output:
    How many tickets do you need?
    10
    100 - 10 = 2013281329
    seatsleft= 2013281329


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

    void main()
    {
    int seatstotal = 100, ticketneed, seatsleft;
    {
    printf("How many tickets do you need?\n");
    scanf("%i", &ticketneed);
    printf("%i - %i = %i", seatstotal, ticketneed, seatsleft, (seatstotal - ticketneed == seatsleft));

    printf("\nseatsleft= %i", seatsleft);
    }
    }

    Thanks in advance...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > void main()
    int main
    main returns an int

    > printf("%i - %i = %i", seatstotal, ticketneed, seatsleft, (seatstotal - ticketneed == seatsleft));
    Try to simplify this
    1. There are 4 parameters, and only 3 % conversions
    2. seatsleft is not updated (it isn't even initialised) prior to printing
    3. Whilst "(seatstotal - ticketneed == seatsleft)" is valid C, it doen't do anything exciting.

    Code:
    printf("How many tickets do you need?\n");
    scanf("%i", &ticketneed);
    seatsleft = seatstotal - ticketneeded;  // Add This
    printf("%i - %i = %i", seatstotal, ticketneed, seatsleft);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    when you substract(or add,...) you have to use '='
    '==' is for comparisation like "if(a==b)"

    this should work:

    Code:
    printf("How many tickets do you need?\n"); 
    scanf("%i", &ticketneed);
    seatsleft=seatstotal-ticketneed;
    printf("%i - %i = %i", seatstotal, ticketneed, seatsleft); 
    printf("\nseatsleft= %i", seatsleft);

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi, mistakes pointed out below
    Code:
    #include<stdio.h> 
    #include<stdlib.h> 
    
    void main() 
    { 
    int seatstotal = 100, ticketneed, seatsleft; 
    {   /*dont need this  */
    printf("How many tickets do you need?\n"); 
    /* format specifier for integers in c is %d not %i   */
    scanf("%i", &ticketneed); 
    printf("%i - %i = %i", seatstotal, ticketneed, seatsleft, (seatstotal - ticketneed == seatsleft)); 
    
    printf("\nseatsleft= %i", seatsleft); 
    } 
    }
    here is how it should look
    Code:
    #include<stdio.h> 
    #include<stdlib.h> 
    
    void main() 
    { 
    int seatstotal = 100, ticketneed, seatsleft; 
    
    printf("How many tickets do you need?\n"); 
    /* format specifier for integers in c is %d not %i   */
    scanf("%d", &ticketneed);
    seatsleft = seatstotal - ticketneed; 
    printf("%d - %d= %d", seatstotal, ticketneed, seatsleft); 
    
    printf("\nseatsleft= %d", seatsleft); 
    
    }
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  5. #5
    And yet again, too late.

    I should really learn to type faster

Popular pages Recent additions subscribe to a feed