Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. need help please

need help please

Scheduled Pinned Locked Moved C#
debugginghelptutorialquestion
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gondzer
    wrote on last edited by
    #1

    for (int i = 0; i < 5; i++)
    {
    i = int.Parse(Console.ReadLine());
    }
    Console.Write(i);
    Console.ReadKey();

    how to get console to print all of the numbers entered (5 numbers)? when i debug this it says that the name 'i' does not exist in current context.

    M V OriginalGriffO J 4 Replies Last reply
    0
    • G Gondzer

      for (int i = 0; i < 5; i++)
      {
      i = int.Parse(Console.ReadLine());
      }
      Console.Write(i);
      Console.ReadKey();

      how to get console to print all of the numbers entered (5 numbers)? when i debug this it says that the name 'i' does not exist in current context.

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Put the console.write and console.readkey INSIDE the for next loop. Move the } down 2 lines.

      Never underestimate the power of human stupidity RAH

      G 1 Reply Last reply
      0
      • G Gondzer

        for (int i = 0; i < 5; i++)
        {
        i = int.Parse(Console.ReadLine());
        }
        Console.Write(i);
        Console.ReadKey();

        how to get console to print all of the numbers entered (5 numbers)? when i debug this it says that the name 'i' does not exist in current context.

        V Offline
        V Offline
        vr999999999
        wrote on last edited by
        #3

        problem in this code is that 'i' is only defined for 'for loop ' to make it run u have to define i before for loop ex: int i = new int(); for (i = 0; i < 5; i++) { i = int.Parse(Console.ReadLine()); } Console.Write(i); Console.ReadKey(); it will not give any error but don't give u the result u want it should be like this.. static void Main(string[] args) { int i = new int(); int []arry=new int[5]; for (i = 0; i < 5; i++) { arry[i] = int.Parse(Console.ReadLine()); } for (i = 0; i < 5; i++) { Console.Write(arry[i]+"\n"); } Console.ReadKey(); }

        G 1 Reply Last reply
        0
        • M Mycroft Holmes

          Put the console.write and console.readkey INSIDE the for next loop. Move the } down 2 lines.

          Never underestimate the power of human stupidity RAH

          G Offline
          G Offline
          Gondzer
          wrote on last edited by
          #4

          i've tried that, it just writes the numbers straight after the entering, i want it to write all 5 AFTER i've entered the fifth number.

          M 1 Reply Last reply
          0
          • V vr999999999

            problem in this code is that 'i' is only defined for 'for loop ' to make it run u have to define i before for loop ex: int i = new int(); for (i = 0; i < 5; i++) { i = int.Parse(Console.ReadLine()); } Console.Write(i); Console.ReadKey(); it will not give any error but don't give u the result u want it should be like this.. static void Main(string[] args) { int i = new int(); int []arry=new int[5]; for (i = 0; i < 5; i++) { arry[i] = int.Parse(Console.ReadLine()); } for (i = 0; i < 5; i++) { Console.Write(arry[i]+"\n"); } Console.ReadKey(); }

            G Offline
            G Offline
            Gondzer
            wrote on last edited by
            #5

            ok, I thought I could do it without the arrays, but thanks anyway

            1 Reply Last reply
            0
            • G Gondzer

              i've tried that, it just writes the numbers straight after the entering, i want it to write all 5 AFTER i've entered the fifth number.

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              And that make no sense at all, you would have to put the numbers into a container (array/List<>) and then loop through the container and print them!

              Never underestimate the power of human stupidity RAH

              1 Reply Last reply
              0
              • G Gondzer

                for (int i = 0; i < 5; i++)
                {
                i = int.Parse(Console.ReadLine());
                }
                Console.Write(i);
                Console.ReadKey();

                how to get console to print all of the numbers entered (5 numbers)? when i debug this it says that the name 'i' does not exist in current context.

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #7

                Just to add to what the others have said - you don't want to do it like that anyway!

                for (int i = 0; i < 5; i++)
                {
                i = int.Parse(Console.ReadLine());
                }

                Since i is both the loop control variable and the place you store the value the user entered, if the user enters 4 or above as his first number, it will exit the loop immediately. You need to separate these two functions into two variables, as well as using an array or List<int> BTW: It is considered a bad idea to use "magic numbers" such as "5" in your code - when you move to an array, it is too easy to later change one and not the other, causing your application to fail, or crash:

                int[] inp = new int[5];
                for (int i = 0; i < 5; i++)
                {
                inp[i] = int.Parse(Console.ReadLine());
                }

                would be better as:

                const int elements = 5;
                int[] inp = new int[elements];
                for (int i = 0; i < elements; i++)
                {
                inp[i] = int.Parse(Console.ReadLine());
                }

                That way, you only have to change the number of elements in a single place, and it all works perfectly. And as a final thing, if your user enters an alphabetic character instead of a digit, your program will crash. You should always check your user input - they do enter rubbish quite often! :laugh: [edit]Typos - OriginalGriff[/edit]

                If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                1 Reply Last reply
                0
                • G Gondzer

                  for (int i = 0; i < 5; i++)
                  {
                  i = int.Parse(Console.ReadLine());
                  }
                  Console.Write(i);
                  Console.ReadKey();

                  how to get console to print all of the numbers entered (5 numbers)? when i debug this it says that the name 'i' does not exist in current context.

                  J Offline
                  J Offline
                  Jibesh
                  wrote on last edited by
                  #8

                  i wonder how this programs builds without any error? there are few things you need to check here 1.the variable i is accessible only inside the loop and its local to that loop never accessible outside the loops. 2. the indexer variable i is modified against your input which may alter the loop execution 3. Console.Write is outside the loop so if variable i is acceible outside the loop it will print only the last entered value not all try modifying your code. define a new variable to store the read line and move the Console.Write inside the loop and see the difference

                  Jibesh.V.P India

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups