Reputation: 231
How do I make this program continue running after the user selects an option? Here is the code I have so far.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace calculator_extended
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press A for addition");
Console.WriteLine("Press S for subtraction");
Console.WriteLine("Press M for Multiplication");
Console.WriteLine("Press D for Divide");
char c = Convert.ToChar(Console.ReadLine());
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
switch (c)
{
case 'A':
case 'a':
{
int d = add(a, b);
Console.WriteLine(d);
break;
}
case 'S':
case 's':
{
int d = sub(a, b);
Console.WriteLine(d);
break;
}
case 'M':
case 'm':
{
int d = mul(a, b);
Console.WriteLine(d);
break;
}
case 'E':
case 'e':
{
int d = div(a, b);
Console.WriteLine(d);
break;
}
default:
{
Console.WriteLine("Please Enter the correct Character");
break;
}
}
}
private static int add(int a, int b)
{
return a + b;
}
private static int sub(int a, int b)
{
return a - b;
}
private static int mul(int a, int b)
{
return a * b;
}
private static int div(int a, int b)
{
return a / b;
}
}
}
Upvotes: 0
Views: 9117
Reputation: 25619
Wrap your code with
while(true) {
// Your code here
// Don't forget to add a switch case for an Exit operation
case 'q':
Application.Exit();
}
If you're new to programming, you should look into Loops - See Listing 4-2 - it's a good example of what you're trying to accomplish.
EDIT: I realize you're new to programming and I do think you should accomplish this by your own. As it's a basic problem, here's a full solution
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace calculator_extended
{
class Program
{
static void Main(string[] args)
{
int d = 0;
while (true)
{
Console.WriteLine("Press A for addition");
Console.WriteLine("Press S for subtraction");
Console.WriteLine("Press M for Multiplication");
Console.WriteLine("Press D for Divide");
char c = Convert.ToChar(Console.ReadLine());
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
switch (c)
{
case 'A':
case 'a':
d = add(a, b);
Console.WriteLine(d);
break;
case 'S':
case 's':
d = sub(a, b);
Console.WriteLine(d);
break;
case 'M':
case 'm':
d = mul(a, b);
Console.WriteLine(d);
break;
case 'E':
case 'e':
d = div(a, b);
Console.WriteLine(d);
break;
case 'q':
case 'Q':
Application.Exit();
default:
Console.WriteLine("Please Enter the correct Character");
break;
}
}
}
private static int add(int a, int b)
{
return a + b;
}
private static int sub(int a, int b)
{
return a - b;
}
private static int mul(int a, int b)
{
return a * b;
}
private static int div(int a, int b)
{
return a / b;
}
}
}
Upvotes: 6
Reputation: 99
First of all, for making the program continue, you have to wrap it into a while(true) loop, or do{}while(some key is pressed) loop. You will have to add a fifth option to your list of options anyway ^^ (which will be the exit option)
I believe that for solving your problem, you have two options:
Using the while(true) loop
All of your code in the main() method will be wrapped in a while(true) loop. After you have done that, you will read the exit key, just like any other of the keys (A,S,M,D), and send it to the proper exit method. This 'exit method' will be created outside of your main function. When done, it should look something like this:
static void Main(string args[])
{
Console.WriteLine("Press A for addition");
Console.WriteLine("Press S for subtraction");
Console.WriteLine("Press M for Multiplication");
Console.WriteLine("Press D for Divide");
Console.WriteLine("Press X to exit");
//... all the stuff up to the switch
switch(c)
{
//all the cases from before
case 'x' : exitMethod();break;
}
}
private static void exitMethod()
{
Application.Exit();
}
Using the do - while loop
All of your code in the main() method will be wrapped in a do - while loop. Yes, it is similar to using the while(true) loop, but you will have to change the statement in the while this time. It will be like this:
do { // here is all the code in main()
}while(Console.ReadKey(true).Key != ConsoleKey.X);
And this is it with the do - while loop. You don't need to call a special exit method, because the program will just jump out of the do while loop, and close itself like it does now (normally).
To be honest, I would rather go with the do - while loop because it can also handle a combination of keys. For more into that please visit this link: http://www.dotnetperls.com/console-readkey
Upvotes: 1
Reputation: 1380
you can use any of technique of iteration by goto command with function calling by unsatisfactory while or do while loop , by infinte for loop
Upvotes: 0
Reputation: 4081
Put your code into the do block
char c = '';
do
{
c = ...
}
while (c != 'q');
Upvotes: 0
Reputation: 6020
You need to look into the use of while loops, this is what you'd use here to get your program to keep looping until certain conditions have been met i.e. a calculation has been chosen then a first value has been entered, and then a second. Something like:
static void Main(string[] args)
{
// main loop to keep your program alive
while (true)
{
// handle your program logic
}
}
Upvotes: 0
Reputation: 485
This can be solved with a loop. I would usually use a Do...While loop for this kind of Console application.
You're going to encounter these things while learning... I suggest you keep following whatever guided learning course you're doing before attempting to write code you don't yet have the skills to complete.
Upvotes: 0