The following code snippet transforms names into telephone book foremat. - 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>>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.
10 3760
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.
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.
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....
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
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.
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.
Thanks all. I`m happy to leave it there and have learnt some too....cheers.
The following code snippet transforms names into telephone book foremat. - 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
instead of just
.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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)
|
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...
|
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?
|
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;
|
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
|
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...
|
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...
|
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.
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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=()=>{
|
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...
| |