Server Help

Trash Talk - problem with c#

hellzlaker - Sun Apr 20, 2008 7:17 pm
Post subject: problem with c#
i decided to learn C# and i am write now writing this little APP
Code: Show/Hide
using System;

class wanked
{
    static void Main()
    {
        string choice;
        string[] names;
        int[] ages;

        do
        {
            Console.Clear();
            Console.WriteLine("\tMenu\n\nA - add a name\nB - add age for a name\nC - view data\nQ - Exit\n\n");
            Console.WriteLine("Enter your choice: ");
            choice = Console.ReadLine();

            switch (choice)
            {
                case "A":
                case "a":
                    {
                        int num;
                        string input;

                        Console.Clear();
                        Console.WriteLine("How many names would you like to add?: ");

                        input = Console.ReadLine();
                        num = Int32.Parse(input);

                        for (int x = 0; x < num; x++)
                        {
                            Console.Clear();
                            Console.WriteLine("Enter name for following IDs:\n");
                            Console.WriteLine("ID #{0}: ", x);
                            names[x] = Console.ReadLine();
                        }
                        Console.Clear();
                        Console.WriteLine("Enter name for following IDS:\n");
                        Console.WriteLine("ID #{0}: ", num);
                        names[num] = Console.ReadLine();


                    }
                    break;
                case "B":
                case "b":
                    break;
                case "C":
                case "c":
                    break;
                case "Q":
                case "q":
                    break;
                default:
                    Console.Clear();
                    Console.WriteLine("{0} is not a valid choice! Try again, press Enter Key to continue...",choice);
                    Console.ReadLine();
                    Console.Clear();
                    break;
            }

        } while (choice != "q" && choice != "Q");
    }
}


its doesnt compile saying this
Code: Show/Hide
Error   3   Use of unassigned local variable 'names'   C:\Documents and Settings\Owner\Local Settings\Application Data\Temporary Projects\as\CodeFile1.cs   37   29   as


i dont get it since i declared names in the begining and it only does it to names in for loop not the one outsite :S icon_confused.gif
tcsoccerman - Sun Apr 20, 2008 8:17 pm
Post subject:
string[] names = new string[size];

you have to use the new statement for arrays. look it up.
hellzlaker - Sun Apr 20, 2008 8:34 pm
Post subject:
i have another question how to run commands from DOS like in C++ its "system("cmd.exe d mrs.exe");"

so how do you do it in C#?
BDwinsAlt - Sun Apr 20, 2008 8:42 pm
Post subject:
I think there is probably a better forum for what you're doing since it isn't subspace related.
D1st0rt - Sun Apr 20, 2008 11:17 pm
Post subject:
The first thing that jumped out at me is that you don't need to (and shouldn't) be using all of those \n's. You should either split it up and call WriteLine for each one (it will do it for you which is actually why it is called WriteLine instead of just Write) or use Environment.NewLine.

You're not doing any validation of the number input, check out the TryParse method.

Also, you can use String.ToUpper to half the number of cases in your switch.

Running external things is done with System.Diagnostics.Process.Start
Smong - Mon Apr 21, 2008 4:11 am
Post subject:
Also...
Code: Show/Hide
                        for (int x = 0; x < num; x++)
                        {
                            Console.Clear();
                            Console.WriteLine("Enter name for following IDs:\n");
                            Console.WriteLine("ID #{0}: ", x);
                            names[x] = Console.ReadLine();
                        }
                        // don't need these 4 lines:
                        Console.Clear();
                        Console.WriteLine("Enter name for following IDS:\n");
                        Console.WriteLine("ID #{0}: ", num);
                        names[num] = Console.ReadLine();

Cheese - Sat Apr 26, 2008 1:03 am
Post subject:
-deleted, was crap-
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group