Homework Help Using Functions

This is a discussion on Homework Help Using Functions within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to write a program that will give me miles per hour. This is what I have so ...

  1. #1
    Registered User Trekkie's Avatar
    Join Date
    Oct 2005
    Posts
    20

    Homework Help Using Functions

    Hi,

    I'm trying to write a program that will give me miles per hour. This is what I have so far:

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    void GetData( float&, float& );//Fill in the function prototype for GetData
    
    int main()
    {
    float miles;
    float hours;
    float milesPerHour;
    cout << "Input miles and hours "<<endl;
    cin >> milesPerHour;
    
    cout << fixed << showpoint;
    
    cout << "The miles per hour are: " <<endl;
    GetData(miles, hours);
    
    //Fill in the code to invoke the function GetData
    
    milesPerHour = miles / hours;
    cout << setw(10) << miles
         << setw(10) << hours
         << setw(10) << milesPerHour << endl;
     return 0;
     }
    I'm getting 2 error messages after I try and run this program. They are: lab3 error LNK2001: unresolved external symbol "void __cdecl GetData(float &,float &)" (?GetData@@$$FYAXAAM0@Z)

    and the second error is: lab3 fatal error LNK1120: 1 unresolved externals

    Can someone help me please??

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,321
    You have to implement the GetData function. In that function you should be reading in miles and hours from the user. You shouldn't be reading in milesperhour from the user since you are supposed to be calculating it from the miles and hours.

  3. #3
    Registered User Trekkie's Avatar
    Join Date
    Oct 2005
    Posts
    20
    Isn't that what I did when I used:

    GetData(miles, hours);

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by Trekkie
    Isn't that what I did when I used:

    GetData(miles, hours);
    That says to execute the function called GetData(). Where is your GetData() function? This is not a function:
    Code:
    void GetData( float&, float& );
    That is a declaration of a function. A function must be defined somewhere, and it will contain the code you want to execute when you call the function:

    GetData(miles, hours);

    Programming would be very easy if we didn't have to define functions and instead the computer could read our minds and execute the code we would like. If that were possible, all my programs would look like this:
    Code:
    void ExecuteTheProgramThatMakesMeAMillionDollars();
    
    int main()
    {
          ExecuteTheProgramThatMakesMeAMillionDollars();
    }
    I could just execute that a few times a day while sipping Strawberry Daiquiris in the Carribean.
    Last edited by 7stud; 11-20-2005 at 06:39 PM.

  5. #5
    Registered User Trekkie's Avatar
    Join Date
    Oct 2005
    Posts
    20
    I made some changes, but I am still getting the same error messages. Here is what my code looks like now:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    void GetData( float&, float& );//Fill in the function prototype for GetData
    
    int main()
    {
    float miles;
    float hours;
    float milesPerHour;
    
    cout << "Input miles "<<endl;
    cin >> miles;
    cout << "Input hours "<<endl;
    cin >> hours;
    
    cout << fixed << showpoint;
    
    cout << "The miles per hour are: " <<endl;
    GetData(miles, hours);//the code to invoke the function GetData
    
    milesPerHour = miles / hours;
    cout << setw(10) << miles
         << setw(10) << hours
         << setw(10) << milesPerHour << endl;
     return 0;
     }

  6. #6
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Code:
    #include <iostream>
    
    using namespace std;
    
    void funk(int first, int second); //Function prototype
    
    
    int main()
    {
    
    	int something_else;
    	int something;
    
    	cout << "Enter some integer: ";
    	cin >> something;
    
    
    	cout << "Enter something other integer: ";
    	cin >> something_else;
    
    
    	funk(something, something_else); //Invokes function called funk...when this happen it jumps to the *********
    
    
    	cout << endl << "This is after the call to funk";
    
    	cin.ignore();
    	cin.get();
    
    	return 0;
    
    }
    
    /************************************* Exectues the function called funk (below here) */
    
    void funk(int first, int second)
    {
    
    	cout << first + second;
    }
    //When everything in the {} is done, it picks back up in main after the function call
    Last edited by Enahs; 11-20-2005 at 07:19 PM.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    Code:
    void GetData(float& miles, float& hours)
    {
           float milesPerHour = miles / hours;
    cout << setw(10) << miles
         << setw(10) << hours
         << setw(10) << milesPerHour << endl;
    }

  8. #8
    Registered User Trekkie's Avatar
    Join Date
    Oct 2005
    Posts
    20
    Thank you so much! I can finally follow the code. I didn't know how or where to input the void GetData part. I'm still learning, so thanks for your patience!

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by Trekkie
    Thank you so much! I can finally follow the code. I didn't know how or where to input the void GetData part. I'm still learning, so thanks for your patience!
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    void GetData( float& var1, float& var2 )
    {
         //PUT THE CODE FOR THE FUNCTION HERE
    }
    
    
    int main()
    {
    float miles;
    float hours;
    float milesPerHour;
    cout << "Input miles and hours "<<endl;
    cin >> milesPerHour;
    
    cout << fixed << showpoint;
    
    cout << "The miles per hour are: " <<endl;
    GetData(miles, hours);
    
    //Fill in the code to invoke the function GetData
    
    milesPerHour = miles / hours;
    cout << setw(10) << miles
         << setw(10) << hours
         << setw(10) << milesPerHour << endl;
     return 0;
     }
    Looking at your code, it's apparent that you want to read in two(2) values from the user: 'miles' and 'hours'. If the user entered milesPerHour, what would be the purpose of your program? Your program is supposed to calculate the milesPerHour for the user.

    You seem to think that a computer will do what you say in a string, e.g.: "Input miles and hours". It doesnt work that way. If it did you could write your whole program like this:
    Code:
    int main()
    {
        cout<<"Get two pieces of data from the user, calculate mph, and display it."
        
        return 0;
    }
    A computer doesn't understand the English language. All that statement will do is display that exact string in the console window, and then do nothing. For your program to work, you have to write the C++ statements that will read in two values, miles and hours, in the correct order, and then because mph is defined to be miles divided by hours, you have to do the division and display the result.

    I would suggest you buy a beginning C++ book, like "C++: A Beginner's Guide". In the meantime, here is some information on functions:
    -----
    Here is a very simple function:
    Code:
    #include <iostream>
    using namespace std;
    
    void showNumber()
    {
       cout<<100<<endl;
    }
    
    int main ()
    {
       showNumber();
    
       return 0;
    }
    Functions must specify a 'return type', and the return type precedes the function name. In the function above, there is no return value, and that is designated by 'void'. The function is 'called' in main() by this line:

    showNumber();

    That says, "please go find a function named showNumber, and execute the lines of code contained therein."

    Here is another example:
    Code:
    #include <iostream>
    using namespace std;
    
    void showNumber(int a)
    {
       cout<<a<<endl;
    }
    
    int main ()
    {
       showNumber(100);
       showNumber(25);
    
       return 0;
    }
    Now, instead of always displaying the same number, you can send the function a specific number, and the function will display whatever number you send it. Once again, the function does not return a value, so its return type is 'void'. You should note that the type of the number you send to the function must match the function 'parameter'. The function parameter above is "int a", which means the function is expecting an int type to be 'sent' to it. You send a value to a function like this:

    showNumber(100);

    To send a value to a function, you just put the value between the parentheses after the function name. The function parameter "int a" creates an integer variable named "a" which stores the value you send to the function--that's why the type of the value you send has to match the function parameter. Then, inside the function you can use the variable "a" by referring to it by name:

    cout<<a<<endl;

    Here is another example:
    Code:
    #include <iostream>
    using namespace std;
    
    int addTen(int a)
    {
       int answer = a + 10;
       return answer;
    }
    
    int main ()
    {
       int result1 = addTen(2);
       int result2 = addTen(5);
    
       cout<<"Here are the results: "<<result1<<" "<<result2<<endl;
       
       return 0;
    }
    This time the function returns a value. The return type preceding the function name must match the type of the value in the 'return statement'. The return statement replaces the function call in main(), e.g. addTen(2), with the return value. So, result1 is assigned 12, and result2 is assigned 15.

    To summarize:
    1) A function must list a return type before its name.
    2) The return type must match the type of the value in the return statement--if there is no return statement, then the type is 'void'.
    3) When you call a function, the value you send it must match the function parameter.
    4) The function call in main() is replaced by the function return value.
    5) Sometimes if a function doesn't have any parameters, like in the first example, "void" will be used for the parameter. For instance you might see the function in the first example written like this:
    Code:
    void showNumber(void)
    {
       cout<<100<<endl;
    }
    (void) is equivalent to ( )
    Last edited by 7stud; 11-20-2005 at 10:01 PM.

  10. #10
    Registered User Trekkie's Avatar
    Join Date
    Oct 2005
    Posts
    20
    Thank you so much for taking the time to explain this to me. I've printed this page and will add it to my notes. I feel like I've jumped into more advanced programming than I am ready for. My teacher is not very good, so I have to rely on the textbook that I have for help and the textbook is awful! It's nice having a forum like this where I can ask questions and there is always someone to help!

    Thanks again!

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    so I have to rely on the textbook that I have for help and the textbook is awful!
    Why struggle with a bad text book? Get a good one to look up stuff you don't understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  2. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  3. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  4. Inline functions and inheritance
    By hpy_gilmore8 in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2004, 05:46 PM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM

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