Guest_imported
New member
- Jan 1, 1970
- 0
Basically my code looks like this:
#include <stdio.h>
#include <stdlib.h>
void main(void) {
int var1, i, j, k, tempCount;
int arrayName[2];
char input1[256], input2[256];
scanf("%d", &var1);
fgets(input1, sizeof(input1), stdin);
i = 0;
j = 0;
while(input1 != '\0') {
k = 0;
while((input1 != ' ') && (input1 != '\0')) {
input2[k] = input1;
k++;
i++;
}
arrayName[j] = atoi(input2);
for(tempCount=0; tempCount<=k; tempCount++){
input2[tempCount] = ' ';
}
i++;
j++;
}
for(i=0; i<=sizeof(arrayName); i++){
printf("%d ", arrayName);
}
}
It's ultimate goal is to take two lines of input, eg:
3
54 89 21
or
5
59 654 7 23 19
and put the second line as elements in an array. The first line of the input is to declare the number of items in the second line.
My code seems to skip over the fgets line, unless I remove the scanf line. Does anyone know why?
Also, it doesn't seem to be setting the first value of the second line entered into the array., the entry arrayName[0] just returns '0'. I can't see any reason for this either.
Thirdly, I wanted to declare the array "arrayName", after the scanf, like this:
int arrayName[var1];
but it won't let me do that either, and again I don't know why?
Thanks in advance.
#include <stdio.h>
#include <stdlib.h>
void main(void) {
int var1, i, j, k, tempCount;
int arrayName[2];
char input1[256], input2[256];
scanf("%d", &var1);
fgets(input1, sizeof(input1), stdin);
i = 0;
j = 0;
while(input1 != '\0') {
k = 0;
while((input1 != ' ') && (input1 != '\0')) {
input2[k] = input1;
k++;
i++;
}
arrayName[j] = atoi(input2);
for(tempCount=0; tempCount<=k; tempCount++){
input2[tempCount] = ' ';
}
i++;
j++;
}
for(i=0; i<=sizeof(arrayName); i++){
printf("%d ", arrayName);
}
}
It's ultimate goal is to take two lines of input, eg:
3
54 89 21
or
5
59 654 7 23 19
and put the second line as elements in an array. The first line of the input is to declare the number of items in the second line.
My code seems to skip over the fgets line, unless I remove the scanf line. Does anyone know why?
Also, it doesn't seem to be setting the first value of the second line entered into the array., the entry arrayName[0] just returns '0'. I can't see any reason for this either.
Thirdly, I wanted to declare the array "arrayName", after the scanf, like this:
int arrayName[var1];
but it won't let me do that either, and again I don't know why?
Thanks in advance.