Connecting Tech Pros Worldwide Forums | Help | Site Map

problem with while loop

Newbie
 
Join Date: Dec 2006
Posts: 15
#1: Dec 6 '06
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



Lives Here
 
Join Date: Oct 2006
Posts: 1,626
#2: Dec 6 '06

re: problem with while loop


Quote:

Originally Posted by angelcd

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
Newbie
 
Join Date: Dec 2006
Posts: 15
#3: Dec 6 '06

re: problem with while 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's Avatar
Lives Here
 
Join Date: Nov 2006
Location: Adelaide, SA
Posts: 1,748
#4: Dec 6 '06

re: problem with while loop


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]
Member
 
Join Date: Dec 2006
Posts: 47
#5: Dec 6 '06

re: problem with while loop


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!
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#6: Dec 6 '06

re: problem with while loop


Quote:

Originally Posted by angelcd

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.
Member
 
Join Date: Nov 2006
Location: Antipolo, City
Posts: 55
#7: Dec 6 '06

re: problem with while loop


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,
Newbie
 
Join Date: Dec 2006
Posts: 15
#8: Dec 7 '06

re: problem with while loop


Quote:

Originally Posted by thefarmer

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
Reply