You are here:

- Home
- Computing/Technology
- C/C++
- C++
- How to create a printdata function in this code?

Advertisement

I have a programming assignment due soon and cannot figure out this last part of the assignment. The program is to calculate cost of installing carpeting. It will ask the user how many rooms they have for installing carpet in, then go through a loop prompting the user to enter the width, length, and cost per sq foot of each room. After the first set of numbers are given (width, length, cost per sq foot) the program will output the total cost for that room, then the user will enter the 2nd set of dimensions if there is more than one room, program will display total cost for the inputted dimensions, repeating the cycle per how many rooms the user entered. The total price will include labor and tax.

This may seem like it should be simple for me to do but I really do not understand functions. Most of what I have so far is through the help of my professor and a little luck on my part. I'm not sure how correct the program is so far, but the only step that I am having trouble coding in is the Printdata function, which prints the total cost.

Below is the code I have so far, not sure how correct it is:

#include <iostream>

#include <cmath>

using namespace std;

const float LABOR_COST=0.35;

const float TAX_RATE=0.05;

void getdata(int&, int&, float&);

float InstalledPrice(int, int, float);

float totalprice(float);

void printdata(int, int, float);

int main()

{

int count;

int length, width;

float installation;

float costpersqfoot;

cout<<"Enter the number of rooms\n";

cin>>count;

for(int i=1;i<=count;i++)

{

getdata(length, width, costpersqfoot);

InstalledPrice(length, width, costpersqfoot);

totalprice(installation);

}

system("pause");

return 0;

}

void getdata(int& length,int& width, float& costpersqfoot)

{

cout<<"Please enter the length, width, and cost per square foot: ";

cin>>length>>width>>costpersqfoot;

}

float InstalledPrice(int length, int width, float costpersqfoot)

{

return length*width*costpersqfoot+LABOR_COST*length*width;

}

float totalprice(float installation)

{

return installation* (1+TAX_RATE);

}

I also have Pseudo code for the assignment:

//Shell for Carpet Cost program

//include libraries

//prototypes

Void getdata (int &length, int& width, float& costpersqfoot);

//read the length and width of the room and the cost per square foot

Float InstalledPrice (int length, int width, float costpersqfoot);

//calculate the installed price taking into account the labor

//cost. The labor cost is a symbolic constant

Float totalprice (float installation);

//finds the total cost using the installed price and the tax rate

//taxrate is also a symbolic constant

Void printdata (int length, int width, float price);

//prints the length, width, and total cost for each data set

Const float LABOR_COST = 0.35; // $0.35 per square foot

Const float TAX_RATE = 0.05;

Int main()

{ // declare length and width of type int

// declare installation cost and total cost of type float.

//Read count for the loop

//for loop based on the count

// call get_data

// find installation cost

// find total cost

// Write the result with the printdata procedure

//endforloop

//return from main

} // end of main

// define functions here, two void and two regular

Is what I have in the main() correct so far? Does a printdata function just need to be added to main() in order to display results? If so could you assist me in creating that function?

Thank for any help

Well, this part of main should be something like:

for(int i=1;i<=count;i++)

{

getdata(length, width, costpersqfoot);

float inst_price = InstalledPrice(length, width, costpersqfoot);

float tot_price = totalprice(inst_price);

printdata( length, width, tot_price ) ;

}

And printdata would be trivial; something like:

void printdata (int length, int width, float total_price )

{

std::cout << "length: " << length << " width: " << width << " price(total): " << total_price << '\n' ;

}

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

about 15 years or so**Education/Credentials**

post graduate engineer