Stopping a while loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whodgson
    Contributor
    • Jan 2007
    • 542

    Stopping a while loop

    The following code snippet transforms names into telephone book foremat.
    Code:
    while(n<35)
       {              
        cin>>first>>middle>>last;
        if(first=="z") break;
        middle=middle.substr(0,1);
        word=last+", "+first+" "+middle+".";
        name[n]=word;
        n++;
       }
    To get it to work properly I had to introduce the 'if' statement in line 3 of the loop as well as pressing Ctrl+Z to quit after entering the last name. Without the 'if' statement it keeps on printing the last word entered until n==35. I have tried using 'while(cin>>wor d)' and placing 1st and 3rd lines in { } but no success. How can I get rid of the 'if' statement and have the code work properly?
    Thanks in advance if you can help.
    Last edited by Ganon11; Feb 7 '08, 01:43 AM. Reason: Fixing [CODE] tags.
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Without that if statement, it will obviously continue looping until n = 35. If you want it to stop before then, you'll need some kind of if statement/switch/something and break. What exactly are you trying to do, make it stop printing when the user stops entering names?

    Also, if you're on Linux/Unix/Cygwin/etc, Ctrl-Z does not kill the process, it pauses it. Use Ctrl-C instead.

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      My OS is XP home. Ctrl+C returns me from the DOS screen to the IDE.
      Yes I just want to print the names which are input not extra copies of the last one. Thanks for your interest.

      Comment

      • gunner666
        New Member
        • Feb 2008
        • 3

        #4
        dude,wat are u trying to do here,
        u trying to take in names frm the users 34 times,is tht right???????
        why do u have a prob wit the code,it seems Fine to me....

        Comment

        • Arulmurugan
          New Member
          • Jan 2008
          • 90

          #5
          Originally posted by whodgson
          My OS is XP home. Ctrl+C returns me from the DOS screen to the IDE.
          Yes I just want to print the names which are input not extra copies of the last one. Thanks for your interest.
          Hi,
          Yes, by pressing Ctrl+C may open the IDE. why means your application is not handling Ctrl+C signal, so it does default action.
          there are 2 sol i can give.

          1.Use signal handler while pressing ctrl+c and print what ever you need..
          2.Every time in loop ask "do you want to continue or not? " if yes continue or break the loop.

          -Arul

          Comment

          • Laharl
            Recognized Expert Contributor
            • Sep 2007
            • 849

            #6
            You can also use some kind of predetermined "exit code" that is data that would not normally be entered. I often use -1 for this sort of thing. Basically, immediately after cin, you'd check if the string is -1 (or whatever you choose, maybe 'quit'). If it is, break. If not, continue with your code. It would be smart to tell the user this, but if you're feeling vicious, make them guess.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Originally posted by :aharl
              Also, if you're on Linux/Unix/Cygwin/etc, Ctrl-Z does not kill the process, it pauses it. Use Ctrl-C instead.
              CTRL+Z does not kill the process in Windows either. You use a CTRL+C fpr that.

              CTRL+Z signals the end of all input. Without it, input loops will cycle forever looking top more input. You can test for this as EOF.

              Comment

              • whodgson
                Contributor
                • Jan 2007
                • 542

                #8
                Thanks all. I`m happy to leave it there and have learnt some too....cheers.

                Comment

                • jcmiras
                  New Member
                  • Feb 2008
                  • 1

                  #9
                  Originally posted by whodgson
                  The following code snippet transforms names into telephone book foremat.
                  Code:
                  while(n<35)
                     {              
                      cin>>first>>middle>>last;
                      if(first=="z") break;
                      middle=middle.substr(0,1);
                      word=last+", "+first+" "+middle+".";
                      name[n]=word;
                      n++;
                     }

                  I suppose your "first" variable is of string type and not a char, try to use

                  Code:
                   if(strcmp(first,"z")==0)
                  instead of just

                  Code:
                  if(first=="z")
                  .

                  Comment

                  • whodgson
                    Contributor
                    • Jan 2007
                    • 542

                    #10
                    Yes i agree but your solution was so simple it would not compile. I am trying to use C++ strings not C strings. Think strcmp() may only be used with C strings.
                    The header files in this code are #include<iostre am>, #include<sstrea m> and #include<string >. Thanks for your interest

                    Comment

                    • Laharl
                      Recognized Expert Contributor
                      • Sep 2007
                      • 849

                      #11
                      The string class has a compare() function that takes another string as an argument and works like strcmp() in return value. Documentation at http://cplusplus.com/reference/strin...g/compare.html .

                      Comment

                      Working...