First time using void functions. Apologies again for the noobish thread.
I have a few questions on this problem.

1) Since the variables miles, gallons and milesPerGallon are used in both main and void, do I need to initialize them in each place? Or if they're initialized in main do I need to initialize them in void as well?

2) I'm not passing any arguments to the void, but rather expecting void to return two values to main. How do I set up the void prototype and the actual function call?

The professor wants us to fill in the prototype and documentation, fill in the code to invoke to void function, and fill in the void function heading, documentation, and actual prompts and read-ins of miles and gallons.

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

void GetMnG (float, float);
// This function asks user to input two real numbers, miles and gallons. it then calculates milesPerGallon and displays it to the page


int main ()
{
    float  miles = 0;
    float  gallons = 0;
    float  milesPerGallon = 0;
	
	void GetMnG ();
    	
	cout  << fixed  << showpoint;
    cout  << setw(10)  << miles
		  << setw(10)  << gallons
          << setw(10)  << milesPerGallon  << endl;
    return 0;
}

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

void GetMnG ()
{
// Precondition: This function requires two real number inputs, miles and gallons
// Post Condition: This function feeds back the two numbers to main, which calculates and displays miles ped gallon
	
	float  miles = 0;
    float  gallons = 0;
	float  milesPerGallon = 0;

	cout << "Input miles as a real number and enter" << endl;
	cin >> miles;
	cout << "Input gallons as a real number and enter" << endl;
	cin >> gallons;
	milesPerGallon = miles / gallons;
}

Recommended Answers

All 9 Replies

Where and how you initialize them depends on the structure of the program.
How much of that structure did the prof give you, and what changes did you make?
It may be easiest if you post the given structure as a reference. That would allow us to see your changes and comment on your changes more effectively.

Where and how you initialize them depends on the structure of the program.
How much of that structure did the prof give you, and what changes did you make?
It may be easiest if you post the given structure as a reference. That would allow us to see your changes and comment on your changes more effectively.

This is the original program as given by the prof.

// file:     milesPerGallon.cpp
// modified:
// date:

// Program milesPerGallon reads miles and hours and prints miles
// per hour.

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

/* FILL IN the function prototype for GetData */
// FILL IN the Documentation!!

int main ()
{
    float  miles;
    float  gallons;
    float  milesPerGalon;

    cout  << fixed  << showpoint;

    /* FILL IN code to invoke function GetMnG */

    milesPerGalon = miles / gallons;
    cout  << setw(10)  << miles
          << setw(10)  << gallons
          << setw(10)  << milesPerGalon  << endl;
    return 0;
}

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

/* FILL IN the function heading for GetMnG */
{
// Fill In the DOCUMENTATON!!
// Precondition:
// Post Condition:

    /* FILL IN Code to prompt for miles and gallons */
    /* FILL IN Code to read miles and gallons */
}

You only need to initialize them in one place, but you may have trouble doing this problem in the fashion you're trying to.

Your second question states that you want to return 2 variables from a function, which is not possible. A void return type means the function returns nothing.

Try declaring the variables in main and pass them to your get function as references, then set their final values in that function.

That structure is pretty much what I was expecting. Sodabread has covered what I intended to. Are you familiar with either pointers or passing by reference?

Sodabread has covered what I intended to.

Yeah, sorry about that, F. I started typing, got up for a couple minutes, got back to my desk and finished the post. I guess I was a couple minutes late =)

Yeah, sorry about that, F. I started typing, got up for a couple minutes, got back to my desk and finished the post. I guess I was a couple minutes late =)

meh.... no worries...

I think I've got it! I looked at a sample program in the textbook and noted a number of errors in my initial program. I made some changes and now it seems to work as I want it to. Now I get it about passing references to the void function.

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

void GetMnG (float& miles, float& gallons);
// This function asks user to input two real numbers, miles and gallons. it then calculates milesPerGallon and displays it to the page


int main ()
{
    float  miles;
    float  gallons;
    float  milesPerGallon;
	
	GetMnG (miles, gallons);
    milesPerGallon = miles / gallons;	
	cout  << fixed  << showpoint;
    cout  << setw(10)  << miles
		  << setw(10)  << gallons
          << setw(10)  << milesPerGallon  << endl;
    return 0;
}

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

void GetMnG (float& miles, float& gallons)
{
// Precondition: This function requires two real number inputs, miles and gallons
// Post Condition: This function feeds back the two numbers to main, which calculates and displays miles ped gallon
	
	cout << "Input miles as a real number and enter" << endl;
	cin >> miles;
	cout << "Input gallons as a real number and enter" << endl;
	cin >> gallons;
	}

Looks like you got the right idea. Solved?

Looks like you got the right idea. Solved?

Solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.