Turn on thread page Beta
    • Thread Starter
    Offline

    21
    ReputationRep:
    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();
    }
    }
    }
    }
    • Community Assistant
    Online

    20
    ReputationRep:
    Community Assistant
    (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 (which defaults to "hello", 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();//String message="The value is ";Int32 val=30;Console.WriteLine("testin g testing";Console.WriteLine(message+val); Console.WriteLine(message.Length );Int32 Len = message.Length;
    //string message = "hello";

    for (int i = 0; i < message.Length; i++) ;{Console.WriteLine(i);Console.R eadKey();}}}}
    Just as a heads up, if you edit your post with the advanced tab, you'll see a code block. Use that to paste your code in, as it makes it much easier to read and understand
    • Thread Starter
    Offline

    21
    ReputationRep:
    (Original post by Acsel)
    Just as a heads up, if you edit your post with the advanced tab, you'll see a code block. Use that to paste your code in, as it makes it much easier to read and understand
    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)
    • Community Assistant
    Online

    20
    ReputationRep:
    Community Assistant
    (Original post by Snoozinghamster)
    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)
    If you hit edit and advanced, there's a # symbol, that's the code block but the line breaks make it readable.

    I'm not super familiar with C# but some things that stand out as odd:
    If Length is a library call, should it be followed by brackets? So message.Length() and does the output of message.Length give the correct value?
    In your loop, can you use i < Len since you've made Len = message.Length
    What is actually happening at the moment? You say you can't get it to run the loop, does it throw an error or does nothing happen, etc.
    • Thread Starter
    Offline

    21
    ReputationRep:
    (Original post by Acsel)
    If you hit edit and advanced, there's a # symbol, that's the code block but the line breaks make it readable.

    I'm not super familiar with C# but some things that stand out as odd:
    If Length is a library call, should it be followed by brackets? So message.Length() and does the output of message.Length give the correct value?
    In your loop, can you use i < Len since you've made Len = message.Length
    What is actually happening at the moment? You say you can't get it to run the loop, does it throw an error or does nothing happen, etc.
    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"
    Offline

    20
    (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!

    Code:
    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();
    }
    }
    }
    }
    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..
    • Community Assistant
    Online

    20
    ReputationRep:
    Community Assistant
    (Original post by Snoozinghamster)
    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"
    Ah okay. No brackets after length does like like a C# thing. Do you still get an error if you change the condition to a fixed number, say i < 10? Your loop looks correct, so I'm not sure what i not existing in context is referring to. By chance does the error throw a line number that you can look at? I'm right in saying that removing the loop means everything works? And I'm guessing this loop also works fine if you chuck it in a blank program?
    • Thread Starter
    Offline

    21
    ReputationRep:
    Acsel, 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!
    • Community Assistant
    Online

    20
    ReputationRep:
    Community Assistant
    (Original post by Snoozinghamster)
    Acsel, 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!
    Glad to hear you've got it sorted. Checking the line before and generally checking semi colons was going to be my suggestion as they often throw errors that don't seem to make much sense
 
 
 
Reply
Submit reply
Turn on thread page Beta
Updated: December 6, 2018

University open days

  • University of East Anglia
    All Departments Open 13:00-17:00. Find out more about our diverse range of subject areas and career progression in the Arts & Humanities, Social Sciences, Medicine & Health Sciences, and the Sciences. Postgraduate
    Wed, 30 Jan '19
  • Solent University
    Careers in maritime Undergraduate
    Sat, 2 Feb '19
  • Sheffield Hallam University
    City and Collegiate Campus Undergraduate
    Sun, 3 Feb '19
Poll
Brexit: Given the chance now, would you vote leave or remain?

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.