# Thread: Pretty simple for you good programers...

1. ## Pretty simple for you good programers...

Im a newbie of course and im taking a class to learning c++. We are in the functions chapter and this is my code which i have made so far.... i have no idea where to go what to do etc.

btw, I am doing problem 3.12
[Source Code]

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

Code:
```#include <iomanip>

using std::setprecision;
using std::setiosflags;
using std::setw;

int calculateCharges ( int )  //function prototype

int main()
{

int hr1,    //first hour
hr2,    //second hour
hr3,    //third hour
ttlhr,  //Total hours
cr1,    //First charge
cr2,    //second charge
cr3,    //third charge
ttlcr;  //Total charge

cout << "Enter 3 hours: " ;
cin >> hr1 >> hr2 >> hr3;

ttlhr = hr1 + hr2 + hr3;

cout << "Car" << setw( 10 ) << "Hours" << setw( 10 ) << "Charge"
<< "\n  1" << setw( 10 ) << hr1 << setw( 10 ) <<
<< "\n  2" << setw( 10 ) << hr2 << setw( 10 ) <<
<< "\n  3" << setw( 10 ) << hr3 << setw( 10 ) <<
<< "\nTotal" << setw( 10 ) << ttlhr << setw( 10 ) << ttlcr;

return 0;
}

//function definition
int calculateCharges ( int hours )

{
int crg = 0;

if ( hours <= 3 )
crg = 2;

if ( hours > 3 )
crg = ( ( hours - 3 ) * .5 ) + 2

if ( hours == 24 )
crg = 10;

return crg;
}```
This is the original problem out of the book:
http://members.cox.net/eazhar/prob3.jpg

2. That's an interesting use of code tags, useing them after the start of your code...
Anyway, w/o reading the original problem, you have an error right off the bat with your if statements.
You check if hours are greater than 3, and if they equal 24. Both conditions can be true. You might look at else if or case statements.
You also never call calculateCharges in main, but maybe that's elsewhere in the problem.

3. ## New code

Code:
```#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <iomanip>

using std::setprecision;
using std::setiosflags;
using std::setw;

double calculateCharges ( double );  //function prototype

int main()
{

double   hr1,    //first hour
hr2,    //second hour
hr3,    //third hour
ttlhr,  //Total hours
cr1,    //First charge
cr2,    //second charge
cr3,    //third charge
ttlcr;  //Total charge

cout << "Enter 3 hours: " ;
cin >> hr1 >> hr2 >> hr3;

cr1 = calculateCharges(hr1);
cr2 = calculateCharges(hr2);
cr3 = calculateCharges(hr3);

ttlcr = cr1 + cr2 + cr3;
ttlhr = hr1 + hr2 + hr3;

cout << "Car" << setw( 10 ) << "Hours" << setw( 10 ) << "Charge"
<< "\n  1" << setw( 10 ) << hr1 << setw( 10 ) <<  cr1
<< "\n  2" << setw( 10 ) << hr2 << setw( 10 ) <<  cr2
<< "\n  3" << setw( 10 ) << hr3 << setw( 10 ) <<  cr3
<< "\nTotal" << setw( 10 ) << ttlhr << setw( 10 ) << ttlcr;

return 0;
}

//function definition
double  calculateCharges ( int hours )

{
double crg = 0;

if ( hours <= 3 ){
crg = 2.0;
}

if ((hours > 3.0) && (hours < 24.0)) {
crg = ( ( hours - 3.0 ) * .5 ) + 2.0;
}

if ( hours == 24.0 )
crg = 10.0;

return crg;
}```
2 errors-
cred.obj : error LNK2001: unresolved external symbol "double __cdecl calculateCharges(double)" (?calculateCharges@@YANN@Z)

Debug/cred.exe : fatal error LNK1120: 1 unresolved externals

UPDATE: Nevermind i found my problem

double calculateCharges ( int hours ) should be double hours not int

4. In fact there is just one error, the second error is the result of the first error.

The prototype has double as input parameter, the implementation has int. So the compiler thinks of this as two different functions. The first is not implemented, the second not prototyped.

5. ## 2nd question

okay now im doing the second question on the link i've posted (3.13)

Code:
```#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <iomanip>

using std::setw;

#include <cmath>

double roundNum ( double );  //function prototype

int main()
{

double num;  //inputted number
int res;     //outputted result

cout << "Enter number: ";
cin >> num;

res = roundNum ( num );

cout << "Rounded result is: " << res << endl;
cout << "\n";

return 0;
}

//function definition
double roundNum ( double x )
{

int y = 0;

y = floor( x + .5 );

return x;
}```
Its executes and everything great but the instructions want it to be able to process several numbers.

I was thinking of using a while loop but im a bit confused on where i am supposed insert it

Thanks for any replies.

6. Code:
```double num = 0;

while (num != -1)
{
cout << "Enter number (-1 to exit): ";
cin >> num;

res = roundNum ( num );

cout << "Rounded result is: " << res << endl;
cout << "\n";
}```
Couple of other problems...

- The res variable is an int, which conflicts with the return type of the roundNum() function. Suggest changing the function return type.
- In roundNum() you are returning x, whereas I think you meant to return y.