Thread: help, please

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    13

    Question help, please

    i'm not sure what i'm doing wrong here........ maybe someone can help me out a little........ here's what i have so far. thanks!

    #include <iostream>
    #include <iomanip>
    using namespace std;

    float GetData (float miles, float hours, float milesPerHour);

    int main ()
    {
    float miles;
    float hours;
    float milesPerHour;

    cout.setf(ios::fixed, ios::floatfield);
    cout.setf(ios::showpoint);

    GetData (float miles, float hours, float milesPerHour);

    milesPerHour = miles / hours;

    cout << setw(10) << miles
    << setw(10) << hours
    << setw(10) << milesPerHour << endl;
    return 0;
    }

    //************************************************** ***

    float GetData (float miles, float hours, float milesPerHour)

    {
    cout<<"Please enter the amount of miles and hours.";
    cout<< "Press the enter key after each entry."<<endl;
    cin>>miles;
    cin>>hours;
    }

  2. #2
    Code tags. And I also noticed that you have GetData as returning a float, but in acuallity you are returning nothing.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    13
    sorry about the code tags. thanks.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    OK, there are three types of ways you can send a parameter to a function, by value, by reference, and with a pointer. The last two are essentially the same except for the syntax. By value is simple, it's what you did. It copies the data and sends it to the function. By reference sends the memory location of the variable you sent. That way, the function can modify them. Pointers are a bit more difficult, do a search.

    For reference parameters, put an amperstand (&) before the variable's name:
    Code:
    void swap(int &a, int&b);
    When calling a function, you do not need to specify the data type or apply special operators to the parameters. Simply, place their names (this is true for by value and by reference):
    Code:
    int a=3, b=4;
    swap(a,b);
    BTW, you can do this instead of what you had:
    Code:
    float miles;
    float hours;
    float milesPerHour;
    // ---------------------
    float miles, hours, milesPerHour;
    Be careful not to put your '/' beside your '*' because that makes a multi-line comment. Not good if you use notepad! (no syntax highlightning, you'll never know you did that)

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    float GetData (float &miles, float &hours, float &milesPerHour);
    
    int main()
    {
    	float miles;
    	float hours;
    	float milesPerHour;
    
    	cout.setf(ios::fixed, ios::floatfield);
    	cout.setf(ios::showpoint);
    
    	GetData(miles, hours, milesPerHour);
    
    	milesPerHour = miles / hours;
    
    	cout << setw(10) << miles
    	        << setw(10) << hours
    	        << setw(10) << milesPerHour << endl;
    
    	return 0;
    }
    
    // *****************************************************
    
    float GetData (float &miles, float &hours, float &milesPerHour)
    {
    	cout<<"Please enter the amount of miles and hours."; 
    	cout<< "Press the enter key after each entry."<<endl;
    	cin>>miles;
    	cin>>hours;
    }

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    And the return type for the GetData should be 'void'. Didn't catch that one, Munkey did.

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    13
    thanks!

  7. #7
    Oh, and it is a good habit to initialize stuff unless you will be assigning a value later. The problem with not initializing is if it has no currently set value and you do something such as increment it, you might end up with an unexpected answer.

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    13
    you're right. thanks!

Popular pages Recent additions subscribe to a feed