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
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??
Yes.
Help us, help you.
gg
The only reason that the implementation is below main(), is to add structure to the source file. C/C++ programmers usually write the function implementations after main().Code:#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!" );
}
The user is supposed to enter in three sets of numbers using for statements, which is why I included the for (int x =1) statement. And that value is supposed to be displayed under hours. And from there I will have to calculate the charges.Code:#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.
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:
You almost have the loop for displaying results. Get rid of the "cin >> x;" and use braces with your for loop. Then move the first cout line outside the for loop (you don't want to print the header 3 times).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
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.
Now, what you need to do is write a function to calculate the values that you need from the input that you get, iow the total hours, charges etc.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;
}
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:
ggCode: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
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.
This is where I stand now. Can anyone help answer or lead me in the right direction to achieve my goal?Code:#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