Do-While Loop is not working as intended

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//marriage test

// Objective:
// If the user inputs 'M' or 'S', it outputs a given sentence. Otherwise, it keeps
// prompting you to get valid input. 

#include <iostream>
using namespace std;

int main()
{
	//declare your variables
	char marriage ;

	//process
	do
	{
		cout << "Pease enter your marriage status (S: Single, M: Married): ";
		cin >> marriage ;
	}while (marriage != 'M' || marriage != 'S');
	

	if (marriage == 'S'|| marriage == 's')
	{
		cout << "You're single!" << endl;
	}
	else if (marriage == 'M' || marriage == 'm')
	{
		cout << "You're married!" << endl;
	}

	//terminate the program
	return 0;
}



The program is supposed to be a very simple test to see if a user typed in an 'S' or an 'M', but as it is, it never exits the loop (even if an 'M' or 'S' is typed). Without the loop, the program can tell which char is typed in, so I know the loop is the problem. What am I missing?

Thanks,
Mike
Think about your loop logic there.

You're currently asking your loop to run as long as marriage isn't equal to M or marriage isn't equal to S.

That condition will always evaluate to true.

You want:
 
while( marriage != 'M' && marriage != 'S' );
Last edited on
Ah! Thanks so much, iHutch105. Didn't see that until now.

Much appreciated! :)
Hi, there.
Very interesting. The problem originates in line 20, testing the while condition. Say you type 'M'. The variable marriage gets the value 'M'. What about the while condition? It's true!, because marriage is 'M' (but it's not 'S') it 's true!!!. As a matter of fact, it will always be true, because a char can't have two values at the same time, so it will always make this true (it will be different from 'M', or different from 'S', or, in most cases, different from both).

You could use:

 
}while (marraige != 'M' && marriage != 'S');


Which may be counter-intuitive, but works if you follow each possibility.

This way, the while condition controlling the loop will become false wheter you input 'M' or 'S'.

marriage | marriage != 'M' | marriage != 'S | marriage != 'M' && marriage != 'S'
........z........|........... true...........|......... true ..........|..........true............................. <----------- repeats the loop
........b........|........... true...........|......... true ..........|..........true............................. <----------- repeats the loop
........a........|........... true...........|......... true ..........|..........true............................. <----------- repeats the loop
........x........|........... true...........|......... true ..........|..........true............................. <----------- repeats the loop
........M........|........... false........|......... true ..........|.........false ........................... <----------- gets out of the loop
........S........|........... true...........|......... false.........|.........false ........................... <----------- gets out of the loop

In fact, you should use:

 
}while (marriage != 'M' && marriage != 'm' && marriage != 'S' && marriage != 's');


to avoid case sensitivity.

Good luck!
Thanks so much, Marcos Modenesi! I was sitting over trying to figure out the logic of why is should be && instead of || and you totally clarified that for me. I agree that it is a bit counter-intuitive at first, but I see the logical connection now.

Thanks so much again. Much appreciated!

-Mike
Topic archived. No new replies allowed.