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 / C++ / MFC
  4. Populate An Array With Numbers.

Populate An Array With Numbers.

Scheduled Pinned Locked Moved C / C++ / MFC
questiondatabasevisual-studiodata-structures
3 Posts 2 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.
  • M Offline
    M Offline
    Mike Certini
    wrote on last edited by
    #1

    I am having problems with populating an array with int numbers. Since I am a new programmer, I do not know why my VS 2008 is not completing the code block. In other words when I execute the program, a blank console screen opens. I then input a number and press enter. After I press enter the program stops in a suspended state. What I have discovered is that I have to input a \o for the program to know that it is the end of my input. I though thought that when I pressed enter, VS 2008 would automatically place this end of string designator. Secondly, I have problems with index variable. When I loop one time or when I press enter after inputing a number, my index number advances to 8. My question is why the index does not advance to 2?

    #include <stdio.h>

    int const limit = 8;
    int numbers[limit];

    int main(void)
    {
    for(int index = 0; index < limit; index++)
    {
    scanf("%d", &numbers[limit]);
    }

    return 0;
    }
    
    N 1 Reply Last reply
    0
    • M Mike Certini

      I am having problems with populating an array with int numbers. Since I am a new programmer, I do not know why my VS 2008 is not completing the code block. In other words when I execute the program, a blank console screen opens. I then input a number and press enter. After I press enter the program stops in a suspended state. What I have discovered is that I have to input a \o for the program to know that it is the end of my input. I though thought that when I pressed enter, VS 2008 would automatically place this end of string designator. Secondly, I have problems with index variable. When I loop one time or when I press enter after inputing a number, my index number advances to 8. My question is why the index does not advance to 2?

      #include <stdio.h>

      int const limit = 8;
      int numbers[limit];

      int main(void)
      {
      for(int index = 0; index < limit; index++)
      {
      scanf("%d", &numbers[limit]);
      }

      return 0;
      }
      
      N Offline
      N Offline
      Nuri Ismail
      wrote on last edited by
      #2

      Here is the problem:

      // You are indexing with "limit", which is the size of your array
      scanf("%d", &numbers[limit]);
      // Change the above line to:
      scanf("%d", &numbers[index]);

      Mike Certini wrote:

      What I have discovered is that I have to input a \o for the program to know that it is the end of my input.

      This is not true. When you enter the limit-th number the loop will end and the program will exit.

      Mike Certini wrote:

      Secondly, I have problems with index variable. When I loop one time or when I press enter after inputing a number, my index number advances to 8. My question is why the index does not advance to 2?

      Actually it is incremented by 1 after each iteration (index++ part of your for loop) but you are indexing with wrong variable (I pointed out this problem at the start of my answer). I hope this helps. :)

      M 1 Reply Last reply
      0
      • N Nuri Ismail

        Here is the problem:

        // You are indexing with "limit", which is the size of your array
        scanf("%d", &numbers[limit]);
        // Change the above line to:
        scanf("%d", &numbers[index]);

        Mike Certini wrote:

        What I have discovered is that I have to input a \o for the program to know that it is the end of my input.

        This is not true. When you enter the limit-th number the loop will end and the program will exit.

        Mike Certini wrote:

        Secondly, I have problems with index variable. When I loop one time or when I press enter after inputing a number, my index number advances to 8. My question is why the index does not advance to 2?

        Actually it is incremented by 1 after each iteration (index++ part of your for loop) but you are indexing with wrong variable (I pointed out this problem at the start of my answer). I hope this helps. :)

        M Offline
        M Offline
        Mike Certini
        wrote on last edited by
        #3

        Nuri, Oh, my.... I feel so dumb. Thank you for your help.

        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