Populate An Array With Numbers.
-
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; }
-
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; }
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 yourfor
loop) but you are indexing with wrong variable (I pointed out this problem at the start of my answer). I hope this helps. :) -
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 yourfor
loop) but you are indexing with wrong variable (I pointed out this problem at the start of my answer). I hope this helps. :)Nuri, Oh, my.... I feel so dumb. Thank you for your help.