Thread: Stuck in a loop

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    40

    Stuck in a loop

    Can anybody see what is wrong with this? It gets stuck in a loop. I think I am too tired!! Thanks!

    Code:
     
    #include <iostream.h>
    #include <string.h> //used for strlen
    
    bool InLanguage(char s[], int first, int last)
    {
        // Even length -> false
        if((last-first)%2 == 1)
            return false;
    
        char sf=s[first],
             sl=s[last];
    
        // Both MUST be equal, otherwise false.
        if(sf != sl)
            return false;
    
        // Since sf==sl, check sf only
        if(sf != 'a' && sf != 'b')
            return false;
    
        // check single char sequence first
        if (first == last)
            return true;
        else
            return InLanguage(s, first + 1, last - 1);
    }
    
    void main()
    {
    char string[20];
    unsigned int First, Last;
    
    cout << "Enter a string less than 20 characters long: ";//prompt
    cin >> string;
    
    do
    { 
    cout << endl;
    cout << "Enter first index: ";//prompt user for input
    cin >> First;                      //input
    cout << "Enter last index: ";//prompt user for input
    cin >> Last;                    //input
    
    } while((First < 0 || First > strlen(string)-1) || (Last <= First || 
    Last > strlen(string)-1));
    
    for(unsigned int a = First; a <= Last; a++)
    {cout << string[a];}
    
    if(InLanguage(string, First, Last))
    {cout << " is in the language." << endl;}
    else
    {cout << " is not in the language." << endl;}
    
    
    }

  2. #2
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    Well it compiles fine. I just dont know what it is that the program is supposed to do.Can you explain what it is that you want it to do?
    big146

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    Yes it compiles fine but after you enter the string of characters etc. it goes into an infinite loop, I don't know why?

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    1. void main() is non standard. Use int main().

    2. iostream.h and string.h are deprecated. Use iostream and string

    3. It doesn't loop infinitely for me. What string and indexes do you use? I used "benny", 0 and 4.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    What input are you giving it?

    For example, if your input string contains a space, that will cause an infinite loop. When you use cin >> to get a string, it stops at the first space. Then, it gets messed up trying to get the int First later in the code. So try it without the space in the input string.

    Also, if you don't know how to use a debugger, now would be a great time to learn. You can learn a lot about why your program isn't working.


    P.S. <iostream.h> is non-standard, <string.h> is standard but deprecated (meaning it might be removed in the future). Replace them with <iostream> and <cstring>, since <string> is completely different (and better) than <string.h>/<cstring>.

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    since <string> is completely different (and better) than <string.h>/<cstring>.
    I was thinking that might be the case, but I wasn't really sure.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    I changed the #includes to <iostream> and <cstring> and it ran fine for me. No infinite loop here. I just dont understand what the output is supposed to be. I did the same thing as bennyandthejets ( my name is benny also) put indexes 0 and 4 and it just told me that it was not part of the language.
    big146

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    That's what it told me. I'm not really sure what the program is supposed to do so that's something for you to work out. Step through the program and check for logic flaws.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  9. #9
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Cool Tip of the Day

    When attempting to 'cin' to a character array.. try using the cin.getline('array name', 'array capacity') standard library function. This will allow you to enter virtually anything into your character array... and your array will be happy

    For example:

    Code:
    void main()
    {
    char string[20];
    unsigned int First, Last;
    
    cout << "Enter a string less than 20 characters long: ";//prompt
    cin >> string;
    Could be this...

    Code:
    void main()
    {
    char string[20];
    unsigned int First, Last;
    
    cout << "Enter a string less than 20 characters long: ";//prompt
    cin.getline(string, 20);

    Then.. when you are ready to 'cout' your character array.. simply refer to the array name:

    Code:
    cout << string;

    tada. The array will be able to accept and display virtually any characters or spaces. Just keep in mind the last element of the array will be assigned the '/0' Null character.
    Last edited by The Brain; 06-22-2004 at 03:11 PM. Reason: Wanted to make the explaination more specific.

  10. #10
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    P.S. <iostream.h> is non-standard, <string.h> is standard but deprecated (meaning it might be removed in the future). Replace them with <iostream> and <cstring>, since <string> is completely different (and better) than <string.h>/<cstring>.
    how about we pick apart that little statement you made there...

    ><iostream.h> is non-standard

    If you are referring to the fact that it is a deprecated header file, then you are correct, however, if you are trying to say that iostream.h is a header file that contains system-specific functions that do not work across the board, then you are completely and utterly wrong. iostream.h was standard for many years until they just changed it to iostream with no extension in the new standard.

    ><string.h> is standard but deprecated (meaning it might be removed in the future).

    cstring is a modification of the C string library to make it more like C++, and it is standard for C++. string.h is not deprecated. it has been the standard C string library for ages. Just because there is a C++ equivalent doesnt mean the C equivalent is deprecated. People may use C code as freely as they like in C++ programs. string is the STL string class and is part of C++ standard.
    My Website

    "Circular logic is good because it is."

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >If you are referring to the fact that it is a deprecated header file, then you are correct
    The standard makes no mention at all of iostream.h. It's neither standard nor deprecated. It and all it's buddies have been completely removed.

    >Just because there is a C++ equivalent doesnt mean the C equivalent is deprecated.
    This is true. But the fact that the standard states that something is deprecated does make it deprecated.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  2. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  4. Help! Stuck in a loop!
    By raell in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2003, 10:47 AM
  5. Stuck in a loop!.....Get me out of here!!
    By rabmaz in forum C Programming
    Replies: 3
    Last Post: 09-01-2002, 09:16 AM