0

I am trying to write a program, where I have a province, and it has a lot of properties. When I start the program, it shall read all the lines in the textFile(provinceData.txt), put it into an Array and then put that value into the struct. Now I tried every way I know to convert variables, and I haven't come to a solution. When I start the program, Visual Studio says"the input string has the wrong format".

Here is the code I already got:

 public struct provinceStruct001
 {
     public string name;
     public int terrain;
 }

 static void getProvinceDataFromFile()
 {
     string[] allLines = File.ReadAllLines("../../save/provinceData.txt");
     //puts all the Lines in the Text File into an Array

     provinceStruct001 ProvinceStruct001 = new provinceStruct001();

     ProvinceStruct001.name = allLines[4 + 1];

     ProvinceStruct001.terrain=Convert.ToInt32(Convert.ToString(allLines[8 + 1]));

     //8 is the Line in the textFile and +1 because an array starts at 0, 
     and i cant read line 0 in a text file
}

*

Here is the TextFile:

   struct provinceStruct001
   {

   string name;

   Munich  //this

   int occupier;

   0       //this 0=noone 1=rebels 2=foreign_country

   int terrain;

   0       //and this are the only important lines 
            0=flatlands,1=forest,2=hilly,3=mountainous

   bool isCapital;

  false

I thank everyone, who takes the time to help me in advance.

5
  • As you say, arrays start at index zero, so line 8 has index 7, right? Commented Jan 28, 2018 at 22:44
  • Put a breakpoint at Convert.ToInt32 and look at what allLines[8 + 1] contains. Commented Jan 28, 2018 at 22:52
  • Something has gone terribly wrong while editing this question. Could you update your question with the exact content of the file? Commented Jan 28, 2018 at 22:54
  • "8 is the Line in the textFile and +1 because an array starts at 0" Nope, it is -1 because an array starts at 0. Commented Jan 28, 2018 at 22:55
  • @derloopkat yes you were right, with 8+1 i would convert a "true" to an integer. It works now, thank you :) Commented Jan 29, 2018 at 10:17

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.