Thread: pattern checking in a string question..

  1. #16
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    in my previous problem i did exactly the same thing for inputting numbers into a 2d array .
    why its not letting me input chars in a row?
    Code:
    int main() {
        int index;
        int str[40];
        int i;
        int length;
    
     printf("enter length\n");
     scanf("%d",&length);
     printf("enter string\n");
     for(index;index<length;index++){
       scanf("%c",&str[index]); 
     }
     i=getchar();

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    in my previous problem i did exactly the same thing for inputting numbers into a 2d array .
    why its not letting me input chars in a row?
    Note that your array is an array of ints, not chars. If you really want to read char by char in a loop, then it probably makes more sense to just call getchar() instead of using scanf().
    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

  3. #18
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i want to enter my string
    from a row of chars which i enter myself

    in every way possible

    i tried
    Code:
    int main() {
        int index;
        int str[40];
        int i;
        int length;
    
     printf("enter length\n");
     scanf("%d",&length);
     printf("enter string\n");
    
       scanf("%c",&str); //13
    
    
    
     printf("%d",checkPoly(str,length));
     return 0;
    }

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters, change this:
    Code:
    int str[40];
    to:
    Code:
    char str[40];
    After that your earlier code would probably work, just that it is a little weird to use scanf() in a loop to read character by character.
    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

  5. #20
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i did that.
    it doesnt let me enter the string in a row
    it doesnt even let me type anthing
    i dont mind changing the method

    i want it to let me enter the string by a row
    Code:
    #include <stdio.h>
    int checkPoly(char str[], int length);
    
    int main() {
        int index;
        char str[40];
        int i;
        int length;
    
     printf("enter length\n");
     scanf("%d",&length);
     printf("enter string\n");
     for(index;index<length;index++){
       scanf("%c",&str[index]); 
     }
     i=getchar();

  6. #21
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You never initialise 'index' to any value before you actually use it.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There is another potential problem in that the user would have to input enough characters to fill the entire string, and you do not account for the null terminator.
    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. #23
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ok i looked up in the javadoc of the file and it should
    recognize "(1.0+1.0i)x^1 +(1.0+1.0i)x^0"

    but i cant see that in the code
    there is no mention for "i' char in the code

    ??
    Last edited by transgalactic2; 12-26-2008 at 02:49 PM.

  9. #24
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    and it doesnt work it doesnt say 1 on that example

    i cant understand how its being built.

    what each part is suppossed to do in order to check for a pattern

  10. #25
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    this program is an example to a pattern checking program

    i want to check other patterns in the string

    how to pattern checking proccess works?

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    and it doesnt work it doesnt say 1 on that example
    Cross reference with the Java version: does the Java program return the correct answer? If it does, then perhaps you converted the program wrongly. If not, then clearly there is a bug in the program (or maybe the documentation, or both).
    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. #27
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    in general is this the way
    i need to do pattern checking?

    or there is an easier way

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    in general is this the way
    i need to do pattern checking?
    That depends on what you mean by "the way". Certainly checking if input follows some format means parsing the input.
    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

  14. #29
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    the way i meant "complex loop" system

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    the way i meant "complex loop" system
    Yes, though there would be the use of functions to hide the complexity, and with say, a recursive descent parser you would likely use recursion instead of iteration.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  5. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM