Program not working, please help!!! :/

I'm having trouble getting my program to work. It displays the messages requesting the information in the main function. But it doesn't calculate any of my functions or display the results. Can someone please show me where I'm going wrong? Very new to programming....

#include <iostream>
#include <iomanip>
using namespace std;

void calculateCarpetSize(float roomlength, float roomwidth)
{
int carpetSize;
carpetSize = roomlength*roomwidth;
cin >> carpetSize;
}
void calculateCarpetCost(float carpetSize, double sellingPrice)
{
double carpetCost;
carpetCost = carpetSize*sellingPrice;
cin >> carpetCost;
}
void calculateLabourCost(float carpetSize)
{
double labourCost;
labourCost = carpetSize*24;
cin >> labourCost;
}
bool qualifyForDiscount(string customerNo)
{
char computeDiscount;
return (customerNo[0] == '0');
}
void computeDiscount()
{
float discount;
cout << "Please enter discount %:" << discount << endl;
cin >> discount;
}
void printCustomerStatement(string customerNo, string customerName, float carpetCost, float labourCost, float discount)
{
double subtotal, subtotal2, total, tax;
tax = 14;
cout << "CROSWELL CARPET STORE STATEMENT" << endl;
cout << "Customer name: " << customerName << endl;
cout << "Customer number: " << customerNo << endl;
cout << "Carpet price: " << carpetCost << endl;
cout << "Labour: " << labourCost << endl;
subtotal = carpetCost+labourCost;
cout << "Subtotal: " << subtotal << endl;
cout << "Less discount: " << discount << endl;
subtotal2 = subtotal-discount;
cout << "Subtotal after discount: " << subtotal2 << endl;
cout << "Plus tax: " << tax << "%" << endl;
total = subtotal2*14-0;
cout << "Total: " << total << endl;
}
int main()
{
string customerNo;
string customerName;
float roomLength;
float roomWidth;
float sellingPrice;
float carpetCost;
float labourCost;
float discount;
int carpetSize;
cout << setprecision(2) << fixed;
cout << "\nPlease enter the following information: ";
cout << "\n Customer name: ";
cin >> customerName;
cout << "\n Customer number: ";
cin >> customerNo;
cout << "\n The length of the room: ";
cin >> roomLength;
cout << "\n The width of the room: ";
cin >> roomWidth;
cout << "\n The carpet selling price: ";
cin >> sellingPrice;
calculateCarpetSize(roomLength, roomWidth);
calculateCarpetCost(carpetSize, sellingPrice);
calculateLabourCost(carpetSize);
if (qualifyForDiscount(customerNo))
computeDiscount();
else
discount = 0.0;
printCustomerStatement(customerNo, customerName, carpetCost, labourCost, discount);
return 0;
} // end main
Please use the code formatting tags provided, on the right hand side (<>). It makes reading the code a whole lot easier.
Copy-paste from another post...
1) I've seen many people do using namespace std; This is fine until you are working on a huge project which may have multiple libraries that have a function with the same name. How can the compiler tell them apart? (More on this here: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) Anyway, using namespace std is considered bad programming practice.
FIX:
1
2
using std::cout; using std::cin; using std::endl;
using std::string;


2) The reason why the calculations aren't working is because the calculations aren't saved in your main function's variables. They're saved in each of the function's variables.
1
2
3
4
5
6
void calculateCarpetSize(float roomlength, float roomwidth)
{
int carpetSize;
carpetSize = roomlength*roomwidth;
cin >> carpetSize;
}

Even though int carpetSize; has the same name as the one in your main function, it doesn't actually save it in the main function's variable. You could make the variables global but (http://www.cplusplus.com/forum/beginner/174778/). I think the best way is to have the function return the calculation. Like so...
1
2
3
4
float calculateCarpetSize(float roomlength, float roomwidth)
{
	return roomlength*roomwidth;
}

This allows you to store the value calculated by the function in a variable. In this case, you'll store it in a variable in your main function, as you intended.
 
carpetSize = calculateCarpetSize(roomLength, roomWidth);


3) I don't really understand why you used a double instead of a float...
1
2
3
4
5
6
void calculateCarpetCost(float carpetSize, double sellingPrice)
{
double carpetCost;
carpetCost = carpetSize*sellingPrice;
cin >> carpetCost;
}

Sure, a double is more precise than a float, but in this case, you really don't need a double. Besides, all your cost variables,etc. are all floats, so why make it a double?

4) For each of the calculation functions, you used cin as if you were trying to store the value in a variable. cin allows the user to input something and you don't want the user to input something in your calculation functions. You won't need to store the variable in the calculation function, since the function can return a value, as shown in the code in 2).

5) Make it a habit to initialise variables.
1
2
3
4
//initialise a string to nothing
string customerNo = "";
//initialise a float
float roomLength = 0.0f

Note the "f" at the end of the "0.0". If it was just "0.0", C++ treats that as a double. Correct me if I'm wrong...

I think that should be it. If there are any mistakes, please feel free to correct them. I'm a bit tired and still learning C++.
Hope this helps :D


edit: You forgot to include the string library.
 
#include <string> 


Last edited on
Topic archived. No new replies allowed.