help on loop

This is a discussion on help on loop within the C Programming forums, part of the General Programming Boards category; any one know who to make loop on this?? how to loop over and over again till i prest "-1" ...

  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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,185
    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,185
    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,185
    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
    22,144
    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.
    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

  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

Page 1 of 4 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