Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 12th, 2007, 12:38 AM
Registered User
 
Join Date: Apr 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sentinel Loop Question

I am newbie at this, please try to be patient with me. I apologise in advance for the length of this post. In my class at school we have been trying out various loops in problems. The instructor calls the one we've been using a "Sentinel Loop". If there is another name for it I don't know what it is. :(

I can get it to accept the input and also the stopping statement. I can even get the program to tell me the sales tax and shipping "on an individual input".

The instructor wants to be able to input several figures and when the stopping statement is triggered, have the program show the total shipping cost and sales tax for all of the inputs together.

I have placed the math parts between the two input statements, and also outside of the loop.

My question is: "Do I need to put the math section into a method and if so where would I place the calling statement so that it would only work after the stop statement is triggered or would something else be even more appropriate?"

I have been beating my head against the wall for the last 10 days, and have not figured out an answer. I have tried everything I can think of.

Can someone either please answer the question or at least steer me to the information on how to do this type of a problem. I would appreciate it greatly.

This is the main body of my sentinel loop program. I can get it to work fine.

using System;
using System.Collections.Generic;
using System.Text;

namespace Exer4Chpt6
{
    class Program
    {
        static void Main()
        {
            string inValue = "";
            double salesAmount = 0;
            double salesTax;
            double shipAmount = 0;
            double grdTotal;
            const double INTEREST_RATE = .07;
            double overAllTotal = 0;
            double totalShippingAmt = 0;
            double totalSalesTax = 0;


            Console.WriteLine("Enter as many different sales amounts ");
            Console.WriteLine("as you want. You will be shown the sales ");
            Console.WriteLine("amount, sales tax, shipping amount, and ");
            Console.WriteLine("the grand total. To Stop, enter");
            Console.WriteLine(" -99");
            Console.WriteLine();

            Console.WriteLine("Please type in a sales amount.");
            inValue = Console.ReadLine(); // Priming read, input before loop
            while (inValue != "-99")
            {
              salesAmount = Convert.ToDouble(inValue);
              salesTax = salesAmount * INTEREST_RATE;

                Console.WriteLine("Please type in a sales amount.");
                inValue = Console.ReadLine(); // reassigns data in inValue to check in loop
            }
        }
    }
}

This is the math section of my program that I'm having the trouble with. The problem isn't in the math expressions themselves, but more in the placement to get it to do what I want

if (salesAmount < 250.01)
{
 shipAmount = 5.0;
}
else if (salesAmount < 500.01)
{
 shipAmount = 8.0;
}
else if (salesAmount < 1000.01)
{
 shipAmount = 10.0;
}
else if (salesAmount <= 5000.00)
{
 shipAmount = 15.0;
}
else if (salesAmount > 5000.00)
{
 shipAmount = 20.0;
}
else
  Console.WriteLine("Please type in a sales amount.");

                grdTotal = salesAmount + shipAmount + salesTax;
                overAllTotal += grdTotal;
                totalShippingAmt += shipAmount;
                totalSalesTax += salesAmount;
Console.WriteLine();
Console.WriteLine("Your final sales amount was: {0:C2}", grdTotal);
Console.WriteLine();
Console.WriteLine("Your sales tax amount was: {0:C2}", totalSalesTax);
Console.WriteLine();
Console.WriteLine("Your shipping amount was: {0:C2}", totalShippingAmt);
Console.WriteLine();
Console.WriteLine("Your total bill is: {0:C2} ", overAllTotal);
Console.WriteLine();

Any help will be very greatly welcome. Thank you.

 
Old April 12th, 2007, 08:50 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Strictly speaking you don't have to put code into another method. It's handy to do so if you plan to reuse it, or simply for compartmentalizing the code to keep it more readable and better organized.

I hesitate to answer your question completely, after all you are the student and should be solving the problem on your own. All the code looks right, you just need to place the last past. Where do you think the while loop ends?

-Peter
 
Old April 12th, 2007, 10:14 AM
Registered User
 
Join Date: Apr 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I was able to recheck on these type of loops and found a solution. By placing the math section after the final input I've got it to work correctly. I placed the math section in the spot shown below.

              salesAmount = Convert.ToDouble(inValue);
              salesTax = salesAmount * INTEREST_RATE;

                Console.WriteLine("Please type in a sales amount.");
                inValue = Console.ReadLine(); // reassigns data in inValue to
                                              //check in loop
            }
               //START math section in this area.
        }
Thank you for your answer. Although I didn't get the answer I was looking for, in a strange way it did help get me going in the right direction.

 
Old April 12th, 2007, 01:19 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

It looks like you answered it correctly.

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop question??? RinoDM Javascript 1 May 13th, 2008 07:35 PM
Question on do while loop savoym Java Basics 2 May 3rd, 2007 05:56 AM
Do While Not Loop Question hcweb Classic ASP Basics 3 January 12th, 2007 01:27 PM
simple loop question Adam H-W Classic ASP Basics 2 July 9th, 2004 01:22 AM
Get unlimited input from user WO using sentinel # frank_jarle J2EE 4 July 9th, 2003 03:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.