views:

125

answers:

3

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

+2  A: 

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

Dr. Tim
After reading a little about fscanf(), I see the first argument is the stream from which the text will be entered. I'm unfamiliar with this. The numbers will be inputted in multiple ways, so I'm not sure if I can specify one. I see that scanf is the same as fscanf with stdin as the first arguement. I apologize. I'm fairly new at this. Can you explain stdin.My understanding is that scanf will continue looking until a new line is hit. I'd like it to continue looking until n numbers have been typed.
Kyle
There are three functions in the family, scanf which reads from stdin, fscanf which reads from a text file and sscanf which "reads" from a string. They use a format string to describe what the input should look like. They will skip arbitrary whitespace and assign variables based upon the format string.What isn't clear is whether this will be flexible enough for you because you say "The numbers will be inputted in multiple ways" without details.Spend some time with a good C manual such as Kernigan and Richie's "The C Programming Language" or poke around on line.Good luck.
Dr. Tim
A: 

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.

jmein
yeah, I meant to have "if (i == 9 || num < 47 || num > 58)"
Kyle
+1  A: 

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.)

Jason E