Wolfeatspotatoes
Wolfeatspotatoes

Reputation: 135

Nested if and for loop problen

I am having problem with the if and for loop. The first closing bracket(}) is closing the first if statement. What I wanted to do is to close the bracket that it is aligned with.

startnum = int.Parse(startnumbers);

endnum = int.Parse(endnumbers);

string route = "1. ";

if (startletters == endletters && startnum > endnum)
{
   for (int count = 0; startnum < endnum; startnum++)
   {
      if (startname != endname)
      {
         count++;
         route += ("Board the {0} line, go {1} stops toward {2}",startletters,count, endname );
      }
   }
}

This is the screenshot

Upvotes: 0

Views: 87

Answers (2)

Rehan Shah
Rehan Shah

Reputation: 1627

The following mistakes that you are going to make:

enter image description here

How to solve it ?

The following ways to sovle this problem are: enter image description here

Your Code [Corrected] :

startnum = int.Parse(startnumbers);

endnum = int.Parse(endnumbers);

string route = "1.";

if (startletters == endletters && startnum > endnum)
{
   for (int count = 0; startnum < endnum; startnum++)
   {
      if (startname != endname)
      {
         count++;
         string tempRoute = string.Format("Board the {0} line, go {1} stops toward {2}",startletters,count, endname);
         route = route + " " + tempRoute;
      }
   }
}

Upvotes: 1

madreflection
madreflection

Reputation: 4947

You have a syntax error is in this line:

route += ("Board the {0} line, go {1} stops toward {2}",startletters,count, endname );

You have the parameters for string.Format but you're not actually calling it. You want this instead:

route += string.Format("Board the {0} line, go {1} stops toward {2}", startletters, count, endnum);

Because of the syntax error, the compiler isn't able to match the braces as you expect.

Upvotes: 3

Related Questions