How to write a console application in Visual C# Studio 2008
To develop a console application in the environment Visual C # Studio 2008 in accordance with the following task: There is an array of (up to 5 array) of the records with two fields: "Employee Name" (type string) and "Age" (type int) in the program.
When you first start the program, on the screen will be showed the following menu:
Number of employees: NO DATA
1 - Adding an employee
2 - Display information about all employees
4 - Exit
As an user you can add information about the new employee. Once again it appears the menu:
Number of employees: 1
1 - Adding an employee
2 - Display information about all employees
4 - Exit
After pressing the 2 key (with number 2) the program will display information about all employees at the moment and again displaye the menu.
We must use a separate function to display information about all employees
Pressing the button 4 is out of the application.
After pressing the button (except 1,2,4)on the screen will be showed the message "Invalid Key Pressed".
Checking the pressed key is performed by the operator if.
The loop in the To develop a console application in the environment Visual C # Studio 2008 in accordance with the following task: Program features an array of (up to 5 array) of the records with two fields: "Employee Name" (type string) and "Age" (type int).
When you first start the program prints the on-screen menu:
Number of employees: NO DATA
1 - Adding an employee
2 - Display information for all employees
4 - Output
Pressing a user can add information about the new employee. Once again it appears the menu:
Number of employees: 1
1 - Adding an employee
2 - Display information for all employees
4 - Output
Pressing the 2 key displays information about all employees and again displayed menu.
The output is in a separate function.
Pressing the button 4 is out of the application.
Pressing except 1,2,4 is the message "Invalid Key Pressed".
Checking the pressed key is performed by the operator if.
The loop in the main program must be do () while ()
Defining the key pressed: function getch ()
Following the development of applications, transfer function developed in a separate module. : do () while ()
Defining the key pressed: function getch ()
After developing the console application, transfer functions developed in a separate module.
My solution is that:
[code=csh]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lab1_C_sharp_
{
struct TWorker
{
public string sName;
public int iAge;
}
static public void ShowInfWork(TWorker []arr, int n)
{
int count=0;
for(count=0; count<n; count++)
Console.Out.WriteLine("Worker: {0}, {1} years old", arr[count].sName,arr[count].iAge);
}
class Program
{
static void Main(string[] args)
{
TWorker[] arWork = new TWorker[5];
int iNum = 0;
char key;
bool bDone = false;
while ((!bDone))
{
Console.Clear();
Console.Out.WriteLine("Number of workers: ");
if (iNum == 0)
Console.Out.WriteLine("No Data");
else
Console.Out.WriteLine(iNum);
Console.Out.WriteLine(" 1 - Add worker");
Console.Out.WriteLine(" 2 - Show information about all workers");
Console.Out.WriteLine(" 4 - Exit");
key = Console.ReadKey(true).KeyChar;
switch (key)
{
case '4': bDone = true; break;
case '1':
if (iNum < 4)
{
Console.Out.WriteLine("Enter worker name: ");
arWork[iNum].sName = Console.In.ReadLine();
arWork[iNum].iAge = int.Parse(Console.In.ReadLine());
}
else
Console.Out.WriteLine("You can not enter more data");
iNum++;
break;
case '2':
Console.Out.WriteLine("Information about all workers: ");
ShowInfWork(arWork, iNum);
key = Console.ReadKey(true).KeyChar;
break;
}
}
}
}
}
[/code]
After compiling I got some messages: "Error 3 Expected class, delegate, enum, interface, or struct" и "Error 2 Identifier expected"
Please correct my mistakes.
Thanks in advanced.
Re: How to write a console application in Visual C# Studio 2008
Re: How to write a console application in Visual C# Studio 2008
Hi !
At first please read forum rules and correct your starting code Tag and reformat it, so it is forammted and could easily be read.
Without correct code Tags code looses format and looks like yours now.
The second point is, we dont do homework. This is also seen in the forum rules.
So please sit down and if your code has runtime errors then simple set breakpoints and run it that way. This is a good way to learn debugging. If you have compile tome errors so your program even doesn't start ( and I think thats your trouble ) then click to the message and you will see which line is involved in your problem. :wave:
As much as I can see you have doen a static method called ShowInfWork(...) outside a class. Methods can only be art of a class never in a namespace in C#
This maybe will help a lot, when I'm looking to your code on a short glance