I need to code a function that will take in x numbers. (x is read into the function as an argument) The numbers can be preceded and separated by any number of white spaces and new-lines, and after the last number is entered, a new-line character ends the scan. I thought about using strings the take in characters, and to disregard any none numerical character. When a numerical character is entered, it will be read into the string, as well as any numerical characters following. When a white space or new-line is entered following a number, I want the number in the string to be entered into an array.
So if the string contains {'1', '3', '2'}, I want to to place the value of "132" into cell in an array.
I'll set a counter to count when a number is placed in an array, and when the necessary amount of numbers is reached, the function will return.
Here's what I have thus far:
void read_vector(int* v, int n)
{
int value, i = 0, j = 0, k;
char num, str[9];
do
{
num = getchar();
if (num > 47 && num < 58)
{
while (i < 10)
{
str[i] = num;
i++;
num = getchar();
if (i = 9 || num < 47 || num > 58)
{
str[i] = '\0'
j++;
for(k = 1; k <= strlen(str); k++)
{
value += str[k - 1] * pow(10, (strlen(str) - k));
}
v[j] = value
if(j = n)
return;
}
}
}
}
while (1);
}
Thanks