|
-
January 31st, 2011, 02:34 PM
#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++;
}
}
-
January 31st, 2011, 03:52 PM
#2
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!
-
January 31st, 2011, 04:21 PM
#3
Re: Newbie cant get this to work...
 Originally Posted by BigEd781
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...)
-
January 31st, 2011, 04:23 PM
#4
Re: Newbie cant get this to work...
@OP: Aah... Case sensitivity?
-
January 31st, 2011, 04:29 PM
#5
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?
-
January 31st, 2011, 04:37 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|