tags:

views:

90

answers:

3
class Class1
 {

  [STAThread]
  static void Main(string[] args)
  {
   string userName;
            int i = 0, totalCal = 0, cal = 1;

   Console.WriteLine("Welcome to the magical calorie counter!");
   Console.WriteLine();
   Console.Write("Enter in your name -> ");
   userName = Console.ReadLine();

       for (i = 0; i <= 10; i++)
       {
      Console.WriteLine("Hello {0}.....Let's add some calories!!", userName);
    }// end for loop
   Console.WriteLine();


    while (cal != 0)
    {

     Console.Write("Enter some Calories:<or 0 to quit>: ");
     cal = Convert.ToInt32(Console.ReadLine());
     Console.WriteLine("You entered {0}", cal);
     Console.WriteLine();

     totalCal += cal;
     Console.WriteLine();
     Console.WriteLine("-------------------So far you have a total of {0}------------------", totalCal);
     Console.WriteLine();    
     Console.WriteLine();
    }// end of while 



  }// end of amin
 }//end of class
}// end of namespace

I'd like to be able to terminate the program whenever the user enters any of "q", "Q", "quit", or "QUIT". How do i achieve this?

A: 

I would prefer a keyboard shortcut a keyboard savvy user may already know, like Ctrl+W, which is the default key for closing tabs on modern web browsers.

Rafael Belliard
And i'd prefer that a console app act like a console app, not Firefox or Chrome or whatever. Keyboard shortcuts aren't just a pain to type and remember; they just don't make sense in a program like this, and add complexity to the reading of input.
cHao
A: 

I think exit, or quit is fine. Or you can accept e or q.

Google
+1  A: 

Assuming there aren't any other bugs in your program... just add a couple lines like this:

using System;

class Class1
{

    [STAThread]
    static void Main(string[] args)
    {
        string userName;
        string line;
        int i = 0, totalCal = 0, cal = 1;

        Console.WriteLine("Welcome to the magical calorie counter!");
        Console.WriteLine();
        Console.Write("Enter in your name -> ");
        userName = Console.ReadLine();

        for (i = 0; i <= 10; i++)
        {
            Console.WriteLine("Hello {0}.....Let's add some calories!!", userName);
        }// end for loop
        Console.WriteLine();


        while (true)
        {

            Console.Write("Enter some Calories:<or 0 to quit>: ");
            line = Console.ReadLine().ToLower();
            if (line == "q" || line == "quit")
            {
                break;
            }
            else if (!int.TryParse(line, cal))
            {
                Console.WriteLine("Not a valid option. Please try again.");
                continue;
            }
            Console.WriteLine("You entered {0}", cal);
            Console.WriteLine();

            totalCal += cal;
            Console.WriteLine();
            Console.WriteLine("-------------------So far you have a total of {0}------------------", totalCal);
            Console.WriteLine();
            Console.WriteLine();
        }// end of while 
    }// end of amin
}
Mark
ahhhh, wrong loop to start with, but i see how that works. Thanks Mark.
Chris
then make it `while(true)` instead. +1 or a checkmark would be nice.
Mark
mark, i need 15 rep to give you a check mark, anything other way i could do it?
Chris
@Chris: Really? Thats kind of dumb... well I just gave you 10 pts, so.. I guess you'll have to wait for the other 2 :p Fill out your profile to get 100 I think.
Mark