473,480 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

problem with while loop

15 New Member
i would like to ask you something about this while problem,

here is my code:

#include <iostream.h>

int main ()
{
cout<<" pls press letters from the choices below:";
cout <<" 1. i 2. m 3. e-exit";

{
char choice;
cin >> choice;


{
if (choice == ' i ')
cout << " Inches";

else if (choice == ' m ')
cout << " Meters";

else if (choice == ' e ')
{
break;
}
else
cout<< " Sorry, press the correct letter word:";

}}

return 0;
}


actually it compiles and it also run the output, my problem is, this program comes back from the start whenever the output comes out

ex :

pls press letters from the choices below
1. i 2. m 3. e-exit";

i
inches

after that it repeats again, i want it to continue the program until i enter "e" for exit, actually i tried to do some while loops but, the output never stop like:

inches
inches
inches
inches
inches
inches
inches

and i cant terminate it , would you please help me on this?

thanks,
angel
Dec 6 '06 #1
7 1711
willakawill
1,646 Top Contributor
i would like to ask you something about this while problem,

here is my code:

#include <iostream.h>

int main ()
{
cout<<" pls press letters from the choices below:";
cout <<" 1. i 2. m 3. e-exit";

{
char choice;
cin >> choice;


{
if (choice == ' i ')
cout << " Inches";

else if (choice == ' m ')
cout << " Meters";

else if (choice == ' e ')
{
break;
}
else
cout<< " Sorry, press the correct letter word:";

}}

return 0;
}


actually it compiles and it also run the output, my problem is, this program comes back from the start whenever the output comes out

ex :

pls press letters from the choices below
1. i 2. m 3. e-exit";

i
inches

after that it repeats again, i want it to continue the program until i enter "e" for exit, actually i tried to do some while loops but, the output never stop like:

inches
inches
inches
inches
inches
inches
inches

and i cant terminate it , would you please help me on this?

thanks,
angel
Hi Angel. I think you have left something out of the code. There is no loop
Dec 6 '06 #2
angelcd
15 New Member
yes theres, no loop can you help me provide loop for my program i do'nt exactly know where do i put that loop,

thanks,
Dec 6 '06 #3
DeMan
1,806 Top Contributor
One (not necessarily ideal) way might be to:

[code]
#include <iostream.h>

int main ()
{
cout<<" pls press letters from the choices below:";
cout <<" 1. i 2. m 3. e-exit";

{
char choice='x';

while(choice=='x')
{
cin >> choice;
if (choice == ' i ')
cout << " Inches";
else if (choice == ' m ')
cout << " Meters";
else if (choice == ' e ')
break;
else
{
cout<< " Sorry, press the correct letter word:";
choice='x'
}
}


return 0;
}
[/choice]
Dec 6 '06 #4
pangsans
47 New Member
hello!

i think for this prg using a switch case is more appropriate
Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. int main()
  3. {
  4.  char choice;
  5.  while(0)
  6. {
  7.  cout<<"Enter a character i - inches m - meters e- exit";
  8.  cin>>choice;
  9.  switch(choice)
  10.  { 
  11.   case 'i':
  12.   {
  13.     cout<<"Inches";
  14.     exit(0);
  15.  }
  16.   case 'm':
  17.   {
  18.     cout<<"Meters";
  19.     exit(0);
  20.  }  
  21. case 'e':
  22. {
  23.   exit(0);
  24. }
  25. default:
  26. {
  27.  cout<<"Enter the correct variable";
  28. }
  29. }
  30. }
  31.  
what happens is while(0) is an indefinite loop so unless you enter one of these variables,the program keeps going!
Dec 6 '06 #5
Ganon11
3,652 Recognized Expert Specialist
my problem is, this program comes back from the start whenever the output comes out

ex :

pls press letters from the choices below
1. i 2. m 3. e-exit";

i
inches

after that it repeats again, i want it to continue the program until i enter "e" for exit
What you need is a while loop, like everyone here has been saying. However, I think the others have misinterpreted what you're asking, so just to clarify: you want a program that will basically present a menu:

Press i for inches, m for meters, or e for exit

that will continue being displayed and calculations performed UNTIL the user hits e for exit.

If so, you can have the while loop controlled by the following condition:

Expand|Select|Wrap|Line Numbers
  1. while (ch != 'e')
You will have to have ch defined (as a character variable) outside of the loop. Also, you will have to ask the user for initial input. Inside the loop, you can immediately start with your calculations (i.e. if ch is i, print inches, or if ch is m, print meters), and do whatever you need to. The final statements in the loop, though, must be the menu and a prompt for user input. This loop will continue executing until the user enters 'e', at which point the loop condition will be false, and the loop will be skipped.

What the other repliers have addressed is a program that continues asking the user for input - as long as that input is bad (not i or m), or the input is e, they are still prompted - when i or m is entered, the program will display inches or meters accordingly and exit.
Dec 6 '06 #6
thefarmer
55 New Member
hi angel,

this will surely work try this:

#include <iostream.h>
#include <string.h>

int main ()
{
cout<<" pls press letters from the choices below:";
cout <<" 1. i 2. m 3. e-exit";

{
char choice;

while(choice!='e')
{
cin >> choice;
if (choice == ' i ')
cout << " Inches";
else if (choice == ' m ')
cout << " Meters";
else if (choice == ' e ')
{
break;
}
else
cout<< " Sorry, press the correct letter word:";

}
}


return 0;
}

i hope this will help you


good luck,
Dec 6 '06 #7
angelcd
15 New Member
hi angel,

this will surely work try this:

#include <iostream.h>
#include <string.h>

int main ()
{
cout<<" pls press letters from the choices below:";
cout <<" 1. i 2. m 3. e-exit";

{
char choice;

while(choice!='e')
{
cin >> choice;
if (choice == ' i ')
cout << " Inches";
else if (choice == ' m ')
cout << " Meters";
else if (choice == ' e ')
{
break;
}
else
cout<< " Sorry, press the correct letter word:";

}
}


return 0;
}

i hope this will help you


good luck,




Thanks, thefarmer it worked!!!!!

thanks alot
Dec 7 '06 #8

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

Similar topics

0
2910
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
1
1783
by: Nick Chorley | last post by:
Lo all, I've written a simple program that connects to a POP3 server, allows me to authenticate and then retrieves a certain message. The problem I'm having is that when it retrives the message...
6
4776
by: Ravi | last post by:
Hi All, I am trying to execute a select statement using the DBI module of perl in a for loop. I am getting a strange behaviour, the select statement is excuting correctly only for the last element...
4
4386
by: JP SIngh | last post by:
Thanks to Manohar for writing the basic code for displaying the managers and the employees in a tree like structure. I have adapted the code below but it gives me an error "exception occcured"...
9
5148
by: tym | last post by:
HELP!!! I'm going round the twist with this... I have a VB6 application which is using DAO to access a database (Please - no lectures on ADO, I know what I'm doing with DAO!!) Ok, problem...
8
2375
by: Jeff | last post by:
Hello everybody, I was doing one of the exercises in the K&R book, and I got something really strange. Here's the source code: /* * Exercise 2-2 from the K&R book, page 42 */ #include...
9
2453
by: Thomas Helmke | last post by:
Hello NG. I'm trying to read in a textfile. I will give you an extract of my code: bool LogfileHandle::ReadFile( string filename ) { std::ifstream LogFile( filename.c_str() ); if(...
1
1520
by: Promextheus Xex | last post by:
First php post... Hi there, I'm writing some code for my website to list people listening to my music. I have a loop for an array that dosen't seem to display all people. most of the time it...
7
2391
by: Problematic coder | last post by:
Dim objdr As Data.OracleClient.OracleDataReader = Nothing Dim objcnn As Data.OracleClient.OracleConnection = Nothing Dim objcom As Data.OracleClient.OracleCommand objcom = New...
0
1952
by: Filemaxor | last post by:
I have gotten my code to be able to allow people to add new text to a .txt document and able to call up files so the user can see whats in it. The problem i'm having is getting the for loop to work...
0
7039
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7080
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6735
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6895
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4476
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2992
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
176
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.