C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2720
Number of posts: 5746

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Help, Student Names Crisis, Posted by Opticknerve on 19 May 2009 at 9:54 PM
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);
}
}
}
}
Report
Re: Help, Student Names Crisis, Posted by Psightoplazm on 20 May 2009 at 10:44 AM
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`~



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.