This is just a reminder that my question has not been answerd. I understand an answer was posted. But I believe there has not been any answer to the specific question. Please I want a complete help on this question so that next time, I will be able to write similar code in a perfect way.
Thanks.
Loading
VulpesPosted May 8, 2011, 11:37 AM
VulpesPosted May 8, 2011, 12:55 PM
while(true)
{
Console.Write("What fruit do you wish to search for ? ");
String name = Console.ReadLine();
if (name == "quit")
{
Console.WriteLine("\nEnd of the Fruit program");
return;
}
f.Search(name);
}
If you want the user to be able to end the program by typing either exit or quit, then change:
if (name == "quit")
to:
if (name == "exit" || name == "quit")
Caleb AdeyemoPosted May 8, 2011, 12:46 PM
Thanks.
*** Now, if I put exit or quit, how can I have an output---"End of the Fruit program" before press any key to continue or replacing press any key to continue?
Thanks again for your support.
Caleb AdeyemoPosted May 8, 2011, 12:15 PM
Caleb AdeyemoPosted May 8, 2011, 11:22 AM
The Fruit class contains the following unsorted fruits.
grape
orange
apple
plum
pear
The Fruit class contains the following sorted fruits.
apple
grape
orange
pear
plum
What fruit do you wish to searc for? (Supposing I input pineapple), the output will be:
The fruit pineapple is not in the list.
What fruit do you wish to search for? (Here again if I input grape), the output should be:
The fruit grape is in the list.
What Fruit do yo wish to search for? ( Here I will input exit), then the output will be:
End of fruit program.
My point again is that going by the help I got in putting the code together, in the output there are these two lines-- (The fruit pineaaple is not in the list. The fruit grape is in the list). These two statements should not have come up until after I input the fruit to search for when prompted for this. But rather they came up even before asking for the fruit to search for, and it still prompted or asks for the fruit to search for after the two statements have already came up. However, the program still runs fine except for those two statements that supposed not to appear before it asks for the fruit to search for. Please run the code and see what I am talking about.
Thanks.
VulpesPosted May 8, 2011, 7:15 AM
Caleb AdeyemoPosted May 4, 2011, 11:55 AM
Ankit NandekarPosted May 4, 2011, 2:03 AM
The fruit grapeis not in the list instead of this it has to give message like when we r trying to search pineapple which is not in list so it has to give message The fruit pineapple not in the list .
try this it will work
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] names = {"grape", "orange", "apple", "plum", "pear"};
Fruit f = new Fruit(names);
Console.WriteLine("The Fruit class contains the following unsorted fruits.");
f.Print();
f.Sort();
Console.WriteLine("The Fruit class contains the following sorted fruits.");
f.Print();
f.Search("pineapple");
f.Search("grape");
Console.WriteLine("Type quit to end the program at any time\n");
while (true)
{
Console.Write("What fruit do you wish to search for ?");
String name = Console.ReadLine();
if (name == "quit")
return ;
f.Search(name);
}
}
}
public class Fruit
{
string[] names;
public Fruit(string[] names)
{
this.names = names;
}
public void Print()
{
foreach (string name in names) Console.WriteLine("{0}",name);
// Console.Write();
Console.WriteLine();
}
public void Sort()
{
Array.Sort(names);
}
public void Search(string name)
{
if (Array.IndexOf(names, name) == -1)
{
Console.Write("The fruit {0} is not in the list",name );
}
else
{
Console.WriteLine("The fruit{0} is in the list", name);
}
Console.WriteLine();
}
}
}
You did mistake in Console.Write("The fruit {1}is not in the list",names ); instead of writing names u should write name there.
Caleb AdeyemoPosted May 4, 2011, 1:38 AM
The Fruit class contains the following unsorted fruits.
grape
orange
apple
plum
pear
The Fruit class contains the following sorted fruits.
apple
grape
orange
pear
plum
The fruit grapeis not in the list
The fruitgrape is in the list
Type quit to end the program at any time
What fruit do you wish to search for ?
Almost done! Pls help me to check it agai.
Thanks.
class Program
{
static void Main()
{
string[] names = {"grape", "orange", "apple", "plum", "pear"};
Fruit f = new Fruit(names);
Console.WriteLine("The Fruit class contains the following unsorted fruits.");
f.Print();
f.Sort();
Console.WriteLine("The Fruit class contains the following sorted fruits.");
f.Print();
f.Search("pineapple");
f.Search("grape");
Console.WriteLine("Type quit to end the program at any time\n");
while (true)
{
Console.Write("What fruit do you wish to search for ?");
String name = Console.ReadLine();
if (name == "quit")
return ;
f.Search(name);
}
}
}
public class Fruit
{
string[] names;
public Fruit(string[] names)
{
this.names = names;
}
public void Print()
{
foreach (string name in names) Console.WriteLine("{0}",name);
// Console.Write();
Console.WriteLine();
}
public void Sort()
{
Array.Sort(names);
}
public void Search(string name)
{
if (Array.IndexOf(names, name) == -1)
{
Console.Write("The fruit {1}is not in the list",names );
}
else
{
Console.WriteLine("The fruit{0} is in the list", name);
}
Console.WriteLine();
}
}
}
Caleb AdeyemoPosted May 3, 2011, 11:38 PM
The Fruit class contains the following unsorted fruits:grapeorangeappleplumpear
The Fruit class contains the following sorted fruits:applegrapeorangepearplum
The fruitpinappleis not in the list
The fruitgrape is in the list
Type quit to end the program at any time
What fruit do you wish to search for ?
Please kindly help to see where the problem is.
Thanks.
VulpesPosted May 3, 2011, 6:29 PM
Caleb AdeyemoPosted May 3, 2011, 6:08 PM
Caleb AdeyemoPosted May 3, 2011, 4:20 PM
Sam HobbsPosted Apr 27, 2011, 11:52 AM
Note that I am not doing this to make things easy for me. It is more work to teach someone than it is to do the work for them.
VulpesPosted Apr 27, 2011, 11:35 AM
http://msdn.microsoft.com/en-us/library/6tf1f0bc.aspx
If you get stuck, we'll help you finish it but it's important to do some thinking yourself or you'll get no benefit from the exercise.
Caleb AdeyemoPosted Apr 27, 2011, 11:07 AM
Honestly speaking, I'm learning. I'll be appreciative of your efforts to complete the job for me. There's no need posting the mess I did!
Thanks.
VulpesPosted Apr 27, 2011, 5:22 AM
I've implemented one of the methods (Print) and it remains now for Caleb to implement the Sort and Search methods:
Sam HobbsPosted Apr 26, 2011, 9:56 PM
FroglegPosted Apr 26, 2011, 9:40 PM
http://www.c-sharpcorner.com/Forums/Thread/118045/C-Sharp-program.aspx
Sam HobbsPosted Apr 26, 2011, 9:28 PM
FroglegPosted Apr 26, 2011, 9:12 PM
Caleb AdeyemoPosted Apr 26, 2011, 5:25 PM
Thanks so much for your concern.
Sam HobbsPosted Apr 22, 2011, 6:58 PM
Also note that we are very concerned about the next time. We want you to be able to do things like this yourself next time instead of depending on others to do it all for you. Please try to do it. Just try.