468,491 Members | 2,010 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

Stopping a while loop

542 512MB
The following code snippet transforms names into telephone book foremat.
Expand|Select|Wrap|Line Numbers
  1. while(n<35)
  2.    {              
  3.     cin>>first>>middle>>last;
  4.     if(first=="z") break;
  5.     middle=middle.substr(0,1);
  6.     word=last+", "+first+" "+middle+".";
  7.     name[n]=word;
  8.     n++;
  9.    }
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>>word)' 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.
Feb 7 '08 #1
10 3475
Laharl
849 Expert 512MB
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.
Feb 7 '08 #2
whodgson
542 512MB
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.
Feb 7 '08 #3
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....
Feb 7 '08 #4
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
Feb 7 '08 #5
Laharl
849 Expert 512MB
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.
Feb 7 '08 #6
weaknessforcats
9,207 Expert Mod 8TB
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.
Feb 8 '08 #7
whodgson
542 512MB
Thanks all. I`m happy to leave it there and have learnt some too....cheers.
Feb 9 '08 #8
The following code snippet transforms names into telephone book foremat.
Expand|Select|Wrap|Line Numbers
  1. while(n<35)
  2.    {              
  3.     cin>>first>>middle>>last;
  4.     if(first=="z") break;
  5.     middle=middle.substr(0,1);
  6.     word=last+", "+first+" "+middle+".";
  7.     name[n]=word;
  8.     n++;
  9.    }

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

Expand|Select|Wrap|Line Numbers
  1.  if(strcmp(first,"z")==0)
instead of just

Expand|Select|Wrap|Line Numbers
  1. if(first=="z")
.
Feb 9 '08 #9
whodgson
542 512MB
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<iostream>, #include<sstream> and #include<string>. Thanks for your interest
Feb 19 '08 #10
Laharl
849 Expert 512MB
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 .
Feb 19 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by JS | last post: by
23 posts views Thread by ern | last post: by
7 posts views Thread by DaVinci | last post: by
3 posts views Thread by libsfan01 | last post: by
14 posts views Thread by Jan Schmidt | last post: by
6 posts views Thread by mgcclx | last post: by
reply views Thread by PythonNotSoGuru | last post: by
3 posts views Thread by numlock00 | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.