I'm randomly generating simple math equations and need to get the numbers from the string and convert them into integers so I can add them. However, when I run the program I receive the "input string was not in the correct format" error on the "int N1Q1" line.
Is there something I'm missing? Would there be a better way to extract and convert a number from a string?
Question1.Text = Convert.ToString(random.Next(1, 9) + " + " + random.Next(1, 9) + " = ");
string FirstQuestion = Convert.ToString(Question1.Text);
int N1Q1 = Convert.ToInt32(FirstQuestion.Substring(0,1));
int N2Q1 = Convert.ToInt32(FirstQuestion.Substring(5,1));
intand do the assignment there. Alternatively you can create a wraper and encapsulate the Question type and enrich it with additional members.FirstQuestion.Substring(0,1)isn't anInt32type at allQuestion1.Textis already a string, why are you callingConvert.ToStringon it?Substring()s or look at them in the debugger to see if they're valid integer representations. I'd guess yourFirstQuestion.Substring(5,1)is actually a space...Convert.ToStringin the first line of code, and theTextproperty is already astring, so you don't need it in the second line either.