help on loop

This is a discussion on help on loop within the C Programming forums, part of the General Programming Boards category; its not working Show your current code. any way you got any idea on how to do this in other ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    its not working
    Show your current code.

    any way you got any idea on how to do this in other way? where if i keyin -1 it will terminate the program..
    Yes, but you should try and come up with it yourself. As I suggested, if you are having trouble solving your entire problem at one go, break it down into parts and solve each part separately, then combine the knowledge you gain from solving them separately to help you solve the entire problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    Code:
    #include<stdio.h>
    #include<string.h>
    
    void main()
    {
    char strsrc[5];
    char strtmp[5];
    
    printf("\n Enter a five-digit number ( -1 to end ): "); gets(strsrc);
    
    strcpy(strtmp,strsrc);
    strrev(strtmp);
    while (strcmp(strcmp, "-1") !=0) {
    if(strcmp(strsrc,strtmp)==0)
    printf("\n &#37;s is a palindrome\n",strsrc);
    else
    printf("\n %s is not a palindrome\n",strsrc);
    }
    }
    my current code

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    You should indent it properly, e.g.,
    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
        char strsrc[5];
        char strtmp[5];
    
        printf("\n Enter a five-digit number ( -1 to end ): ");
        gets(strsrc);
    
        strcpy(strtmp,strsrc);
        strrev(strtmp);
        while (strcmp(strcmp, "-1") !=0) {
            if(strcmp(strsrc,strtmp)==0)
                printf("\n &#37;s is a palindrome\n",strsrc);
            else
                printf("\n %s is not a palindrome\n",strsrc);
        }
    
        return 0;
    }
    Now, where is strrev() defined? As far as I can tell, you are trying to use a function that does not exist. Even before implementing your while loop, this should have come to your attention.

    Stop what you are doing right now and complete the tasks that I outlined. You are clearly jumping into the problem without making effort to break it down into parts and confirm that the individual pieces are working before integrating them. At this rate, you have no hope of finishing this simple problem since any attempt to fix the loop will not fix the problem as you are not even reversing the string, and any attempt to fix the string reversal will not fix the problem since the loop is incorrect.

    Oh, and you really should not be using gets(). Use say, fgets() instead (and you will discover that you actually want strsrc and strtmp to have 6 chars, not 5).

    EDIT:
    Actually, since you already have some idea of how to use strcmp(), you could actually accelerate working through the smaller tasks:
    • Write a program that reads in a word of no more than 5 letters and prints the word in reverse.
    • Write a program that reads in a word of no more than 5 letters and prints "Yes!" if the word is a palindrome, otherwise print "No!".
    • Write a program that reads in words of no more than 5 letters. While the word is not "-1", print the word and read another word, otherwise end the program.

    So your final program would basically be: write a program that reads in words of no more than 5 letters. While the word is not "-1", (print "Yes!" if the word is a palindrome, otherwise print "No!") and read another word, otherwise end the program.

    Nonetheless, resist the urge to jump tasks.
    Last edited by laserlight; 09-17-2008 at 01:31 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    oh ok wait actualy my code is this

    Code:
    #include<stdio.h>
    #include<string.h>
    
    void main()
    {
    char strsrc[5];
    char strtmp[5];
    
    printf("\n Enter a five-digit number ( -1 to end ): "); gets(strsrc);
    
    strcpy(strtmp,strsrc);
    strrev(strtmp);
    
    if(strcmp(strsrc,strtmp)==0)
    printf("\n &#37;s is a palindrome\n",strsrc);
    else
    printf("\n %s is not a palindrome\n",strsrc);
    }

    strrev <<< this mean revers the caracter so that i can compare with the origenal one witch is user have keyin

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    strrev <<< this mean revers the caracter so that i can compare with the origenal one witch is user have keyin
    Be warned that strrev() is not in the standard C library.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    then what are other method that i can use?

    and i need it to repeat over and over again..

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    then what are other method that i can use?
    Implement it yourself. Actually, you do not need to reverse the entire string to determine if it is a palindrome... but perhaps you can change this later.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    When you say it doesn't work what do you mean?
    Does it work when you not check for the -1? Just when you check if it is a palindrome?
    And you where noted that you will need 6 char for the strings. Five for the digits, one for the null character. Change it. Then if strrev() works correctly the program should work (I suppose).

    Now, if you want to add the -1 thing to terminate then I already suggested why it wouldn't work. Again, -1 is two characters. If you reverse this string "-1", you get this string "1-". Don't mix characters with numbers. So you can check for "1-" to terminate. But if this is not actually a requirement, as I already said, it is not very "nice" to do so. Better check for "quit" or another key word/character. The idea of -1 is good if you actually read int from the input not characters.

  9. #24
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    what i wan is when i run the program the program execute it over and over again...

    this code that i have post here is only can test for 1 time then the program end, but i wan its over and over again till i key in -1 then only the program end



    some thing like this
    Code:
    Enter a five-digit number ( -1 to end ): 21212
    21212is a palindrome
    
    Enter a five-digit number ( -1 to end ): 16633
    16633 is not a palindrome
    
    Enter a five-digit number ( -1 to end ): -1
    do you get me?

  10. #25
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655

  11. #26
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    can i do this in division and remainder operators to separate the number into its individual digits?

  12. #27
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Anyway, we are making circles here. Here is what you want (even though I already posted it)
    Try this and it will end properly.
    But AGAIN, you have MORE errors. So do this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void) {
       char strsrc[6];
       char clean[100];
       while(1) {
           printf("Enter 5 digits or -1 to exit: ");
           fgets(strsrc, 6, stdin);
           if (strcmp(strsrc, "-1\n") ==0) break; //exit while
           printf("OK\n"); //otherwise print OK
           fgets(clean, 100, stdin); //clean remaining characters
       }
    }
    the above will print OK until you press -1. You NEED to clean the remaining characters. If you don't, then if somebody enters 5 digits the \n will remain (since they pressed enter). So next time that will be read. To avoid this, you call one more fgets() which will read everything that is left and "clean" stdin. If somebody enters more than 5 digits, then the first five will be read and the remaining will be cleaned. In any way this should work for you for (almost, don't try to prove me wrong) all input.

    EDIT: fgets reads also the \n, so if you enter -1 you will actually have "-1\n". So I changed the strcmp to include this
    Last edited by C_ntua; 09-17-2008 at 03:10 PM.

  13. #28
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    fgets(strsrc, 6, stdin);

    I prefer
    fgets(strsrc, sizeof strsrc, stdin);
    so no question arises what is magic 6
    and also no problem could appear if the size of the array is suddenly changed



    //clean remaining characters
    if the string was read fully - this call will require user to press enter once again - better to check that the \n is not present in the strsrc buffer before calling this "cleaning" fgets
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #29
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    At what point does your program exit?

  15. #30
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by vart View Post
    fgets(strsrc, 6, stdin);

    I prefer
    fgets(strsrc, sizeof strsrc, stdin);
    so no question arises what is magic 6
    and also no problem could appear if the size of the array is suddenly changed




    if the string was read fully - this call will require user to press enter once again - better to check that the \n is not present in the strsrc buffer before calling this "cleaning" fgets
    Yup, sizeof() is much better, it was a mistake not to have it.
    As for the freezing, it will also freeze if you only enter 2 or 3 digits. It was just a sample code, not meant to be perfect, that is the OP's job. I just added it because otherwise it will be completely wrong since it would automatically read again...

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 11:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 03:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 01:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21