Jump to content

  • Log In with Google      Sign In   
  • Create Account

Breaking out of an infinite loop


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 Sid_TheBeginner   Members   -  Reputation: 157

Like
0Likes
Like

Posted 24 June 2012 - 08:26 PM

Suppose i have a code like this:

for(;;)
{
int value;

cout << "Enter value(q to quit): ";
cin >> value;
}

I want to quit when q is entered. How do i do it? I'm yet not introduced to the ctype library in the book I'm following. So which are the other methods to do so?

Thanks,
--Sid

Sponsor:

#2 mark ds   Members   -  Reputation: 1483

Like
0Likes
Like

Posted 24 June 2012 - 08:34 PM

if value == 'q' break;

#3 Sid_TheBeginner   Members   -  Reputation: 157

Like
0Likes
Like

Posted 24 June 2012 - 08:43 PM

if value == 'q' break;


That's just causing an infinite loop at the command prompt.

#4 japro   Members   -  Reputation: 887

Like
0Likes
Like

Posted 24 June 2012 - 08:52 PM

the problem is that q is not parsed as a number so you just get a 0 I think. So you either have to read in a string see if it equals q and otherwise convert it to a int (std::stringstream, std::atoi or boost::lexical_cast), or use a special number instead of q ("enter -1 to exit"),

#5 Sid_TheBeginner   Members   -  Reputation: 157

Like
0Likes
Like

Posted 24 June 2012 - 09:09 PM

Sorry I didn't understand. Can you please post a small snippet of code? -1 works fine. But I just wanted to know how to quit using a character such as 'q'.

Edited by Sid_TheBeginner, 24 June 2012 - 09:15 PM.


#6 DevLiquidKnight   Members   -  Reputation: 834

Like
0Likes
Like

Posted 24 June 2012 - 09:22 PM

#include <iostream>
using namespace std;
int main()
{
for(;;)
{
  char value; // changed from integer to character value cin was expecting a integer before not a character.
  cout << "Enter value(q to quit): ";
  cin >> value;
  if(value == 'q')
  {
   break;
  }
}
cin.get();
return 0;
}
Alternatively you could just use a do-while loop instead of using a for loop in this way which would make more sense in this case. Or even use getline to receive the data and store it in a std::string.

Edited by DevLiquidKnight, 24 June 2012 - 09:26 PM.


#7 Sid_TheBeginner   Members   -  Reputation: 157

Like
0Likes
Like

Posted 24 June 2012 - 09:27 PM

Oops! sorry for the confusion i'll post my code:
Its the Binary Search Algo:

#include<iostream>
#include<ctype.h>
using namespace std;
int main()
{
int arr[] = {1, 4, 5, 6, 9, 14, 21, 23, 28, 31, 35, 42, 46, 50, 53, 57, 62, 63, 65, 74, 79, 89, 95};
int value;
for(int i = 0; i < 23; i++)
cout << arr[i] << " ";
cout << endl;
while(1)
{
cout << "Enter seach value(-1 to exit): ";
cin >> value;
if(value == -1)
{
cout << "Exiting..\n";
break;
}
int beg = 0;
int end = 23;
for(beg = 0; beg < 23; beg++)
{
int middle = (beg+end)/2;
if(value == arr[middle])
{
cout << "Its at location " << middle << endl;
break;
}
else if(value > arr[middle])
end = middle + 1;
else if(value < arr[middle])
end = middle - 1;
else
cout << "Element Not found\n";
}
}
}




So basically, I want to quit when q is entered. For now I'm using -1 as suggested by japro. An exercise demands 'q' and so I decided to post.

Edited by Sid_TheBeginner, 24 June 2012 - 09:27 PM.


#8 DevLiquidKnight   Members   -  Reputation: 834

Like
0Likes
Like

Posted 24 June 2012 - 09:34 PM

#include<iostream>
#include<ctype.h>
using namespace std;
int main()
{
int arr[] = {1, 4, 5, 6, 9, 14, 21, 23, 28, 31, 35, 42, 46, 50, 53, 57, 62, 63, 65, 74, 79, 89, 95};
char value;
for(int i = 0; i < 23; i++)
  cout << arr[i] << " ";
  cout << endl;
while(1)
{
  cout << "Enter seach value(q to exit): ";
  cin >> value;
  if(value == 'q')
  {
   cout << "Exiting..\n";
   break;
  }
  int beg = 0;
  int end = 23;
  for(beg = 0; beg < 23; beg++)
  {
   int middle = (beg+end)/2;
   cout << middle << endl;
   if(value == arr[middle])
   {
    cout << "Its at location " << middle << endl;
    break;
   }
   else if(value > arr[middle])
   {
    end = middle + 1;
   }
   else if(value < arr[middle])
   {
    end = middle - 1;
   }
   else
   {
    cout << "Element Not found\n";
   }
  }
}
}

Like I said Posted Image

#9 Brother Bob   Moderators   -  Reputation: 8611

Like
3Likes
Like

Posted 25 June 2012 - 03:18 AM

Like I said Posted Image

Did you try your own fix? It's not as easy as you make it by simply changing the type of value from an int to a char. First, you cannot enter a two-digit number anymore since only one character is read from the input, and second, the actual integer value in the variable value is the character code of the character and not its logical value.

Solid text input management, especially with mixed types, is not the easiest problem to handle correctly. To begin with, you have three cases to check for:
  • an integer is entered.
  • a 'q' is entered.
  • something else is entered.
All three of these cases must be handled. You also need to decide how to handle further special cases of case 3. For example is "3.14" an invalid input, or should it be treated as "3" and discard the rest, or treat it as a sequence of "3", an invalid input (the period) followed by the value "14"?

These questions may all be above your current task. For example, it may be enough for you to assume that the input is always valid in which case point number 3 does not have to be considered. Point 3 is where all error handling is made though.

My initial suggestion is to read the input into an std::string, inspect the input and act on it based on that. For example, you can check the first character, and if it's a 'q' you exit, if it's an integer you parse it to a value, and if it's neither you repeat the loop and ask for an input again.

#10 Dragonsoulj   Crossbones+   -  Reputation: 2127

Like
0Likes
Like

Posted 25 June 2012 - 04:16 AM

The issue is that you are storing a 'q' into an integer, I'm assuming it tries to give the numeric value of character q, which will cause problems searching unless you don't use a number that high. The only options i can think are to either do two inputs, one to continue/quit and then if they continue, take a value. The other, is the string parsing example where you see if it is a 'q' and if not, translate the number to an int.

#11 japro   Members   -  Reputation: 887

Like
1Likes
Like

Posted 25 June 2012 - 04:50 AM

something like:
for(;;)
{
   std::string input;
   std::cout << "Enter value(q to quit): ";
   std::cin >> input;
   if(input[0] == 'q')
	  break;
   int value = std::atoi(input.c_str());
   //...
}


#12 SiCrane   Moderators   -  Reputation: 9671

Like
0Likes
Like

Posted 25 June 2012 - 06:21 AM

The issue is that you are storing a 'q' into an integer, I'm assuming it tries to give the numeric value of character q....

No, if the input cannot be parsed as an integer the extraction operator will just set the fail bit on the stream to signal a failed parse.

#13 Sid_TheBeginner   Members   -  Reputation: 157

Like
0Likes
Like

Posted 25 June 2012 - 08:42 AM

I've given up this program for now. I curse my speed. I'm just rushing Posted Image I'm gonna come back to it tomorrow and try to reconstruct the whole program. On some other forum I'm told that this is not even binary search Posted Image Thanks a lot for the help. Posted Image

Edited by Sid_TheBeginner, 25 June 2012 - 08:43 AM.


#14 Brother Bob   Moderators   -  Reputation: 8611

Like
0Likes
Like

Posted 25 June 2012 - 08:51 AM

I wasn't even looking at the search algorithm itself since the question was focusing on how to parse the input. But that's correct, it's not a proper binary search.

You can easily see that from the output (well, if you look for it in the first place :) ), because you have 23 elements in the array so a binary search will never look at more than 5 elements before finding the element or concluding that it doesn't exist. If you have 6 or more array lookups to compare with the value, there is something wrong with the search method.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS