Click here to Skip to main content
16,021,464 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello everyone!
Why when i enter an integer it's work perfect(code below), but when i enter a char type simbol it's going crazy and loop run a single line forever? I understand that is a problem with the diferent variable types and compiler do not understand simply what need to enter.
How to define variable, that if you enter a char type, it converts to number and do normaly loop by condition or maybe how to define, that you could enter only a number?

do
{
int x;
std::cout << "Enter number: ";
std::cin >> x;

if (x == 1)
   std::cout "...";
   else if (x == 2)
   std::cout "...";
   else if (x == 3)
   std::cout "...";
   else
   std::cout "...";}
while (x < 1 || x > 3);
{
   x++;
}


Any ideas or offers? :)
Posted

The problem with loop is way too simple.

The problem is the condition and its OR operator (||) you use, Actually your define the union of sets {x < 1} and {x > 3}, which is the set outside of the segment [1, 3]. Should initial value be 3 or more, it will grow infinitely.

Did you really mean AND (&&) operator?

—SA
 
Share this answer
 
v3
Comments
Sandeep Mewara 31-Mar-11 14:34pm    
5 for the explanation of infinite loop :)
Sergey Alexandrovich Kryukov 31-Mar-11 14:54pm    
Thank you, Sandeep.
--SA
Tarun.K.S 31-Mar-11 14:40pm    
And a 5 here too! :D
Its been a long time SA, how are you doing sir?
Sergey Alexandrovich Kryukov 31-Mar-11 14:52pm    
Thank you, Tarun.
Different...
--SA
Tarun.K.S 31-Mar-11 14:58pm    
Ok.
Possible - using stringstream

Here: Using cin to get user input.[^]
This shows the way to convert the char to integer (a valid value if pssible.)

More details here:Basic Input/Output[^]


UPDATE:
Just came across this too (a good alternative): use std::cin.good() - It is 0 if the input is invalid.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 31-Mar-11 14:27pm    
My 5 but... You did not answer the trivial question about the inf. loop, which is asked in the title.

I did -- please see.
--SA
Tarun.K.S 31-Mar-11 14:39pm    
Lol teamwork?!
Sergey Alexandrovich Kryukov 31-Mar-11 14:59pm    
A hell of teamwork indeed, of the LOL, too :-)
--SA
Sandeep Mewara 31-Mar-11 14:55pm    
Thanks to you too. :)
Sandeep Mewara 31-Mar-11 15:05pm    
Great! Good to know.
The problem has been solved. I used string stream.
...
...
do
{
int x;
string xstr;
std::cout << "Enter number: ";
std::getline(std::cin, xstr);
std::stringstream(xstr) >> x;
if (x == 1)
   std::cout "...";
   else if (x == 2)
   std::cout "...";
   else if (x == 3)
   std::cout "...";
   else
   std::cout "...";}
while (x < 1 || x > 3);
{
   x++;
}


And it works now good even you enter a char type. And need don't forget includes...:D
 
Share this answer
 
A similar question had come up before, see this...

why program crashes when user gives char to a integer?[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900