472,807 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,807 software developers and data experts.

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 3760
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,208 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

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

Similar topics

9
by: JS | last post by:
#include <stdio.h> main(){ int c, i, nwhite, nother; int ndigit; nwhite = nother = 0; for (i = 0; i < 10; ++i)
23
by: ern | last post by:
I have a program that runs scripts. If the user types "script myScript.dat" the program will grab commands from the text file, verify correctness, and begin executing the script UNTIL... I need...
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
7
by: DaVinci | last post by:
I am writing a pong game.but met some problem. the ball function to control the scrolling ball, void ball(int starty,int startx) { int di ,i; int dj,j; di = 1; dj = 1; i = starty;
1
by: pauljturner99 | last post by:
Hi, I'm trying to pass a parameter from a for loop to the nested while loop but only the first counter is passed. Here is the code: dim ctr redim ctr(5) ctr(0) = 2 ctr(1) = 4 ctr(2) = 6
3
by: libsfan01 | last post by:
hi all in my js code i have a while loop contained within a while loop executed on the basis of a conditional if statement, what i want to do is end the entire function on the last execution on...
14
by: Jan Schmidt | last post by:
Hi, in a nested do-while-loop structure I would like to "continue" the outer loop. With goto this should be no problem in while-loops. However, for do-while I cannot get it to work (without a...
6
by: mgcclx | last post by:
For loop and while loop. which one is faster? I see many articles fighting over it and different people come up with different results.
0
by: PythonNotSoGuru | last post by:
Hi everyone. I am using a while loop in a game and during the game loop i want the user to be able to give input without the program stopping at a certain point and asking for the input. In other...
3
by: numlock00 | last post by:
I have a nested 'while' loop that won't repeat, no matter how many times the outer loop repeats. The outer loop reads through an array of elements; the inner loop Ithe 'while' loop) is supposed to...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.