Thread: help on loop

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    21

    Question help on loop

    any one know who to make loop on this??

    how to loop over and over again till i prest "-1" then only the program end


    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 %s is a palindrome\n",strsrc);
    else
    printf("\n %s is not a palindrome\n",strsrc);
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Use while ...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    where do i need to put the while?

    Mats you mean <math.h>

    ?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Not forgetting the FAQ on void main as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    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 (strsrc!=-1)
    if(strcmp(strsrc,strtmp)==0)
    printf("\n &#37;s is a palindrome\n",strsrc);
    else
    printf("\n %s is not a palindrome\n",strsrc);
    }
    this not working cuz its says !=' : no conversion from 'const int' to 'char *'

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's because you don't compare strings with !=. You compare strings with strcmp.

    Also, you would need to compare with the string "-1", not the number -1.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    then its must be like 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);
    while (strcmp!="-1")
    if(strcmp(strsrc,strtmp)==0)
    printf("\n &#37;s is a palindrome\n",strsrc);
    else
    printf("\n %s is not a palindrome\n",strsrc);
    }
    this still erroe thou...

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So if "you" did this one right:
    Code:
    if(strcmp(strsrc,strtmp)==0)
    why do you think it should be
    Code:
    while (strcmp!="-1")
    ?

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    because matsp says use while, and i dono where to used the while...

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Who's complaining about the while? It's the only thing right about that second quoted line.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    i still dont get you sir.. do you mind correct that code please..

  13. #13
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Code:
    while (strcmp(strcmp, "-1") !=0) {
    ...
    }
    You cannot use operators like !=, == with strings. Besides, strcmp is not even a string, it is a pointer to an array of char.

    Why test with -1 for a string by the way?? I don't know if this will work. If you enter -1 that will be two characters saved inside strsrc. If you reverse them, with strrev(), I assume you will have 1- as a string, not -1. You can just press "quit" or 'q' to quit the program...

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    StormRoBoT, try doing these tasks first:
    • 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 and of no more than 5 letters and prints "Correct!" if the word is "Storm" and "Wrong!" otherwise.
    • 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.

    Once you are done with these three tasks, you will have a better idea of how to combine them to solve your current problem.
    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. #15
    Registered User
    Join Date
    Sep 2008
    Posts
    21

    Unhappy

    its not working,

    any way you got any idea on how to do this in other way? where if i keyin -1 it will terminate the program..

    something 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

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, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12: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