1

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;
        }
    }
  • `public StreamReader filePath = new StreamReader("countries.txt");`, The variable name File path is not a file path but an open Stream to a file. Stream have to be close. Swtich the type to string and store the Filepath in filepath. And use a [using block](https://stackoverflow.com/questions/212198) before the While loop – xdtTransform Apr 19 '19 at 07:42
  • even when i comment that out it seems to sill happen – Troydon Luicien Apr 19 '19 at 07:52
  • 1
    By the way, that `(finished = false)` **sets** finished to `false` (and has a false value). You want a `==` there or better: use `( ! finished)` – Hans Kesting Apr 19 '19 at 08:46
  • In my comment was about an other issue that the one you faced. You already have an answer for that one. It was an logical error. As you are learning, I provided basic advice on your questions( this one and the geocery one). – xdtTransform Apr 19 '19 at 08:47
  • and thanks you for that xdTransform it helps :) Oh i decided to remove the finished part cus I was just adding it to make it more hard but it looks like its already too hard. – Troydon Luicien Apr 19 '19 at 10:23

1 Answers1

1

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.

Luaan
  • 62,244
  • 7
  • 97
  • 116
  • okay i just did that because it was the only way for me to access name from Country so how else would I access the names from Country, or would I just have to add the name and bool to country list as well? – Troydon Luicien Apr 19 '19 at 07:08
  • 1
    @TroydonLuicien I'm not sure how to help you with the other points; I'd suggest starting with the simplest thing that works, rather than trying to write everything in one go. Right now, your code is very confused; you're trying to do too many things at once, and you don't really know how. That's not weird - you're obviously just starting; but try to keep focus, and do things one small bit at a time. Write the code for reading the file first, and e.g. writing the lines to screen. Then add the code that does the filtering. Then add the UI. Don't complicate things, just do the simple thing. – Luaan Apr 19 '19 at 10:52