Kyle
Kyle

Reputation: 1

reading in numbers using strings

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

Upvotes: 0

Views: 295

Answers (3)

Jason E
Jason E

Reputation: 683

I would reccomend that you use strtok() to split the string into tokens, and the use the atoi(), atol(), or atof() to convert the text into numbers. For example:

void
read_nums(int count, double *buffer, char *str)
{
    int i;
    char *str2;

    // Read the first token
    str2 = strtok(srt, " \n\t\r");

    for (i = 0; i < count;) {
        if (*str2)
            // Convert and store the number
            buffer[i++] = atof(str2);

        // Read the next token
        str2 = strtok(NULL, " \n\t\r");
    }
}

I would recomend that you look up some good documentation on these functions to see exactly how to implement it (I never actually tested this example, and it fails to check for correctness.)

Upvotes: 1

Josh Mein
Josh Mein

Reputation: 28635

First of all, you seem to be a little confused about using comparison operators in C. Check out comparison and logical operators in C

Right now your ifs wont do what you would expect them to do

And Not to be nitpicky or anything, but your first while loop would probably suit your needs better as a for loop since you need a counter anyways.

Upvotes: 0

Tim Allman
Tim Allman

Reputation: 1521

I would use a member of the scanf family depending upon how your input is presented. fscanf() does pretty much what you're asking.

Upvotes: 2

Related Questions