tags:

views:

46

answers:

3

If the user enters A it works then they enter B it works and it works if they enter E it also works but if they then enter A to start over the program closes.

const double COMMRATE = 0.10;

const double COMMRATE = 0.10; 

string inputstring; 

char response; 
Console.Write("Do you want to Calculate Sales A or B or E..."); 
inputstring = Console.ReadLine(); 
response = Convert.ToChar(inputstring);

    while (response == 'A')
     {
    Console.WriteLine("Enter amount of sales");
    string salesStr = Console.ReadLine();
    Console.WriteLine(Double.Parse(salesStr) * COMMRATE);
    Console.WriteLine("Enter sales member to continue or Z to exit");
    response = Convert.ToChar(Console.ReadLine());
     }
    while (response == 'B')
    {
        Console.WriteLine("Enter amount of sales");
        string salesStr = Console.ReadLine();
        Console.WriteLine(Double.Parse(salesStr) * COMMRATE);
        Console.WriteLine("Enter sales member to continue or Z to exit");
        response = Convert.ToChar(Console.ReadLine());
    }
    while (response == 'E')
     {
         Console.WriteLine("Enter amount of sales");
    string salesStr = Console.ReadLine();
    Console.WriteLine(Double.Parse(salesStr) * COMMRATE);
    Console.WriteLine("Enter sales member to continue or Z to exit");
    response = Convert.ToChar(Console.ReadLine());
       }
+3  A: 

This is the same situation as your other question: http://stackoverflow.com/questions/3672829/do-loops-and-while-loops

You'll want to use an if statement or a switch statement to parse the user's input:

if (response == 'A')
{
    Console.WriteLine("Enter amount of sales");
    string salesStr = Console.ReadLine();
    Console.WriteLine(Double.Parse(salesStr) * COMMRATE);
    Console.WriteLine("Enter sales member to continue or Z to exit");
    response = Convert.ToChar(Console.ReadLine());
}

if (response == 'B')
{
    Console.WriteLine("Enter amount of sales");
    string salesStr = Console.ReadLine();
    Console.WriteLine(Double.Parse(salesStr) * COMMRATE);
    Console.WriteLine("Enter sales member to continue or Z to exit");
    response = Convert.ToChar(Console.ReadLine());
}

if (response == 'E')
{
    Console.WriteLine("Enter amount of sales");
    string salesStr = Console.ReadLine();
    Console.WriteLine(Double.Parse(salesStr) * COMMRATE);
    Console.WriteLine("Enter sales member to continue or Z to exit");
    response = Convert.ToChar(Console.ReadLine());
}

Also, you're declaring the string salesStr inside the scope of your while (or in my example, the if). You won't be able to access it anywhere else. Since you're repeating the same logic in each block, but with a different value, I would be inclined to move the repeating code outside it, and only put it in there once. This way you only need to change one place in the future if the requirements change:

string salesStr;

if (response == 'A')
{
    Console.WriteLine("Enter amount of sales");
    salesStr = Console.ReadLine();
}

if (response == 'B')
{
    Console.WriteLine("Enter amount of sales");
    salesStr = Console.ReadLine();
}

if (response == 'E')
{
    Console.WriteLine("Enter amount of sales");
    salesStr = Console.ReadLine();
}

Console.WriteLine(Double.Parse(salesStr) * COMMRATE);
Console.WriteLine("Enter sales member to continue or Z to exit");
response = Convert.ToChar(Console.ReadLine());

Since each of these if statements is doing the same thing, you can combine them too:

string salesStr;

if (response == 'A' || response == 'B' || response == 'E')
{
    Console.WriteLine("Enter amount of sales");
    salesStr = Console.ReadLine();
}

Console.WriteLine(Double.Parse(salesStr) * COMMRATE);
Console.WriteLine("Enter sales member to continue or Z to exit");
response = Convert.ToChar(Console.ReadLine());
Michael Shimmins
I have the same problem. I can start at A and goto B then E, but when I try to hit A again the program closes.
randywhite30
@randywhite30 - check out Val's answer above, it incorporates the loop to keep the program running until you hit Z.
Michael Shimmins
@Michael +1 for the included lesson plan. @randy You're not looping your program, you're using the loops like if statements.
Val
+1  A: 

Look at the order your program flows in: first, it checks for As, and then it checks for Bs, and then Es, and then it exits.

It looks like you're not doing anything important with the ID of the salesperson yet, although I assume you will later.

Instead of three separate loops, you need one big loop that continues as long as the response isn't Z, and then uses a set of if(...) statements internally, or perhaps even a switch statement, to decide what to do with the ID of the salesperson.

Kistaro Windrider
+4  A: 

Because your code only loops per response type. It iterates while the response is A, then while B, then while E. Meaning that it can only operate in that sequence

Maybe you should look at something like:

do
{
    Console.WriteLine("EnterNextCommand");
    inputstring = Console.ReadLine();
    response = Convert.ToChar(inputstring);
    switch (response)
    {
        case 'A':
        case 'a':
        //case A logic
        break;
        case 'B':
        case 'b':
        //case B logic
        break;
        //etc.
        default:
        //they enter something you're not handling
        break;
    }

}
while (inputstring != "Z")
Val