Thread: Loop issues.

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    5

    Loop issues.

    My assignment:
    Write a program that asks the user for a salesperson's initial. While the user does not type 'Z', continue by prompting for the amount of a sale the salesperson made. Calculate the salesperson's commission as 10% of the sale amount and add the commission to a running total for that salesperson. After the user types 'Z' for an initial, display each salesperson's total commission earned.

    Here's what I have so far. I cannot get my results to print after entering 'Z'. What am I doing wrong?

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace TubSales
    {
        class Program
        {
            static void Main(string[] args)
            {
                String initial;
                double sales;
                double com = 0.10;
                double totalSalesA = 0;
                double totalSalesB = 0;
                double totalSalesE = 0;
    
                Console.WriteLine("Enter Sales Persons First Intial: ");
                initial = Console.ReadLine().ToUpper();
                Console.WriteLine("Enter Sales amount: ");
                sales = Convert.ToDouble(Console.ReadLine());
    
                while (initial != "Z")
                {
                    if (initial == "A")
                    {
                        totalSalesA = totalSalesA + sales;
                    }
    
                    if (initial == "B")
                    {
                        totalSalesB = totalSalesB + sales;
                    }
    
                    if (initial == "E")
                    {
                        totalSalesE = totalSalesE + sales;
                    }
    
                    Console.WriteLine("Enter Sales Person's First Intial: ");
                    initial = Console.ReadLine().ToUpper();
                    Console.WriteLine("Enter Sales amount: ");
                    sales = Convert.ToDouble(Console.ReadLine());
                }
    
                if (initial == "Z")
                {
                    Console.WriteLine("Commission for Andrea is: {0}", totalSalesA * com);
                    Console.WriteLine("Commission for Brittany is: {0}", totalSalesB * com);
                    Console.WriteLine("Commission for Eric is: {0}", totalSalesE * com);
                }
            }
        }
    }

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    After you enter 'Z', does the console window close? I'm guessing that the output is being written to the window, but the window is closing before you can see it. Try putting a Console.ReadLine() at the end of your program to keep the window open until you hit Enter.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  2. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. Newb needs help with loop issues
    By FatalError in forum C Programming
    Replies: 15
    Last Post: 09-04-2004, 02:19 PM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM