Function Prototype problem

This is a discussion on Function Prototype problem within the C++ Programming forums, part of the General Programming Boards category; I have a very generalized, very basic idea of how function prototypes work, but I am stuck on setting them ...

  1. #1
    NIM
    NIM is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    24

    Function 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??

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,652
    Yes.
    Help us, help you.

    gg

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    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 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().

  4. #4
    NIM
    NIM is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    24
    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;
    
    }
    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.

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

    I just need a nudge in the right direction.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,652
    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
    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).
    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

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    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;
    }
    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.

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,652
    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
    gg

  8. #8
    NIM
    NIM is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    24
    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.
    Last edited by NIM; 10-26-2003 at 01:18 AM.

  9. #9
    NIM
    NIM is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    24
    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;
    }
    This is where I stand now. Can anyone help answer or lead me in the right direction to achieve my goal?

    Thanks in advance

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  5. Replies: 5
    Last Post: 02-08-2003, 06:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21