The Student Room Group

C# help, for loop

hi,

im currently learning C# and ive run into an issue, i just cant get the for loop to work as i want it to (im used to python for context),

its meant to be you enter a message, and then for every character in said message it runs the for loop, theres a series of checks it will then hopefully perform on each character. however at the moment i cant make it run the for loop.

one of the many attempts is below. it should just essentially end up counting up to the number of letters in the message, any help would be much appreciated, as this is kinda time sensitive!


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter message");
string message;
message = Console.ReadLine();
Console.WriteLine(message.Length);
Int32 Len = message.Length;

for (int i = 0; i < message.Length; i++) ;
{
Console.WriteLine(i);
Console.ReadKey();
}
}
}
}
(edited 5 years ago)
Thanks, might be being a bit stupid, cant find the code block, have edited to add some spaces to make it at least mostly legible, just missing indentations etc which i dont believe are the isuse! (for once)
oh yeh i ended up doing the length thing twice to see if either way worked (they both threw up errors) but the Len = message.Length line can go, but makes no difference.
before i added the for loop the length bit worked fine. i grabbed that line from the interent and it didnt have brackets there so assumed it was just a C# thing as my brain wanted brackets as well, using brackets just gives me an extra error though!
the current error im getting is "Possible mistaken empty statement" "The name 'i' does not exist in the current context"
Reply 3
Original post by Snoozinghamster
hi,

im currently learning C# and ive run into an issue, i just cant get the for loop to work as i want it to (im used to python for context),

its meant to be you enter a message, and then for every character in said message it runs the for loop, theres a series of checks it will then hopefully perform on each character. however at the moment i cant make it run the for loop.

one of the many attempts is below. it should just essentially end up counting up to the number of letters in the message, any help would be much appreciated, as this is kinda time sensitive!


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter message":wink:;
string message;
message = Console.ReadLine();
Console.WriteLine(message.Length);
Int32 Len = message.Length;

for (int i = 0; i < message.Length; i++) ;
{
Console.WriteLine(i);
Console.ReadKey();
}
}
}
}
You can't just say i < message.Length in a for condition. Try i <= message.Length - i is less than or equal to message length. I can't remember how to make it a concrete exclusive less than though tbh..
, thanks for your help, turns out it was a rogue semicolon. had one at the end of the for line, turns out i shouldnt have. who even knows where they should go!
(edited 5 years ago)

Quick Reply

Latest

Trending

Trending