-6

I wrote the following loop as a part of a program for my CS homework, however regardless of the input, the program keeps looping at this exact point. What am I doing wrong?

#include <iostream>
using namespace std;

char choice;
do
{
  cout << "Type 'c' for characters or type 'n' for numbers: ";
  cin >> choice;
}while (choice != 'c' || choice != 'n');

1 Answers1

3

A do-while statement loops as long as the while expression is true.

Your while expression is

choice != 'c' || choice != 'n'

In common English, that expression means

choice is not 'c' OR choice is not 'n'

That statement, logically, is always true. choice is always not one of those things.

In both English and C++, you would want to use and/&& in that expression.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180