Here is the code

static void Main(string[] args)
    {
        int qty, prodNum;
        double totalRetail;

        Console.WriteLine("Enter product number 1, 2, or 3. ");
        prodNum = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Enter in quantity sold of this product or enter -1 to quit: ");
        qty = Convert.ToInt32(Console.ReadLine());
        totalRetail = 0;
        while (qty != -1)
        {
            switch (prodNum) 
            {
                case 1 : 
                totalRetail = 2.98 * qty;
                break;
                case 2:
                totalRetail = 4.50 * qty;
                break;
                case 3:
                totalRetail = 9.98 * qty;
                break;


            }
            Console.WriteLine("Total Sales:  {0:C}", totalRetail);          
        }

    }
}
}

It doesn't just output the total what am I missing.

Thank you

Recommended Answers

All 3 Replies

It loops because the while loop says 'Continue UNTIL the user enters -1.' But you never give the user a chance to enter -1 after the loop has begun. Try this.

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

namespace daniweb
{
    class Program
    {
        static void Main(string[] args)
        {

            int qty, prodNum;
            double totalRetail;

            Console.WriteLine("Enter product number 1, 2, or 3. ");
            prodNum = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter in quantity sold of this product or enter -1 to quit: ");
            qty = Convert.ToInt32(Console.ReadLine());
            totalRetail = 0;
            if (qty != -1)
            {
                switch (prodNum)
                {
                    case 1:
                        totalRetail = 2.98 * qty;
                        break;
                    case 2:
                        totalRetail = 4.50 * qty;
                        break;
                    case 3:
                        totalRetail = 9.98 * qty;
                        break;
                }
                Console.Write("Total Sales: {0:C}", totalRetail);
            }
                
            
            Console.ReadLine();
            
        }   
        
    }
}

P.S. Next time use the provided code tags. It makes things easier to read.

Sorry papanyquiL, apparently I should reload my tabs more often, I had this tab open an hour before I got around to answering, lol, I guess I am over multitasking...

Below is the almost identical answer as above lol, and daniweb doesn't have a delete button, just an edit.

You have a while loop going, its looking for qty to be -1. since your loop never changes the value of qty. it is allays whatever it was set to during read-line, which happens before the loop is created.

since qty is never set to -1 its just going to loop forever.

obviously you just want to check first if qty is -1. so you are better off just using an if statement.

static void Main(string[] args)
{
int qty, prodNum;
double totalRetail;

Console.WriteLine("Enter product number 1, 2, or 3. ");
prodNum = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter in quantity sold of this product or enter -1 to quit: ");
qty = Convert.ToInt32(Console.ReadLine());
totalRetail = 0;
if(qty == -1)
{
Console.WriteLine("Exiting app...");
return;
}

switch (prodNum)
{
case 1 :
totalRetail = 2.98 * qty;
break;
case 2:
totalRetail = 4.50 * qty;
break;
case 3:
totalRetail = 9.98 * qty;
break;

Console.WriteLine("Total Sales: {0:C}", totalRetail);
}

}
}
}

try this instead.

That's funny :D I do that all the time!

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.