Reputation: 11
Cannot seem to find where I am going wrong, should have tested earlier pretty sure there is a major logic error here . I am trying to list countries from a text file based on user input, and I am getting this error, I have tried removing most of the code and tried typing other test codes and still get a major error, no idea what I am doing wrong here if anyone can show me what the issue is here, it would be great thanks. Basically this program is supposed to also list all the countries later if the user input is a capital letter but I can get to that later.
class CountryProgram
{
static void Main(string[] args)
{
CountryList printSearch = new CountryList();
printSearch.findAll();
}
public class CountryList
{
//declare all variable lists for each search
public StreamReader filePath = new StreamReader("countries.txt");
public static List<string> countryItems = new List<string>();
public static List<string> capitalItems = new List<string>();
public static List<string> populationItems = new List<string>();
public static List<string> allItems = new List<string>();
Country memberName = new Country();
//string retriever read all inputs
public string getString()
{
//<<<<<==============Country Search===================>>>>>
Console.WriteLine("Enter the first letter of the countries you wish to search: "); //prompts user to enter the country
string upperCaseInput = Console.ReadLine(); //gets input
string line = ""; //creates a empty string
bool finished = false;
List<string> upperList = new List<string>(); //creates a list for user input chars
foreach (char item in upperCaseInput) //adds all chars into a list named chars
{
if ( Char.IsUpper(item)) //checks if upper case and add to list if true
{
upperList.Add(item.ToString());
memberName.startsWith = true; //Assigns startsWith to true the moment a char entered is uppercase
}
}
//Begin listing countries once startsWith == true
while (((line = filePath.ReadLine()) != null) && (finished = false))
{
if (memberName.startsWith == true) // if upper case in inputted loop through the first column listing countries
{
countryItems.Add("test output"); //supposed to write results based on inputted upper case char item above
}
else //if not upper case show error message and end program
{
finished = true;
}
if (finished == true)
{
Console.WriteLine("Please enter one character in capital letters");
Console.ReadLine();
}
}
//<<<<<==============Capital Search===================>>>>>//
return countryItems.ToString();
}
//executing write to control panel depending on userinput
public void findAll()
{
Console.WriteLine(memberName.getString());
Console.ReadLine();
}
}
class Country : CountryList
{
//list variable properties
public string name { get; set; }
public string capital { get; set; }
public int population { get; set; }
public bool startsWith { get; set; }
public bool capitalHas { get; set; }
public bool lesserPopulation { get; set; }
//list counstructors
public Country()
{
}
public Country(string n, string c, int p)
{
name = n;
capital = c;
population = p;
}
}
Upvotes: 1
Views: 445
Reputation: 63732
Country
is derived from CountryList
. When you try to create an instance of CountryList
, it also tries to create an item of Country
(for the memberName
field). However, Country
also contains a memberName
field, and also needs to create it. For every instance of Country
, you create another instance of Country
until you run out of stack space - StackOverflowException
.
There's really no reason why you'd want to derive Country
from CountryList
. If CountryList
is supposed to be a list of countries, just have the list contain the items.
Upvotes: 1