help with creating code for conversion of ounces to cups then cups to pints

if someone could help me, these are the requirements:


1. all functions must be below the main program

2. cannot perform Math in your cout statement

3. Use double

4. set precision to 2 for ounces per cup

5. set precision to 3 for cups per pint

6. a loop in the program is required
cout<<"Welcome to my conversion program. Would you like to convert ounces to cups?"<<"Enter y(yes) or n(no)";

7. make the loop non-case sensitive

8. send ounces from main() to oz_to_cups() function, then return answer back to main and display the answer.

9. now ask the user if they would like to convert cups to pints and send your cup answer from main to cups_to_pints function, then return pints answer back to main and display the answer.

10. then ask the user: cout<<"Welcome to my conversion program. Would you like to convert ounces to cups?"<<"Enter y(yes) or n(no)";

I just do not know where to start, if someone could show me an example that would be great!

closed account (2LzbRXSz)
Start with your first function, oz_to_cups() Since you'll want it to return a value, let's make it int oz_to_cups(double ounce) In there, you will take ounce and do the math to make it a cup, which should be a pretty simple step. After you've got that down, at the bottom of the function write, return ounce (Remember! You have to define your function before main in order for it to work.)

After that, setup int cups_to_pints(double cups)

Steps 4 and 5...? I don't mean to sound stupid, but what do you mean by
set precision to 2
?

As for the loop, I'd suggest a while loop, which you can read more about here.
http://www.cplusplus.com/doc/tutorial/control/

You'll need a variable, for example... bool whileTrue in order for the loop to run. Make sure that whileTrue becomes false, or else you'll be stuck in an infinite loop.

In that loop, you'll have
cout<<"Welcome to my conversion program. Would you like to convert ounces to cups?"<<"Enter y(yes) or n(no)";
Get the input, and use an if statement to check. If you don't want it to be case sensitive, you could convert all the characters in the string to uppercase (or lowercase) that way you don't have to worry about case sensitivity. Otherwise, using an || (or) operator could work just fine.
1
2
3
4
if (userinput == "yes" || userinput == "YES")
{
    // Get input again. Send that input to oz_to_cups()
} 


Do the same process when asking if the user wants to convert cups to pints

I tried to be vague so that you had some room to figure stuff out yourself, but if I was too vague my apologies! There are a few extra steps you could take to make the program more efficient but since it's not specified to do so in the directions, don't worry about it.

If you want me to take a look at your code once you've got it all down, or if something doesn't make sense I wouldn't mind helping at all:)
Last edited on
how do i get this to run, its not working the way that i want and am really banging my head against the wall over this!

here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
double pints(0);
double oz_to_cup(double num);
double cup_to_pint();
double num;
int main()
{
	cout << "Welcome! \n" << endl;
	cout << "Please enter the amount of ounces you want to convert to cups:" << endl;
	cin >> num;
	cout << oz_to_cup(num) << endl;

}
double oz_to_cup(double num)
{
	num = num / 8;

	return num;
}


all im trying to do is add the pints formula to it and to run properly, i have still yet able to figure out how to add the part when it asks how convert from ounces to pints after it has done the ounces to cups part
closed account (2LzbRXSz)
I don't recommend having double num; defined outside of int main() That's really only done if you need it to have a global scope, and it's not necessary for this program (at least, not in my eyes. I actually think it having a global scope could mess up re using the variable for the other function). You can read about that here http://www.cplusplus.com/doc/tutorial/namespaces/

Are you having trouble writing the program? Or trouble figuring out how to incorporate that part? Writing the program, you would ask the user if they want to convert from ounces to pints at this point (and get their input with a string value using getline). Which, would work just like oz_to_cup(double num) (but with the pints formula).

Since oz_to_cup(double num) doesn't use a reference operator, it does not change the value of the variable in int main() called double num; it just passes the value num has at the time, through the function, and returns the value of num divided by 8. That way, you can still use num's value, which holds OUNCES, to convert ounces to pints:)

You can read about that here http://www.cplusplus.com/doc/tutorial/functions/ under "Arguments passed by value and by reference"
is there any way you could show me what you mean?
closed account (2LzbRXSz)
Sure! I can walk you through the whole thing, but I have a question. Do you need to convert cups to pints? Or ounces to pints? I'm a little confused because I just re read your original post, and it says cups_to_pints, but your second one says ounces to cups.

I'm just going to go based off of the first post. In that case, we'll be needing the opposite of what I said in my last post. You'll need a reference operator so that num's value is changed as a result of the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>

using namespace std;

// I changed your variable names, just so it'd be a little less confusing
double oz_to_cup(double &ounces); // A reference operator is used so that the value is changed by the function
double cup_to_pint(double cups); // A reference operator isn't needed here, because we don't HAVE to change the value
int main()
{
    double num;
    string userinput;
    cout << "Welcome! \n" << endl;
    cout << "Please enter the amount of ounces you want to convert to cups:" << endl;
    cin >> num;
    cin.ignore(); // So it doesn't mess up getline(cin, userinput);
    cout << oz_to_cup(num) << endl;
    cout << "Would you like to convert cups to pints? Enter (y)es or (n)o." << endl;
    getline(cin, userinput); /* Get the user's input, but accounts for multiple character input.
                              You could also change userinput to a char, and use cin instead of getline.
                              I just prefer getline, but it isn't necessary, since we really only need
                              1 character input. Feel free to change this! */
    if (userinput == "y" || "Y") // "y" as in "yes". It also checks if the userinput was in capital letters.
    {
        cout << cup_to_pint(num) << endl;
    }
    else // If the user doesn't say yes
    {
        exit(0); // The program exits with a "0". 0 meaning that the program exited with no failures.
    }
    return 0; // Meaning the same thing as "exit(0);" but used as the return value for int main()
}

double oz_to_cup(double &ounces)
{
    ounces = ounces / 8;
    
    return ounces;
}

double cup_to_pint(double cups)
{
    cups = cups / 2; // 2 cups == 1 pint
    return cups;
}


I know that seeing it is a lot easier than trying to figure out what I was explaining (it's also easier for me to get my point across). I don't want you just copying and pasting this though if you don't understand how it works, because that's cheating. If there's anything you don't understand, let me know:) I tried to make it as a clear as possible.

You also had a function defined, double pints(0); but I wasn't sure what it's purpose was, and I didn't find it necessary in my code - so I took it out.
oh my, this makes so much more sense, this was the part that i kept getting messed up on
1
2
3
4
5
6
7
8
9
10
11
12
double num;
    string userinput;
    cout << "Welcome! \n" << endl;
    cout << "Please enter the amount of ounces you want to convert to cups:" << endl;
    cin >> num;
    cin.ignore(); // So it doesn't mess up getline(cin, userinput);
    cout << oz_to_cup(num) << endl;
    cout << "Would you like to convert cups to pints? Enter (y)es or (n)o." << endl;
    getline(cin, userinput); /* Get the user's input, but accounts for multiple character input.
                              You could also change userinput to a char, and use cin instead of getline.
                              I just prefer getline, but it isn't necessary, since we really only need
                              1 character input. Feel free to change this! */


i just didnt know how to write it properly to work with the functions
1
2
3
4
5
6
7
8
9
10
11
12
double oz_to_cup(double &ounces)
{
    ounces = ounces / 8;
    
    return ounces;
}

double cup_to_pint(double cups)
{
    cups = cups / 2; // 2 cups == 1 pint
    return cups;
}


i salute you for everything you know about c++ programming!

i will not be copying and pasting, i will be sharing this knowledge with fellow students!
closed account (2LzbRXSz)
Haha, thank you! I'm glad I could help you, and help your classmates:)
Topic archived. No new replies allowed.