Your existing code is written really weird - was this a template given to you?
it looks like you already have an exit, which is typing "y", but when your count gets to 10 it doesn't ever exit the loop so it keeps asking for more names after displaying the count.
here is a change to fix that one issue -
class Students
{
static void Main()
{
Students s = new Students();
s.Run();
}
void Run()
{
string done ="y";
int count = 0;
int totcount;
string [] stuNames = new string[9];
while (done != "Y" && count < 10) // Exit if count == 10
{
Console.WriteLine("Please enter student names");
stuNames[count++] = Console.ReadLine();
totcount = count + 1;
done = Console.ReadLine();
}
// Write your result after you leave the loop
Console.WriteLine("Total Students:" + totcount);
}
}
but here is a cleaner approach, though you would probably want to just pop this code into your entry point rather than a class - and Students doesn't quite properly describe what this class is.:
class Students
{
// If you have an entry point somewhere then that is
// where you need to create this class and run it - don't put
// a static method on a class that constructs and then runs
// itself
// Declare your constants out here:
private const string INPUTCOMPLETE = "exit";
void Run()
{
//Use a 'List' when you are doing a variable length
//item entry function like this
var students = new List<string>();
var input = ""; // Use only 1 input variable
//Check the count of items in your list
while (students.Count < 10)
{
Console.WriteLine("Please type student name or (" + INPUTCOMPLETE + ") to exit.");
input = Console.ReadLine();
//Make your exit here
if (input == INPUTCOMPLETE) break;
//Otherwise add the input to your list
students.Add(input);
}
// Write your result after you leave the loop
Console.WriteLine("Total Students:" + students.Count);
}
}
></\/~Psightoplasm`~