Thread: Why is this not working?

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    51

    Why is this not working?

    Everything is explained in the program on how it works. However, when compiled, it doesn't display the longest string. It displays the string I last entered. For example if I enter "What" "kind" "of" "program" "is" "this," it displays "this."
    Code:
    #include <stdio.h>
    #include "strlib.h"
    #include "genlib.h"
    #include "simpio.h"
    
    main()
    
    {
          string i, z, g;
          
          printf ("This program displays the longest string out of the strings you input.\n");
          printf ("Signal the end of your input list with the word 'end'.\n");
          printf ("Enter your first string: ");
          g = GetLine();
          while (!(StringEqual (g , "end")))
          {
                z = g;
                printf ("Enter your next string: ");
                g = GetLine();
                if (StringLength (g) > (StringLength (z)))
                {
                      i = g;
                      }
                }
          printf ("\nThe longest string is: %s", i);
          getchar();
          }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You are only comparing to the most recent string (the old value of g, now stored in z), not to the longest-so-far (stored in i).

    Hint: This is why one-letter variable names for anything more complicated than a counter make people annoyed.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't use any standard functions, and string isn't a standard C data type. Also, your indentation is messed up.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by tabstop View Post
    You are only comparing to the most recent string (the old value of g, now stored in z), not to the longest-so-far (stored in i).

    Hint: This is why one-letter variable names for anything more complicated than a counter make people annoyed.
    wow, thanks. I didn't think it would be that simple. I simply just replaced z with i in the if statement and assigned g to i in before the while statement. It looks like this.

    Code:
    #include <stdio.h>
    #include "strlib.h"
    #include "genlib.h"
    #include "simpio.h"
    
    main()
    
    {
          string i, z, g;
          
          printf ("This program displays the longest string out of the strings you input.\n");
          printf ("Signal the end of your input list with the word 'end'.\n");
          printf ("Enter your first string: ");
          g = GetLine();
          i = g;
          while (!(StringEqual (g , "end")))
          {
                z = g;
                printf ("Enter your next string: ");
                g = GetLine();
                if (StringLength (g) > (StringLength (i)))
                {
                      i = g;
                      }
                }
          printf ("\nThe longest string is: %s", i);
          getchar();
          }
    Last edited by PYROMANIAC702; 07-25-2011 at 08:39 PM.

  5. #5
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Also,
    Code:
    main()
    is wrong. Use
    Code:
    int main( void )
    instead.
    Code:
    while(!asleep) {
       sheep++;
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by PYROMANIAC702
    I simply just replaced z with i in the if statement and assigned g to i in before the while statement.
    tabstop expressed this as a hint, but it seems you missed it: use descriptive variable names. Otherwise, I will begin giving you a in which all n in my s are abbreviated to their first l.
    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

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by laserlight View Post
    tabstop expressed this as a hint, but it seems you missed it: use descriptive variable names. Otherwise, I will begin giving you a in which all n in my s are abbreviated to their first l.
    Sorry about that. I just had no clue on what to name them.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by PYROMANIAC702 View Post
    Sorry about that. I just had no clue on what to name them.
    Calling "i" "longest" and "z" "previous" would probably have prevented the whole thread.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by PYROMANIAC702
    I just had no clue on what to name them.
    Well, look at what you wrote:
    Code:
    g = GetLine();
    Code:
    printf ("\nThe longest string is: %s", i);
    So, g is the current string that you are considering and i is the longest string found thus far. You did not actually use z in your revised code, so you could have removed it.
    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

  10. #10
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by laserlight View Post
    Well, look at what you wrote:
    Code:
    g = GetLine();
    Code:
    printf ("\nThe longest string is: %s", i);
    So, g is the current string that you are considering and i is the longest string found thus far. You did not actually use z in your revised code, so you could have removed it.
    Oh yeah... z was used in my old code and I just forgot to remove it.

    Code:
    #include <stdio.h>
    #include "strlib.h"
    #include "genlib.h"
    #include "simpio.h"
    
    main()
    
    {
          string i, g;
          
          printf ("This program displays the longest string out of the strings you input.\n");
          printf ("Signal the end of your input list with the word 'end'.\n");
          printf ("Enter your first string: ");
          g = GetLine();
          i = g;
          while (!(StringEqual (g , "end")))
          {
                printf ("Enter your next string: ");
                g = GetLine();
                if (StringLength (g) > (StringLength (i)))
                {
                      i = g;
                      }
                }
          printf ("\nThe longest string is: %s", i);
          getchar();
          }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why is this not working
    By roelof in forum C++ Programming
    Replies: 15
    Last Post: 06-19-2010, 11:34 AM
  2. why is this not working
    By mouse666666 in forum C Programming
    Replies: 3
    Last Post: 02-08-2010, 11:25 PM
  3. Replies: 9
    Last Post: 03-30-2009, 04:09 AM
  4. While not working :(
    By fatty acid in forum C Programming
    Replies: 7
    Last Post: 12-05-2005, 06:04 PM
  5. Why isn't this working?
    By incognito in forum Windows Programming
    Replies: 1
    Last Post: 12-18-2003, 01:59 PM