Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This code creates a table with dinner prices form 10 to 100 dollars and tip rates from 0.05 to 0.25. I have to alter it so users can type in the price of dinner and percentage of tip. I'm totally lost.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TippingTable.cs
{
    class TippingTable
    {
        static void Main()
        {
            double dinnerPrice = 10.00;
            double tipRate;
            double tip;
            const double LOWRATE = 0.10;
            const double MAXRATE = 0.25;
            const double TIPSTEP = 0.05;
            const double MAXDINNER = 100.00;
            const double DINNERSTEP = 10.00;

            Console.Write("   Price");
            for (tipRate = LOWRATE; tipRate <= MAXRATE; tipRate += TIPSTEP)
                Console.Write("{0, 8}", tipRate.ToString("F"));
            Console.WriteLine();
            Console.WriteLine("------------------------------------------");

            tipRate = LOWRATE;
            while (dinnerPrice <= MAXDINNER)
            {
               
                Console.Write("{0, 8}", dinnerPrice.ToString("C"));
                while (tipRate <= MAXRATE)
                {
                    tip = dinnerPrice * tipRate;
                    Console.Write("{0, 8}", tip.ToString("F"));
                    tipRate += 0.05;
                }
                dinnerPrice += DINNERSTEP;
                tipRate = LOWRATE;
                Console.WriteLine();
            }

            dinnerPrice += DINNERSTEP;
            tipRate = LOWRATE;
            Console.WriteLine();


        }
    }
}
Posted
Comments
Richard Deeming 10-Feb-15 12:55pm    
A combination of Console.ReadLine[^] and double.TryParse[^] should get you started.
Member 11403574 10-Feb-15 12:57pm    
Yeah, I'm not sure where to fit them in. I don't even know what a double.TryParse is
Richard Deeming 10-Feb-15 13:03pm    
Console.ReadLine returns a string containing the line of text that the user typed in.

double.TryParse attempts to convert a string to a double.

You need to ask the user to type in the price, read what they type in, and try to convert it to a double. If you can't convert it, loop until they enter a valid price.

Do the same thing for the tip rate.

Then do your calculations and write out the result.
Richard MacCutchan 10-Feb-15 13:08pm    
I also notice that you are still using Console.Write rather than Console.WriteLine as I suggested in your previous version of this question yesterday.
Member 11403574 10-Feb-15 15:30pm    
That's because the word 'Price' and the tip rates are supposed to be on the same line above the dotted line.

I would suggest you familiarise yourself with the MSDN documentation. It is full of interesting items, including https://msdn.microsoft.com/en-us/library/system.double.tryparse%28v=vs.110%29.aspx[^].

And the place to put your input requests is somewhere before the point at which you are using those values. You could work through .NET Book Zero by Charles Petzold[^], which explains many of these things in excellent clear detail, and includes lots of useful source code samples.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Feb-15 13:15pm    
5ed.
—SA
In addition to Answer 1:

Yes, you can use the methods System.Console.Read, ReadLine and ReadKey methods (https://msdn.microsoft.com/en-us/library/system.console%28v=vs.110%29.aspx[^]), but I would advise you to look at the professionally-written console-only utilities. You will find very few using interactive input, which work as a fully-fledged command interpreters, CMD.EXE is just one of them. The majority of the utilities, or any console-only applications, never use interactive input. This is done for a very good reason: if interactive input is really needed, for a "regular" application, a graphical UI is more suitable. So, interactive input in console-only application is just not needed and is generally inconvenient for a user. Your application is a typical example of such application.

Instead, such application provides all the input in a command line. If some volume of data is too big for a command-line parameter, the file name parameter is used, so this portion of detail is supplied in a file. This is much more convenient for a user, because one little mistake won't spoil long input work. The command line is passed to the entry point arguments (Main method) or can be obtained from System.Environment:
https://msdn.microsoft.com/en-us/library/system.environment.commandline%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.environment.getcommandlineargs(v=vs.110).aspx[^].

For command line parsing, please see my article or another CodeProject work I recommended in this article: Enumeration-based Command Line Utility[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900