Hey guys,

This is only my second thread on DaniWeb and I am a beginner programmer.

My code looks like this: (really easy to understand and shouldn't need any further explanation besides the fact that I need help understanding why when i give an answer related to the 'else' loop, it says what is needed, but gets stuck in an infinite loop!)

#include <iostream>
#include <string>

using namespace std;

int main()	{
	
	char answer;

	cout << "do you think megan fox is hot? <y/n> "<<endl;
	cin>>answer;
	
	do {

	if (answer == 'y' || answer == 'Y')
		cout<<"You are the man"<<endl;
	else if (answer == 'n' || answer == 'N')
		cout<<"What is wrong with you?"<<endl;
	else
		cout<<"answer the question with a 'y' or 'n' please."<<endl;
	} while (answer == 'y' || answer == 'Y' || answer == 'n' || answer == 'N');

	cin.get();
	return 0;
}

Edited 5 Years Ago by __avd: Added [code] tags.

Please use CODE TAGS next time - forum rules.

Shouldn't it be while answer != a valid answer ? Though it seems redundant(and time consuming) to check the variable for all valid answers in the while statement and once again in the if statements.

You could try:

bool valid_answer = false;
while(!valid_answer) { 
  switch(answer) {
    case y:
      ...
      valid_answer = true;
      break;
    case n:
      ...
      valid_answer = true;
      break;
    default:
      ...
  }
}

Edited 5 Years Ago by MosaicFuneral: n/a

you can also use toupper() or tolower() in <cctype>
or use the falling-through behavior of the switch for 'Y' and 'y' etc.

Though, personally I would make a small function to test if it was valid.

you can also use toupper() or tolower() in <cctype>
or use the falling-through behavior of the switch for 'Y' and 'y' etc.

A label fall-through would probably be best for such a small piece of input.

Hey guys,

This is only my second thread on DaniWeb and I am a beginner programmer.

My code looks like this: (really easy to understand and shouldn't need any further explanation besides the fact that I need help understanding why when i give an answer related to the 'else' loop, it says what is needed, but gets stuck in an infinite loop!)

#include <iostream>
#include <string>

using namespace std;

int main()	{
	
	char answer;

	cout << "do you think megan fox is hot? <y/n> "<<endl;
	cin>>answer;
	
	do {

	if (answer == 'y' || answer == 'Y')
		cout<<"You are the man"<<endl;
	else if (answer == 'n' || answer == 'N')
		cout<<"What is wrong with you?"<<endl;
	else
		cout<<"answer the question with a 'y' or 'n' please."<<endl;
	} while (answer == 'y' || answer == 'Y' || answer == 'n' || answer == 'N');

	cin.get();
	return 0;
}

There are plenty of suggestions here about how to improve your code with respect to the number of comparisons performed, so I won't bother with that aspect of it.

Working with your existing code, I'd like to answer your question about the infinite loop. The loop is infinite because:

  1. You don't have any way to change the value of the variable the ending condition is based on.
  2. The way you wrote the condition causes the loop to repeat if the input is valid NOT when it's invalid.

Since this is a DO loop, you can handle your prompt a little differently from the traditional "priming read" configuration (what you currently have an attempt at). In a Do loop, you can actually do your priming read as part of the loop and save some code.

Move Lines 10 and 11 to Line 14, then change the conditions in the while to Logical ANDs with the NOT-EQUAL comparison.

do {
  cout << "do you think megan fox is hot? <y/n> "<<endl;
  cin>>answer;

  if (answer == 'y' || answer == 'Y')
    cout<<"You are the man"<<endl;
  else if (answer == 'n' || answer == 'N')
    cout<<"What is wrong with you?"<<endl;
  else
    cout<<"answer the question with a 'y' or 'n' please."<<endl;
} while (answer != 'y' && answer != 'Y' && answer != 'n' && answer != 'N');

As mentioned by the others, this isn't the most efficient way of handling the situation, but it does work. You'll want to take a look at what other types of control structures you are familiar with and see if you can come up with something that takes fewer steps to accomplish the same task. Don't forget, every operator you use is another step that needs to be completed. For example, your while statement at the end of your loop requires 8 steps to complete (4 boolean operators and 3 logical operators, plus the final comparison for the actual "while") even though it's only one statement. There are quicker ways to do that.

Edited 5 Years Ago by Fbody: n/a

This question has already been answered. Start a new discussion instead.