CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2011
    Posts
    1

    Newbie cant get this to work...

    Hi guys, i am trying to learn c# , i am following some tutorials and now i am trying to make a simple program but just wont work.
    It seems like the problem is that Console.ReadLine doesn't work , and i got no idea why...

    Can you guys help me?
    The error seems to be at viewDb()


    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    
    
    class ProgramMain
    {
        public static void Main()
        {
            DatabaseProgram db = new DatabaseProgram();
            db.menu();
        }
    }
    
     class DatabaseProgram
    {
        int count = 0;
        string test;
        struct database
        {
            public string name;
            public long number;
        }
    
        database[] db = new database[50];
        public void menu()
        {
            char choise = 'm';
            while (choise != 'Q')
            {
    
                Console.WriteLine("Menu");
                Console.WriteLine("A to add a new person into the database");
                Console.WriteLine("B to view the database");
                Console.WriteLine("Q to quit");
                choise = (char) Console.Read();
    
                switch (choise)
                {
                    case 'A':
                        addPerson(ref db[count].name, ref db[count].number,ref count);
                        break;
                    case 'B':
                        Console.WriteLine("Test");
                        Console.ReadLine();
                        break;
                    case 'Q':
                        choise = 'Q';
                        break;
                    default:
                        Console.WriteLine("It can be tricky to read the instructions sometimes :) ");
                        break;
    
    
                }
            }
    
        }
    
        public void viewDb()
        {
            for (int a = 0; a != count; a++)
            {
                Console.WriteLine("{0}: Name {1}  Sec-num: {2}", (a + 1), db[a].name, db[a].number);
            }
        }
        public void addPerson(ref string name, ref long number, ref int count)
        {
            Console.WriteLine("Enter the name: ");
            name = Console.ReadLine();
            Console.WriteLine("Enter the sec-num");
            db[count].number = Console.Read();
            count++;
        }
    
    }

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Newbie cant get this to work...

    You need to define "doesn't work". That said, I'm not sure how you ever expect this loop to execute:

    Code:
    char choise = 'm';
    while (choise != 'Q')
    {
        // ...
    }
    'choise' will never equal 'Q' at that point because you just assigned the value 'm' to it!

  3. #3
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Newbie cant get this to work...

    Quote Originally Posted by BigEd781 View Post
    You need to define "doesn't work". That said, I'm not sure how you ever expect this loop to execute:

    Code:
    char choise = 'm';
    while (choise != 'Q')
    {
        // ...
    }
    'choise' will never equal 'Q' at that point because you just assigned the value 'm' to it!
    Actually, that's precisely why it will execute... Been partying?

    @Eqric: BigEd781 is right about you being vague - when you post a question, make sure to describe the problem in more detail (what you expected, what actually happened, what errors/exceptions you've got, if any...)

  4. #4
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Newbie cant get this to work...

    @OP: Aah... Case sensitivity?

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Newbie cant get this to work...

    Errr, sorry, missed the != .

    Yeah, you'll have to define "doesn't work" for us. Have you stepped through the code in the debugger?

  6. #6
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    2

    Re: Newbie cant get this to work...

    Hi

    Not sure what you're intentions were, but I've done a quick scan and changed some things, it's commented what I changed, this should point you in the right direction.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    class ProgramMain
    {
        public static void Main()
        {
            DatabaseProgram db = new DatabaseProgram();
            db.menu();
        }
    }
    
    class DatabaseProgram
    {
        int count = 0;
        string test;
        struct database
        {
            public string name;
            public long number;
        }
    
        database[] db = new database[50];
        public void menu()
        {
            char choise = 'm';
            while (choise != 'Q')
            {
    
                Console.WriteLine("Menu");
                Console.WriteLine("A to add a new person into the database");
                Console.WriteLine("B to view the database");
                Console.WriteLine("Q to quit");
                choise = Char.ToUpper(Console.ReadKey().KeyChar); //need to make it uppercase
                                                                  //so upper/lower chars can be matched in the switch 
                switch (choise)
                {
                    case 'A':
                        //addPerson(db[count].name, db[count].number, count); //params not needed/used in this case
                        addPerson();
                        break;
                    case 'B':
                        viewDb();
                        //Console.WriteLine("Test");
                        //Console.ReadLine();
                        break;
                    case 'Q':
                        //choise = 'Q'; //not needed, choise already has this value
                        break;
                    default:
                        Console.WriteLine("It can be tricky to read the instructions sometimes :) ");
                        break;
    
    
                }
            }
    
        }
    
        public void viewDb()
        {
            for (int a = 0; a != count; a++)
            {
                Console.WriteLine("{0}: Name {1}  Sec-num: {2}", (a + 1), db[a].name, db[a].number);
            }
        }
        //public void addPerson(string name, long number, int count) //not needed/used in this case
        public void addPerson()
        {
            Console.WriteLine("Enter the name: ");
            db[count].name = Console.ReadLine();
            Console.WriteLine("Enter the sec-num");
            db[count].number = Convert.ToInt32(Console.ReadLine()); //need to use ReadLine here, and convert to int
            count++;
        }
    
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured