question:
4. A program is required for a grocer that will add two weights together (in pounds and ounces). The program should also convert the total weight to a value in kilograms.

Use three functions.

Function 1: allows the user to enter the name of the produce, and the weights in pounds and ounces, which are validated and stored..

Function 2: calculates the total weight in pounds and ounces and converts that to kilograms.

Function 3: displays the name of the produce, the total weight in pounds and ounces and the total weight in kilograms.
Think about function 2 and the actual values that will be returned from this function.

The user should be given an option to convert another weight or end the program

Note: there are 16 ounces in one pound, and there are 0.454 kilograms in one pound.

here is what i have so far:

string product;
            Single weightp = 0f;
            Single weighto= 0f;
            Single convertp= 0f;
            Single ptokilo = 0.45359237f;
            Single converto= 0f;
            Single otokilo = 0.0283495231f;
            Single totalkiloweight = 0f;

            Console.WriteLine("the name of the produce");
            product = Console.ReadLine();

            Console.WriteLine("Enter weight in poounds");
            weightp = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter weight in Ounces");
            weighto = Convert.ToInt32(Console.ReadLine());

            convertp = weightp * ptokilo;

            Console.WriteLine("Here is pounds converted to kilograms");
            Console.WriteLine(convertp);

            converto = weighto * otokilo;

            Console.WriteLine("Here is Ounces converted to kilograms");
            Console.WriteLine(converto);

            totalkiloweight = converto + convertp;

            Console.WriteLine("Here is the total Weight in Kilograms for the two weights");
            Console.WriteLine(totalkiloweight);

            Console.ReadKey();

any idead how to add a loop iv tried a while loop shows so many errors when i try to add it

Recommended Answers

All 4 Replies

Here's a brief skeleton that you can work from. It seems like your instructor wants you to break off the methods (no functions in C#). There's an example do while loop along the lines. Flip ahead in your text to learn more about methods or do a quick net search.

static void Main(string[] args)
        {
            
            string name;
            int pds;
            int ozs;
            string another;
            do
            {
                //your other code here
                namePdsOzs(out name, out pds, out ozs);
                Console.WriteLine("Enter another? (N or n to exit)");
                another = Console.ReadLine();
                             
            } while (another.Substring(0,1) !="N" && another.Substring(0,1)!="n");

        }

        static void namePdsOzs(out string name, out int pds, out int ozs)
        {
            //read in name,pounds,ounces, do any conversions (so you   
            // may need to modify the return

        }

Please avoid creating duplicate threads. Escpecially when people have responded to your earlier post: need help. The forum shows threads in order of most recent posts so if you had answered the questions we asked your old thread would have been bumped back to the top of the list anyway.

Until you can clarify what your lecturer expects from the term 'function' we cant be sure what direction we need to guide you in. As i explained at length and jonsca mentioned, C# has no functions. Please answer us, in order that we can answer you.

I hadn't seen OP's original, else I would have referred OP back.

no worries jonsca, it was for memory100's benefit, not yours :p

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.