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
float
s, 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.