Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Need Help

Options
  • 20-07-2017 6:26pm
    #1
    Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,382 Mod ✭✭✭✭


    Recently got C Programming Absolute Beginner's Guide and working through the book, I copied out the program in ch 5 variables as it was written in the book and its not running for me, for some reason lunchbox has a red line underneath it and I can't figure out why, Its doing my head in.

    Any help appreciated


    [HTML]#include <stdio.h>


    int main()
    {
    char firstInitial, middleInitial;
    int number_of_pencils;
    int number_of_notebooks;
    float pencils = 0.23;
    float notebooks = 2.89;
    float lunchbox = 4.99;

    firstInitial = 'J';
    middleInitial = 'R';

    number_of_pencils = 7;
    number_of_notebooks = 4;

    printf("%c%c needs %d pencils, %d notebooks, and 1 lunchbox\n,
    firstInitial, middleInitial,number_of_pencils,
    number_of_notebooks);
    printf("The total cost is $%.2f\n\n", number_of_pencils*pencils
    + number_of_notebooks*notebooks + lunchbox");

    firstInitial = 'A';
    middleInitial = 'J';

    number_of_pencils = 10;
    number_of_notebooks = 3;

    printf("%c%c needs %d pencils, %d notebooks, and 1 lunchbox\n",
    firstInitial, middleInitial,number_of_pencils,
    number_of_notebooks);
    printf("The total cost is $%.2\n\n", number_of_pencils*pencils
    + number_of_notebooks*notebooks + lunch);

    firstInitial = 'M';
    middleInitial = 'T'


    number_of_pencils = 9;
    number_of_notebooks = 2;

    printf("%c%c needs %d pencils, %d notebooks, and 1 lunchbox\n",
    firstInitial, middleInitial,number_of_pencils,
    number_of_notebooks);

    printf("The total cost is $%.2f\n",
    number_of_pencils*pencils + number_of_notebooks*notebooks +
    lunchbox);

    return 0;

    }
    [/HTML]


Comments

  • Registered Users Posts: 109 ✭✭Boxman


    You've a " character after lunchbox which shouldn't be there.


  • Registered Users Posts: 246 ✭✭Alcoheda


    #include <stdio.h>
    
    
    int main()
    {
    char firstInitial, middleInitial;
    int number_of_pencils;
    int number_of_notebooks;
    float pencils = 0.23;
    float notebooks = 2.89;
    float lunchbox = 4.99;
    
    firstInitial = 'J';
    middleInitial = 'R';
    
    number_of_pencils = 7;
    number_of_notebooks = 4;
    
    printf("%c%c needs %d pencils, %d notebooks, and 1 lunchbox\n",
           firstInitial, middleInitial,number_of_pencils,
           number_of_notebooks);
    printf("The total cost is $%.2f\n\n", number_of_pencils*pencils
           + number_of_notebooks*notebooks + lunchbox);
    
    firstInitial = 'A';
    middleInitial = 'J';
    
    number_of_pencils = 10;
    number_of_notebooks = 3;
    
    printf("%c%c needs %d pencils, %d notebooks, and 1 lunchbox\n",
           firstInitial, middleInitial,number_of_pencils,
           number_of_notebooks);
    printf("The total cost is $%.2\n\n", number_of_pencils * pencils
           + number_of_notebooks * notebooks + lunchbox);
    
    firstInitial = 'M';
    middleInitial = 'T';
    
    
    number_of_pencils = 9;
    number_of_notebooks = 2;
    
    printf("%c%c needs %d pencils, %d notebooks, and 1 lunchbox\n",
           firstInitial, middleInitial,number_of_pencils,
           number_of_notebooks);
    
    printf("The total cost is $%.2f\n",
           number_of_pencils*pencils + number_of_notebooks*notebooks +
           lunchbox);
    
           return 0;
    
    }
    

    You messed up some of the quotation marks in your printf statements and referred to lunchbox as lunch in line 35, there is also a missing semicolon on line 38.

    Are you using an editor with syntax highlighting?
    Always read your compiler output, you can ignore the warnings but the errors will tell you what's going wrong and where.


Advertisement