Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › c++: use value inputed in a function in main function? or other function?
New Posts  All Forums:Forum Nav:

c++: use value inputed in a function in main function? or other function?

post #1 of 3
Thread Starter 
This is for a homework assignment, that's why the code has to be written a certain way and contain specific things what I need help with, is I currently have values being input into functions ('volume' in the function containerVolume and 'beer' in the function beer) that I need to use in the main function. Does that make sense?

broken down:

function runs
user input
calculation gives result
function ends
more user input
*this is where I need help*use calculation from function in another calculation in main function
display results


Code:
// Calculates cost of one ounce of pure alcohol

#include <iostream>
using namespace std;

        float proof, volume, beerc, beerv, price, worth, content, ml;
        char beeryn;    // beer yes or no
        char help;      // need help converting yes or no



float containerVolume(float volume);
float beer(float beerv);

int main()
{


        float proof, volume, beerc, beerv, price, worth, content, ml;
        char beeryn;    // beer yes or no
        char help;      // need help converting yes or no



        containerVolume(volume);        // volume input & conversion
        cout << endl;
        beer(beerv);                    // volume calculation for a case of beer
        cout << endl;

        cout << "Enter the proof: ";
        cin >> proof;

        cout << "Enter the price: $";
        cin >> price;
        cout << endl;

        content = (volume)  * ( proof / 200 );
        worth = (price) / (content);

        cout << "There are " << content << " ounces of pure alcohol present" << end
l;
        cout << " which you are paying for at a rate of $" << worth;
        cout << " per ounce of pure alcohol." << endl;

        return 0;
}

float containerVolume(float volume)
{
        float ml;

        cout << "First, do you need help converting from milliliters to ounces?";
        cout << " (Y or N): ";
        cin >> help;

        if ((help == 'Y') || (help == 'y'))
        {
        cout << "How many milliliters is it? (If calculating for beer ";
        cout << "enter volume for one can of beer)";
        cin >> ml;
        volume = ml * 0.0338140226;

                if (ml <= 0)
                {
                cout << ml << " is not a possible volume! Enter a volume ";
                cout << "greater than 0." << endl;
                }
                else
                {
                volume = ml * 0.0338140226;
                cout << ml << " milliliters is equal to " << volume << " ounces" <<
 endl;
                }
        }


        if ((help == 'N') || (help == 'n'))
        {
        cout << "What is the volume? ";
        cin >> volume;
        }

        return(volume);
}

float beer(float beerv)
{
        cout << "Is this beer? (Y or N) ";
        cin >> beeryn;

        if ((beeryn == 'Y') || (beeryn == 'y'))
        {
        cout << "Enter number of beers in case: ";
        cin >> beerc;
        cout << "Enter volume of one can of beer in ounces: ";
        cin >> beerv;
        volume = beerv * beerc;                
        }

        if (((beeryn == 'N') || (beeryn == 'n')) && ((help == 'N') || (help == 'n')
))
        {
        cout << "Enter the volume in ounces: ";
        cin >> volume;
        }



        return(volume);
}
post #2 of 3
Either you can pass the variable by reference:

Ex. Define beer() like this:
Code:
float beer(float &beerv);
and just use normally. This will pass the address of the variable rather than the value and it will change it the value at the address.

Or, you can call beer() like this:

Code:
beerv beer(beerv)
This will pass the value of beerv and then set beerv to whatever is returned.
post #3 of 3
You're defining your functions to accept variables which you're not inputing into them. For example you're calling beer(beerv), but beerv is not given a value yet.

The way you're doing it is globally establishing volume and then when each function modified volume it's affecting the global variable.

I made a few changes below:

Code:
// Calculates cost of one ounce of pure alcohol

#include <iostream>
using namespace std;

float containerVolume(float volume);
float beer(float beerv);

int main()
{

        float proof, volume, beerc, beerv, price, worth, content, ml;
        char beeryn;    // beer yes or no

        cout << endl;
        beer(beerv);                    // volume calculation for a case of beer
        cout << endl;

        cout << "Enter the proof: ";
        cin >> proof;

        cout << "Enter the price: $";
        cin >> price;
        cout << endl;

        content = containerVolume()  * ( proof / 200 );
        worth = (price) / (content);

        cout << "There are " << content << " ounces of pure alcohol present" << end
l;
        cout << " which you are paying for at a rate of $" << worth;
        cout << " per ounce of pure alcohol." << endl;

        return 0;
}

float containerVolume()
{
        float ml, ounces, ml; 
        char help;
        

        cout << "First, do you need help converting from milliliters to ounces?";
        cout << " (Y or N): ";
        cin >> help;

        if ((help == 'Y') || (help == 'y'))
        {
        cout << "How many milliliters is it? (If calculating for beer ";
        cout << "enter volume for one can of beer)";
        cin >> ml;
        
        while (ml <= 0)
        {
                cout << ml << " is not a possible volume! Enter a volume ";
                cout << "greater than 0." << endl;
                }
                
                {
                ounces = ml * 0.0338140226;
                cout << ml << " milliliters is equal to " << volume << " ounces" <<
 endl;
        
        }
        return(ounces)
        }

        if ((help == 'N') || (help == 'n'))
        {
        cout << "What is the volume? ";
        cin >> ounces;
        
        return(ounces);
        }

        
}

float beer(float beerv)
{
        cout << "Is this beer? (Y or N) ";
        cin >> beeryn;

        if ((beeryn == 'Y') || (beeryn == 'y'))
        {
        cout << "Enter number of beers in case: ";
        cin >> beerc;
        cout << "Enter volume of one can of beer in ounces: ";
        cin >> beerv;
        volume = beerv * beerc;                
        }

        if (((beeryn == 'N') || (beeryn == 'n')) && ((help == 'N') || (help == 'n')
))
        {
        cout << "Enter the volume in ounces: ";
        cin >> volume;
        }



        return(volume);
}
there are still a few changes to be made. Modify your beer function, so that it is called during the volume function and it takes the ounces passed to it from volume and multiplies it by the number of beers and the volume function can return the value which beer() ultimately gives it.
    
CPUMotherboardGraphicsRAM
Xeon E5645 Asus Rampage II Gene eVGA GTX460 SSC 3x4GB Corsair Dominator PC3-12800 
Hard DriveHard DriveHard DriveOS
Crucial C300 64GB Seagate Momentus XT 750 Western Digital GP 2TB EARS Windows 7 Premium 
MonitorKeyboardPowerCase
Asus VE278Q DSI SMK-88 Seasonic X-660 Lian Li U6B 2011 SE 
MouseMouse Pad
Logitech G500 KAI.g3 HIEN - HARD 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Xeon E5645 Asus Rampage II Gene eVGA GTX460 SSC 3x4GB Corsair Dominator PC3-12800 
Hard DriveHard DriveHard DriveOS
Crucial C300 64GB Seagate Momentus XT 750 Western Digital GP 2TB EARS Windows 7 Premium 
MonitorKeyboardPowerCase
Asus VE278Q DSI SMK-88 Seasonic X-660 Lian Li U6B 2011 SE 
MouseMouse Pad
Logitech G500 KAI.g3 HIEN - HARD 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › c++: use value inputed in a function in main function? or other function?