How to use string as sentinels in while loop

Hello, everyone. This is my first time to post questions here. Thank you in advanced for any help and suggestions.

I tried to write a loop that reads strings from standard input where the string is either "land", "air", or "water". The loop terminates when "xxxxx" (five x characters) is read in. Other strings are ignored.

It complied; however, when I type in "xxxxx", which is the sentinels, the program doesn't stop, it turns out to a infinite loop. The problem should be in the declaration and while loop condition, but I don't know how to fix it. please help, thank you ahead!

Here is my code:
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
#include <iostream>
#include <cstring>
using namespace std;

int main() 
{

	string inputname;
	string land, air, water;
	string xxxxx;
	int numberOfLand, numberOfAir, numberOfWater;
	
	cout << "Please enter a word, either land, air, or water, and enter 'xxxxx' to quit the program." << endl;
	cin >> inputname;
	while (inputname != xxxxx)
	{
		
		if (inputname == land) {numberOfLand ++;}
		if (inputname == air) {numberOfAir ++;}
		if (inputname == water) {numberOfWater ++;}
		
		cin >> inputname;

	}

	cout << "The number of 'land' is " << numberOfLand << endl;
	cout << "The number of 'air' is "<< numberOfAir << endl;
	cout << "The number of 'water' is "<< numberOfWater << endl;

	return 0;
}

You have a variable xxxxx, but it has no value.

Better with this :

string Sentinel = "xxxxx";

while (inputname != Sentinel){

HTH
Just to add to what he said, if the sentinel string isn't going to be changing inside the program ever, I'd suggest declaring it as const (constant) as well. E.g.:

const string Sentinel = "xxxxx";

It's good style to do this, and it's far more self-explanatory in code seeing

while (inputname != Sentinel)

than seeing

while (inputname != "xxxxx")

and other references to arbitrary values littered throughout. Perhaps this is more important in larger applications where you'll be reusing certain arbitrary values throughout the code, but still a good habit to get into early on.
Last edited on
Good work Moeljbcp .

This also:
if (inputname == land) {numberOfLand ++;}

probably should be:

if (inputname == "land") {numberOfLand ++;}

If this is the situation, then you won't need the variables land, air water.

Otherwise, assign a value to the variables land, air water.

Initialising variables is one of the main sources of error in programming. One of my main source of error is failing to read things properly!! Ha har !!

Thank you TheIdeasMan and Moeljbcp.

Like you both said, I initialized the variables,

int numberOfLand =0, numberOfAir=0, numberOfWater=0;,

changed the declaration

const string sentinels = "xxxxx";,

and modified the while loop condition,

1
2
3
4
5
while (inputname != sentinels)
	{
		
		if (inputname == "land") {numberOfLand ++;}
		


now it works.

In the old code,
if (inputname == land) {numberOfLand ++;}
when cout the numberOfLand, it is 0, it didn't change although type in the word "land".

Thank you again!
One final thing - there should be no space between a variable name and the ++ operator.

As I said earlier if you have this:

if (inputname == "land") {numberOfLand++;}

there is no need for the variable land.
Topic archived. No new replies allowed.