Thread: Determining what is the input's type.

  1. #46
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RyanC
    To be more frankly, I'm not that much practiced in using sscanf and I expect if I would done with it, I would get errors..so can you please write a code that resemble what you are talking exactly? once again not forcing you to write a code, take it as general, lets say there's "dynamic allocation: int ARRAY 300".
    I expect that if I wrote the code that did what I was talking about and posted it for you to read, you would remain with a lack of practice in using sscanf, so the next time you want to use sscanf, you would have to come running for help... and then since someone will then write the sscanf call for you, you will be a rank beginner in the use of sscanf forever and ever amen.

    So, here's a chance to get some practice with sscanf. Start by simplifying: I suggested that you find the ':' and then use sscanf, so simplify by removing the need to find the ':':
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char line[] = "int ARRAY 300\n";
        char object_type[10];
        int object_count;
        if (sscanf(/* ... */) == 2)
        {
            printf("Object type: %s\nObject count: %d\n", object_type, object_count);
        }
        return 0;
    }
    So, it is your task to replace the comment with the correct arguments to sscanf such that the above program will print:
    Code:
    Object type: int
    Object count: 300
    Hint: you can use a * in the right place to suppress an assignment. Use this to avoid having to actually create an array of char to read into for "ARRAY".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #47
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    I expect that if I wrote the code that did what I was talking about and posted it for you to read, you would remain with a lack of practice in using sscanf, so the next time you want to use sscanf, you would have to come running for help... and then since someone will then write the sscanf call for you, you will be a rank beginner in the use of sscanf forever and ever amen.

    So, here's a chance to get some practice with sscanf. Start by simplifying: I suggested that you find the ':' and then use sscanf, so simplify by removing the need to find the ':':
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char line[] = "int ARRAY 300\n";
        char object_type[10];
        int object_count;
        if (sscanf(/* ... */) == 2)
        {
            printf("Object type: %s\nObject count: %d\n", object_type, object_count);
        }
        return 0;
    }
    So, it is your task to replace the comment with the correct arguments to sscanf such that the above program will print:
    Code:
    Object type: int
    Object count: 300
    Hint: you can use a * in the right place to suppress an assignment. Use this to avoid having to actually create an array of char to read into for "ARRAY".
    here's what I've arrived, still gives wrong result.
    Code:
    #include <stdio.h>
    #include <string.h>
     
    int main(void)
    {
        char line[] = "int ARRAY 300\n";
        char object_type[10];
        int object_count;
        if (sscanf(line,"%s %d",object_type,&object_count) == 2)
        {
            printf("Object type: %s\nObject count: %d\n", object_type, object_count);
        }
        return 0;
    }

  3. #48
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    "%s %d" would match a string and a number. You need to match two strings and a number.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #49
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    "%s %d" would match a string and a number. You need to match two strings and a number.
    then actually must do as "%s %s %d"
    Last edited by RyanC; 06-05-2015 at 10:55 AM.

  5. #50
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Refer to my hint in post #46.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #51
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    Refer to my hint in post #46.
    didn't get you practically very well, that's what I arrived:
    Code:
       if (sscanf(line,"%s %s %d",object_type,*object_type,&object_count) == 2)

  7. #52
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, the * is applied to the conversion specifier, e.g.,
    Code:
    if (sscanf(line, "%s %*s %d", object_type, &object_count) == 2)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #53
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    No, the * is applied to the conversion specifier, e.g.,
    Code:
    if (sscanf(line, "%s %*s %d", object_type, &object_count) == 2)
    that's all I know about sscanf,, so??

  9. #54
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So now go on to experiment with:
    Code:
    char line[] = "dynamic allocation: int ARRAY 300\n";
    You can parse this with sscanf alone too, but then you need to read up on how to match character classes so you can match for "not ':'".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #55
    Banned
    Join Date
    Apr 2015
    Posts
    596
    I've done the max "Potential" for me, that's why I need help in my code...I already told you what my problem is, so can you please tell me how to fix it directly? because other than would just be useless! I thought too many times to fix it but all ways were really useless!!

  11. #56
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RyanC
    I've done the max "Potential" for me, that's why I need help in my code.
    So, what help do you need? I have broken down the problem for you and given you hints. The next thing that I can do is to give you the answer (and I arguably did that in my previous post), but if I have to do that, then you will not learn very much.

    Perhaps you need to take a break and try again later.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #57
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    So, what help do you need? I have broken down the problem for you and given you hints. The next thing that I can do is to give you the answer (and I arguably did that in my previous post), but if I have to do that, then you will not learn very much.

    Perhaps you need to take a break and try again later.
    LOl, I'm not blaming you actually and I already explained my problem, once again what should I do to my code for working correctly? my code is given below after I edited it many times and I think that's really the finish.(my input.txt just included "dynamic allocation:int arr1 10")
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    int* dynamic_allocation (int f)
    {
        int i=0,j=0;
        char arr1[100];
        for (i;i<100;i++)
            arr1[i]='*';
        i=0;
        for (j=99;j>=(100-f*sizeof(char));j--)
            arr1[j]='X';
        for (i;i<100;i++)
        {
            printf("%c ",arr1[i]);
        }
        return;
    }
     
    int main(void)
    {
        const char *filename = "E:/input.txt";
        FILE *fp = fopen(filename, "r");
        if (fp)
        {
            char line[80];
            while (fgets(line, sizeof(line), fp))
            {
                /* match for "dynamic allocation" at the start of the line: */
                if (strstr(line, "dynamic allocation") == line)
                {
                    int numberfromfile = 0;
                    char *parse_start = line;
                    char *parse_end = NULL;
                    /* find a starting point for a number: a number after a space */
                    while (parse_start[0] != '\0' && parse_start[1] != '\0' && !(isspace(parse_start[0]) && isdigit(parse_start[1])))
                    {
                        ++parse_start;
                    }
      
                    numberfromfile = strtol(parse_start, &parse_end, 100); 
                    if (*(parse_end) == '\n' || *(parse_end) == '\0')
                    {
                        dynamic_allocation(numberfromfile);
                    }
                }
            }
            fclose(fp);
        }
        else
        {
            fprintf(stderr, "There was an error opening '%s'\n", filename);
        }
        return 0;
    }
    I have succeeded to read to the function dynamic_allocation through input.txt "dynamic allocation:", but the plot is still need for getting the type of the arr1 and the integer number that's appearing after the arr1, e.g... "int arr1 10", I must get the type of arr1 as its "int" and the integer number "10" for doing this calculating sizeof(int)*10 which those are the number of allocated bytes!, that's all!
    P.S-I used "f" as the integer number that's appeared after the arr1 in input.txt
    thanks for your help and cooperation in advance.
    Last edited by RyanC; 06-05-2015 at 10:48 AM.

  13. #58
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You have to extend the parsing to include the type now. Laserlight has shown you how to do that, and I actually recommend that you follow her suggestion.

    The only other thing I think you should do is interpret that input and pass the number of bytes to dynamic_allocation as an argument. This makes the dynamic_allocation function simpler as well. No longer would you need to calculate 100 - f * sizeof(char).
    Code:
    if (strcmp("int", object_type) == 0)
    {
        dynamic_allocation(numberfromfile * sizeof(int));
    }
    else if (strcmp("float", object_type) == 0)
    {
        dynamic_allocation(numberfromfile * sizeof(float));
    }
    And it continues like that until all the types of input are covered.

    I note that all of these things are small edits that you've been shown how to do, so you should be able to fix it yourself now.

  14. #59
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RyanC
    what should I do to my code for working correctly?
    You should declare your "RAM array" in the main function. It cannot be declared in your dynamic_allocation function because your dynamic_allocation function should compute how much space to "allocate", and then "allocate" it by assigning 'X' to elements of the array. Rather, pass a pointer to the first element of that array, along with a count of the number of bytes allocated. You also need to update the count of the number of bytes allocated.

    But really, you do not need the "RAM array" at all:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define MEMORY_SIZE 100
    
    size_t compute_allocation_size(char object_type[], int object_count)
    {
        size_t object_size = 0;
        if (strcmp(object_type, "int") == 0)
        {
            object_size = sizeof(int);
        }
        else if (strcmp(object_type, "char") == 0)
        {
            object_size = sizeof(char);
        }
        else
        {
            fprintf(stderr, "Unrecognised type '%s'\n", object_type);
        }
        return object_size * object_count;
    }
    
    void print_result(size_t allocation_count)
    {
        size_t i;
        for (i = 0; i < allocation_count; ++i)
        {
            putchar('X');
        }
        for (i = allocation_count; i < MEMORY_SIZE; ++i)
        {
            putchar('*');
        }
        putchar('\n');
    }
    
    int main(void)
    {
        const char *filename = "input.txt";
        FILE *fp = fopen(filename, "r");
        if (fp)
        {
            size_t allocation_count = 0;
            char line[80];
            while (fgets(line, sizeof(line), fp))
            {
                char object_type[10];
                int object_count;
                if (sscanf(line, "%*[^:]: %9s %*s %d", object_type, &object_count) == 2)
                {
                    allocation_count += compute_allocation_size(object_type, object_count);
                }
                else if (strcmp(line, "print the result\n") == 0)
                {
                    print_result(allocation_count);
                }
            }
            fclose(fp);
        }
        else
        {
            fprintf(stderr, "There was an error opening '%s'\n", filename);
        }
        return 0;
    }
    Quote Originally Posted by RyanC
    but the plot is still need for getting the type of the arr1 and the integer number that's appearing after the arr1, e.g... "int arr1 10", I must get the type of arr1 as its "int" and the integer number "10" for doing this calculating sizeof(int)*10 which those are the number of allocated bytes!, that's all!
    My past few posts have been all about getting to you come to this stage yourself. The idea is to simplify to work on the problem by itself, then apply it to your actual program, but evidently you chose not to do that. You can find my answer in the code that I posted. I hope that you learn from it, but frankly, you're becoming a help vampire, so I shall stop here.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #60
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    You should declare your "RAM array" in the main function. It cannot be declared in your dynamic_allocation function because your dynamic_allocation function should compute how much space to "allocate", and then "allocate" it by assigning 'X' to elements of the array. Rather, pass a pointer to the first element of that array, along with a count of the number of bytes allocated. You also need to update the count of the number of bytes allocated.

    But really, you do not need the "RAM array" at all:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define MEMORY_SIZE 100
    
    size_t compute_allocation_size(char object_type[], int object_count)
    {
        size_t object_size = 0;
        if (strcmp(object_type, "int") == 0)
        {
            object_size = sizeof(int);
        }
        else if (strcmp(object_type, "char") == 0)
        {
            object_size = sizeof(char);
        }
        else
        {
            fprintf(stderr, "Unrecognised type '%s'\n", object_type);
        }
        return object_size * object_count;
    }
    
    void print_result(size_t allocation_count)
    {
        size_t i;
        for (i = 0; i < allocation_count; ++i)
        {
            putchar('X');
        }
        for (i = allocation_count; i < MEMORY_SIZE; ++i)
        {
            putchar('*');
        }
        putchar('\n');
    }
    
    int main(void)
    {
        const char *filename = "input.txt";
        FILE *fp = fopen(filename, "r");
        if (fp)
        {
            size_t allocation_count = 0;
            char line[80];
            while (fgets(line, sizeof(line), fp))
            {
                char object_type[10];
                int object_count;
                if (sscanf(line, "%*[^:]: %9s %*s %d", object_type, &object_count) == 2)
                {
                    allocation_count += compute_allocation_size(object_type, object_count);
                }
                else if (strcmp(line, "print the result\n") == 0)
                {
                    print_result(allocation_count);
                }
            }
            fclose(fp);
        }
        else
        {
            fprintf(stderr, "There was an error opening '%s'\n", filename);
        }
        return 0;
    }

    My past few posts have been all about getting to you come to this stage yourself. The idea is to simplify to work on the problem by itself, then apply it to your actual program, but evidently you chose not to do that. You can find my answer in the code that I posted. I hope that you learn from it, but frankly, you're becoming a help vampire, so I shall stop here.
    Lets say I want to follow your direction, the code you have submitted with my "input.txt" that's included just "dynamic allocation:int arr1 10" gives wrong result.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Determining function based an input
    By Suchy in forum C++ Programming
    Replies: 7
    Last Post: 02-20-2008, 02:52 PM
  2. Determining if input is int, double, or char
    By Lucid003 in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2005, 04:16 PM
  3. Determining a user-defined class type
    By Mingzhi in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2004, 05:33 PM
  4. Determining whether input is an integer.
    By scottmanc in forum C++ Programming
    Replies: 18
    Last Post: 02-29-2004, 05:39 PM
  5. Determining a variable type...
    By -leech- in forum C Programming
    Replies: 3
    Last Post: 11-17-2002, 03:02 PM