I have a very generalized, very basic idea of how function prototypes work, but I am stuck on setting them up to work for my program.

Can anyone help??

Printable View

- 10-25-2003NIMFunction Prototype problem
I have a very generalized, very basic idea of how function prototypes work, but I am stuck on setting them up to work for my program.

Can anyone help?? - 10-25-2003Codeplug
Yes.

Help us, help you.

gg - 10-25-2003The DogCode:
`#include <stdio.h>`

//This is a function prototype

void welcome(); //Don't forget the semi-colon

int main( void )

{

welcome(); //Here we call the function

return 0;

}

//This is a function implementation

void welcome() //Notice that there's no semi-colon

{

printf( "Welcome to the wonderful world of functions!" );

}

- 10-25-2003NIMCode:
`#include <iostream>`

#include <iomanip>

using std::cout;

using std::endl;

using std::setw;

using std::cin;

int main ()

{

int car1 = 0;

int car2 = 0;

int car3 = 0;

int hours = 0;

double totalhours = 0;

double calculatetotal = 0;

double calculatecharges = 0;

cout << "Enter the number of hours parked for three cars: ";

for ( int x = 1; x <= 3; x++ )

cin >> x;

cout << "\nCar" << setw(13) << "Hours" << setw(21) << "Charge";

cout << "\n 1" << setw(11) << car1 << setw(21) << calculatecharges;

cout << "\n 2" << setw(11) << car2 << setw(21) << calculatecharges;

cout << "\n 3" << setw(11) << car3 << setw(21) << calculatecharges;

cout << "\n Total" << setw(11) << totalhours << setw(21) << calculatetotal << endl;

return 0;

}

I don't expect anybody to finish the code - because I need to learn.

I just need a nudge in the right direction. - 10-25-2003Codeplug
You can't get input and display the results at the same time.

If you want to have multiple statements inside a for loop, then you have to use braces:

Code:`for (...)`

{

statement1;

statement2;

statement3;

}//for

Once you have your ouput looking good, work on the input loop.

If part of the requirement is that you get your input using a loop, then you'll want to use an array instead of car1, car2, and car3.

gg - 10-25-2003The Dog
Basically, you'll need to write a function to calculate the charges for you. You need to pass a value to the function and return a value from the function.

Here's a function that calculates the product of two numbers and returns the result.

Code:`#include <iostream>`

using std::cout;

//The prototype : We are passing two integer values to the function, and we are returning an integer

int product( int a, int b );

int main()

{

//Here's how we'd use it

int prod = product( 5, 6 );

cout << prod;

return 0;

}

//Implementation

int product( int a, int b )

{

int result = a * b;

return result;

}

- 10-25-2003Codeplug
Let me try again (now that I see your code differently).

You don't want to get input into your loop variable. Here is a simple example:

Code:`const int SIZE = 3;`

int a[SIZE];

int n;

//input loop

for (n = 0; n < SIZE; n++)

{

cout << "Enter a number: ";

cin >> a[n];

}//for

//output loop

for (n = 0; n < SIZE; n++)

{

cout << "Number " << (n + 1) << " is " << a[n] << endl;

}//for

- 10-26-2003NIM
is there any way to have a for input loop to print on the same line?

I.e

Enter number: 1 2 3

Number 1 is 1

Number 2 is 2

Number 3 is 3

Based on Codeplug's code how can you add the total number entered i.e

Enter hours: 8 1 9

Car Hours

1 8

2 1

3 9

Total 18

EDIT: Still searching for the above answer but I am editing the post to ask another question. - 10-26-2003NIMCode:
`#include <iostream>`

#include <iomanip>

using std::cout;

using std::cin;

using std::setw;

using std::endl;

int main ()

{

double calculatecharges = 0;

double calculatetotal = 0;

int totalhours = 0;

const int SIZE = 3;

int a[SIZE];

int n;

//input loop

for (n = 0; n < SIZE; n++)

{

cout << "Enter the hours parked for three cars: ";

cin >> a[n];

}//for

cout << "\nCar" << setw(11) << "Hours" << setw(19) << "Charge";

//output loop

for (n = 0; n < SIZE; n++)

{

cout << "\n " << (n + 1) << setw(9) << a[n] << setw(19) << calculatecharges;

}//for

cout << "\n Total" << setw(6) << totalhours << setw(19) << calculatetotal << endl;

if ( a[n] <=3)

calculatecharges = 2;

return 0;

}

Thanks in advance