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
7 1711
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
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,
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]
hello!
i think for this prg using a switch case is more appropriate -
#include<iostream.h>
-
int main()
-
{
-
char choice;
-
while(0)
-
{
-
cout<<"Enter a character i - inches m - meters e- exit";
-
cin>>choice;
-
switch(choice)
-
{
-
case 'i':
-
{
-
cout<<"Inches";
-
exit(0);
-
}
-
case 'm':
-
{
-
cout<<"Meters";
-
exit(0);
-
}
-
case 'e':
-
{
-
exit(0);
-
}
-
default:
-
{
-
cout<<"Enter the correct variable";
-
}
-
}
-
}
-
what happens is while(0) is an indefinite loop so unless you enter one of these variables,the program keeps going!
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:
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.
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,
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
...
|
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...
|
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...
|
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"...
|
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...
| |
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...
|
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(...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |