Sign in to follow this  

Problem with my array

This topic is 3592 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've pretty much got my program working, with one minor flaw. My program takes strings and puts them into ascending order, or alphabetically. In this case, my program has the user input names, last name,firstname. It works fine, but when the user types "quit" to end the while loop, the string "quit" is listed in the order. I tried using an if statement in the while loop so that the input strings are stored if the input is not equal to "quit". Here is what I have, is there something I am doing wrong? using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace Namelist Ascending { class Namelist { static void Main(string[] args) { string [] nameArray = new string [200]; int i = 0; Console.WriteLine("Enter in a name (LastName,FirstName), <type quit to exit>"); if (nameArray[i] != "quit") { nameArray[i] = Console.ReadLine(); } while (nameArray[i] != "quit") { Console.WriteLine("Enter in a name (LastName,FirstName), <type quit to exit>"); i++; if (nameArray[i] != "quit") { nameArray[i] = Console.ReadLine(); } } Console.Clear(); Array.Sort(nameArray); foreach (string inValue in nameArray) { Console.Write(inValue + "\n"); } Console.Read(); } } }

Share this post


Link to post
Share on other sites
NameArray[i] will never contain quit, you increment the counter before you check, and you never put the value into the name array unless it's not quit, which means you'll always put the value into the array...


while (nameArray[i] != "quit")
{
Console.WriteLine("Enter in a name (LastName,FirstName), <type quit to exit>");
i++;
if (nameArray[i] != "quit")
{
nameArray[i] = Console.ReadLine();
}

}




Should be;


std::string nameInput;

while (nameArray[i] != "quit")
{
Console.WriteLine("Enter in a name (LastName,FirstName), <type quit to exit>");

nameInput = Console.ReadLine( );

if( nameInput != "quit")
{
nameArray[ i++ ] = nameInput;

}

}




note: My C++ is rusty at best, but I do believe that should be the idea..

Share this post


Link to post
Share on other sites
Quote:
Original post by hammerstein_02
NameArray[i] will never contain quit, you increment the counter before you check, and you never put the value into the name array unless it's not quit, which means you'll always put the value into the array...

*** Source Snippet Removed ***

Should be;

while (nameArray[i] != "quit")

This approach suffers from a similar problem -- since "quit" will never make its way into the array, it'll never leave this loop. The while loop should be checking for nameInput to be "quit".
string nameInput = Console.ReadLine();

while (nameInput != "quit")
{
// figure out the good parts on your own, you're well on your way.
// if you need a nudge, though, let us know.
}

Share this post


Link to post
Share on other sites
Quote:
Original post by hammerstein_02
note: My C++ is rusty at best, but I do believe that should be the idea..


Considering we're discussing C# here... [grin]

Anyway, to the OP: it's much easier than that.

Step 1: Don't do something once, and then repeatedly do it "while" something. That's what do-while is for.

Step 2: Let the "quit" end up in the container, and just *remove* it afterward. You know where it is; it's the last element you read. You know it's there.

Step 3: You're "using System.Collections", so why not actually *use them*? :)


using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Namelist Ascending {
class Namelist {
static void Main(string[] args) {
ArrayList names = new ArrayList();
do {
Console.WriteLine("Enter in a name (LastName, FirstName), <type quit to exit>");
names.Add(Console.ReadLine());
} while (names[names.Count - 1] != "quit");
names.removeAt(names.Count - 1);
Console.Clear();
names.Sort();
foreach (string name in names) {
Console.WriteLine(name);
}
// Don't artificially pause your programs at the end!
}
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by jouley
Quote:
Original post by hammerstein_02
NameArray[i] will never contain quit, you increment the counter before you check, and you never put the value into the name array unless it's not quit, which means you'll always put the value into the array...

*** Source Snippet Removed ***

Should be;

while (nameArray[i] != "quit")

This approach suffers from a similar problem -- since "quit" will never make its way into the array, it'll never leave this loop. The while loop should be checking for nameInput to be "quit".
*** Source Snippet Removed ***


Haha.. Whoops. Completely overlooked that, my apologies. Rusty C++ aside..

As for not noticing C#, apologies to OP for adding a completely worthless response.

Thanks for the correction guys, feel a little embarrassed now. :o

[Edited by - hammerstein_02 on February 13, 2008 5:40:29 AM]

Share this post


Link to post
Share on other sites

This topic is 3592 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this