calculate total lobster
Dec 17, 2012 at 4:43pm UTC
why my coding did not show correct answer? =="
1 2 3 4 5 6 7 8 9 10 11 12 13
This program will calculate the total lobster caught for numbers of fisherman.
Number of fishermen : 3
Enter lobster total collection in Kilogram : 12.4
Enter lobster total collection in Kilogram : 5.7
Enter lobster total collection in Kilogram : 9.8
Pice per kg is RM 67.00
Total lobster is : 9.8 Kg from 3 fishermen
Total price of lobster is RM : 656.6.00
Press any key to continue . . .
this is my code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/*
Objective: calculate total lobster
*/
#include <iostream>
using namespace std;
int j=0, sum=0;
float CalculateTotalLobster(int , float &);
float TotalPrice(float );
int main ()
{
int NumberofFishermen;
float KilogramofLobster;
cout << "This program will calculate the total lobster caught for"
<<" numbers of fisherman." <<endl<<endl;
cout << "Number of fishermen : " ;
cin >> NumberofFishermen;
cout<<endl;
for (int i=1;i<=NumberofFishermen;i++)
{
cout << "Enter lobster total collection in Kilogram : " ;
cin >> KilogramofLobster;
j++;
CalculateTotalLobster(NumberofFishermen, KilogramofLobster);
cout<<endl;
}
cout<<"Price per kg is RM 67.00" <<endl;
cout << "\nTotal lobster is : " << KilogramofLobster << " Kg from "
<< NumberofFishermen << " fishermen" ;
cout << "\nTotal price of lobster is RM : " << TotalPrice(KilogramofLobster)
<< ".00" << endl;
cout << endl;
system("PAUSE" );
return 0;
}
float CalculateTotalLobster(int a, float &b)
{
float sum;
sum = sum + b;
if (j==a)
b=sum;
}
float TotalPrice(float c)
{
float price;
price = c * 67.00;
return price;
}
Dec 17, 2012 at 4:56pm UTC
You are not adding KilogramofLobster anywhere, it's using the last value entered.
Here is what you need to add, you figure out where.
1 2 3 4 5 6 7 8 9
float TKilogramofLobster=0;
TKilogramofLobster=TKilogramofLobster+KilogramofLobster;
CalculateTotalLobster(NumberofFishermen, TKilogramofLobster);
cout << "\nTotal lobster is : " << TKilogramofLobster << " Kg from "
<< NumberofFishermen << " fishermen" ;
Last edited on Dec 17, 2012 at 4:58pm UTC
Dec 18, 2012 at 4:33am UTC
Why the 0.00?
Line 38.
Delete that:
1 2
cout << "\nTotal price of lobster is RM : " << TotalPrice(KilogramofLobster)
<< ".00" << endl;
and put:
printf ("\nTotal proce of lobster is RM: %.2f\n" ,TotalPrice(KilogramofLobster));
Last edited on Dec 19, 2012 at 2:54am UTC
Topic archived. No new replies allowed.