Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Need Help

  • 20-07-2017 06:26PM
    #1
    Moderators, Computer Games Moderators, Social & Fun Moderators, Paid Member Posts: 81,191 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, Registered Users 2 Posts: 111 ✭✭Boxman


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


  • Registered Users, Registered Users 2 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