hey all.

this is (if i remember correctly) my first post, i have done alot of reading on this forum though and have found the info invaluable.
my program is a calculater that calculates the amount of sheets of plasterboard and bags of plaster needed for a wall, firstly: have i gone the long way around for this particular code, i mean is there a shorter way of writing it, and can anyone point me in the right direction for creating some kind of loop that will calculate wall after wall and add the total when all of the measurements have been input. thank you in advance for any advice given. Ryan

#include<iostream>


using namespace std;

int main()

{    
    double b = 2.88; 
    double h = 2.4;
    double w;
    int p = 25;
    
    cout<< "Enter the width of the wall.\n    ";
    cin >> w;
    cout<< "\n";  
    cout<< "The amount of boards needed are\n    ";
    cout<<  h*w/b;
    cout<< "\n\n";
    cout<< "The amount of bags of plaster needed are\n    ";
    cout<<  h*w/p;
    cout<< "\n\n";
     
        
    system("PAUSE");
        
    return 0;
    }
Member Avatar for iamthwee

Wow that code is longgggggg.

Do you sense the irony?

lol yes i do funnly enough

what i mean is i am new to this and would like to know ( if it isnt to much trouble) have i written it like a weathered progrmmerer would do so, or can it be done differently. so if any one else can offer a bit of CONSTRUCTIVE critisism it would be muchly appreciated, and also can somone help with a loop of some kind. thanks to all who CAN help.

p.s @ above i think the word you are looking for is sarcasm not irony ;)

I think that your code is very good, I doubt it could be simpler.

One correction is that you shouldn't use the system function, instead use cin.get():

Here I modified your code for the loop:

#include<iostream>


using namespace std;

int main()

{    
    double b = 2.88; 
    double h = 2.4;
    double w;
    int p = 25;
    int times; //How many times the user wants to re-run the app

    cout<<"How many times do you wish to use this program?";
    cin>>times;

    for (int i = 0; i > times; i++){
    
    cout<< "Enter the width of the wall.\n    ";
    cin >> w;
    cout<< "\n";  
    cout<< "The amount of boards needed are\n    ";
    cout<<  h*w/b;
    cout<< "\n\n";
    cout<< "The amount of bags of plaster needed are\n    ";
    cout<<  h*w/p;
    cout<< "\n\n";

    }
     
        
    cin.get();
        
    return 0;
    }

I believe that should work (sorry if not).

I think I know what you're after, try having a variable to hold the totals for each value, something like this maybe:

#include<iostream>
using namespace std;

int main()
{    
    double b = 2.88; 
    double h = 2.4;
    double totalBoards = 0;
    double totalBags = 0;
    double w;
    int p = 25;
    int numOfWalls;

    cout << "How many walls would you like to include? ";
    cin >> numOfWalls;

    for(int x = 0; x < numOfWalls; x++)
    {
        cout << "Enter the width of wall " << x+1 << ": ";
        cin >> w;

        totalBoards += h*w/b;
        totalBags +=  h*w/p;
    }

    cout << "Total number of boards is: " << totalBoards << endl;
    cout << "Total number of bags is: " << totalBags << endl;
     
    cin.get();
    return 0;
}

that should work if I understood you correctly, hope that's helpful

~J

thanks very much, that is exactly what im looking for. you have both been great

I would also recommend commenting your code. In something such as this, its not needed, but as code you write starts to get bigger and bigger, comments are a must.

thanks for the tip mate, will do

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.