I have been working on this one assignment for two weeks now and I can't figure it out. I have seen some things on here that mention using classes and methods but in the assignment instructions it says not to use them since we haven't learned it yet.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Assignment_2
{
class Program
{
static void Main(string[] args)
{
Write("Enter a salesperson's name: ");
string name = ReadLine();
do
{
Write("\n" "Enter an item number between 1 and 4 or -1 to quit: ");
String item = ReadLine();
int intItem = Convert.ToInt32(item);
Write("\n" "Enter the quantity sold: ");
String quantity = ReadLine();
int intQuantity = Convert.ToInt32(quantity);
switch (intItem)
{
case 1:
{
Double price = 239.99;
double total = price * intQuantity;
WriteLine("Salesperson " name " sold " intQuantity " of item #" intItem " at $" total);
double dblTotalSales = total;
break;
}
case 2:
{
Double price = 129.75;
double total = price * intQuantity;
WriteLine("Salesperson " name " sold " intQuantity " of item #" intItem " at $" total);
double dblTotalSales = total;
break;
}
case 3:
{
Double price = 99.95;
double total = price * intQuantity;
WriteLine("Salesperson " name " sold " intQuantity " of item #" intItem " at $" total);
double dblTotalSales = total;
break;
}
case 4:
{
Double price = 350.89;
double total = price * intQuantity;
WriteLine("Salesperson " name " sold " intQuantity " of item #" intItem " at $" total);
double dblTotalSales = total;
break;
}
case -1:
{
WriteLine("Salesperson " name " sold a total of $" dblTotalSales);
break;
}
}
} while (true);
}
}
}
CodePudding user response:
Your problem is the variable dblTotalSales
.
You are declaring the variable as a new variable inside each case of your switch statement, which means it is scoped to ONLY the specific case where it is declared.
If you are wanting to make the variable accessible to all of the case's, the variable needs to be declared outside the scope of the switch statement:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Assignment_2
{
class Program
{
static void Main(string[] args)
{
Write("Enter a salesperson's name: ");
string name = ReadLine();
// declare here to be outside the scope of the loop,
// but accessible to everything inside the loop.
double dblTotalSales = 0;
do
{
Write("\n" "Enter an item number between 1 and 4 or -1 to quit: ");
String item = ReadLine();
int intItem = Convert.ToInt32(item);
Write("\n" "Enter the quantity sold: ");
String quantity = ReadLine();
int intQuantity = Convert.ToInt32(quantity);
switch (intItem)
{
case 1:
{
Double price = 239.99;
double total = price * intQuantity;
WriteLine("Salesperson " name " sold " intQuantity " of item #" intItem " at $" total);
// now just reference the previously declared variable
dblTotalSales = total;
break;
}
case 2:
{
Double price = 129.75;
double total = price * intQuantity;
WriteLine("Salesperson " name " sold " intQuantity " of item #" intItem " at $" total);
// now just reference the previously declared variable
dblTotalSales = total;
break;
}
case 3:
{
Double price = 99.95;
double total = price * intQuantity;
WriteLine("Salesperson " name " sold " intQuantity " of item #" intItem " at $" total);
// now just reference the previously declared variable
dblTotalSales = total;
break;
}
case 4:
{
Double price = 350.89;
double total = price * intQuantity;
WriteLine("Salesperson " name " sold " intQuantity " of item #" intItem " at $" total);
// now just reference the previously declared variable
dblTotalSales = total;
break;
}
case -1:
{
WriteLine("Salesperson " name " sold a total of $" dblTotalSales);
break;
}
}
} while (true);
}