by using functions, write a CPP program that calculates a water bill for a user. In order to compute the water bill, user needs to input the unpaid balance, the current and previous meter reading. RM1.10 will be charged for every thousand gallons. A surcharge of 2% is added to every unpaid balance.
.

my program doesn't seem to work. what are the mistakes?

#include <iostream.h>
#include <cmath>

using namespace std;

double unpaid(int a)
{
       double unpaid;
       unpaid=(float)(a+(a*(2/100)));
       return unpaid;
       
}

double current(int b)
{
       double current;
       current=(b/1000)*1.10;
       return current;
}

double previous(int c)
{
       double previous;
       previous=(c/1000)*1.10;
       return previous;
}

int main(void)
{
    double previous;
    double current;
    double unpaid;
    
    cout<<"Please enter your unpaid balance in RM";
    cin>>unpaid;
    cout<<"Please enter your current meter reading in Gallons";
    cin>>current;
    cout<<"Please enter your previous meter reading in Gallons";
    cin>>previous;
    cout<<"Your bill is" "  "<<unpaid+(current-previous);
    
    system("pause");
    return 0;

    
}

It wouldn't compile on Dev without using <iostream> instead of <iostream.h>... otherwise. The other problem is that you're declaring functions that you never call. When you declare double previous, current and unpaid in main(), you are creating variables with those names, not calling the functions. Try this:

#include <iostream>
#include <cmath>

using namespace std;

double unpaid(int a)
{
       double unpaid;
       unpaid=(float)(a+(a*(2/100)));
       return unpaid;

}

double current(int b)
{
       double current;
       current=(b/1000)*1.10;
       return current;
}

double previous(int c)
{
       double previous;
       previous=(c/1000)*1.10;
       return previous;
}

int main()
{

int a, b, c;
double d, e, f;
cout<<"Please enter your unpaid balance in RM";
cin>>a;
cout<<"Please enter your current meter reading in Gallons";
cin>>b;
cout<<"Please enter your previous meter reading in Gallons";
cin>>c;
d = previous(a);
e = current(b);
f = unpaid(c);
cout<<"Your bill is" " "<<d+(e-f);

system("pause");
return 0;


}

It wouldn't compile on Dev without using <iostream> instead of <iostream.h>... otherwise. The other problem is that you're declaring functions that you never call. When you declare double previous, current and unpaid in main(), you are creating variables with those names, not calling the functions. Try this:

#include <iostream>
#include <cmath>

using namespace std;

double unpaid(int a)
{
       double unpaid;
       unpaid=(float)(a+(a*(2/100)));
       return unpaid;

}

double current(int b)
{
       double current;
       current=(b/1000)*1.10;
       return current;
}

double previous(int c)
{
       double previous;
       previous=(c/1000)*1.10;
       return previous;
}

int main()
{

int a, b, c;
double d, e, f;
cout<<"Please enter your unpaid balance in RM";
cin>>a;
cout<<"Please enter your current meter reading in Gallons";
cin>>b;
cout<<"Please enter your previous meter reading in Gallons";
cin>>c;
d = previous(a);
e = current(b);
f = unpaid(c);
cout<<"Your bill is" " "<<d+(e-f);

system("pause");
return 0;


}

the program display negative output no matter what value input it is. what's wrong? is it because of the formula?

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.