Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Help, Student Names Crisis,

OpticknerveOpticknerve Posts: 23Member
How do i get the program to ask the user to input 10 students names and then after each entery the user must be able to exit, After exiting the program should print the total number of students.


using System;
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")
{
Console.WriteLine("Please enter student names");
stuNames[count++] = Console.ReadLine();

totcount = count + 1;
done = Console.ReadLine();

if (totcount == 10)
{
Console.WriteLine("Total Students:",+ totcount);
}
}
}
}

Comments

  • PsightoplazmPsightoplazm Posts: 332Member ✭✭
    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 -
    [code]
    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);
    }
    }

    [/code]

    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.:
    [code]
    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();

    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);
    }
    }

    [/code]
    ><//~Psightoplasm`~
Sign In or Register to comment.