Loop not functioning

I have everything set ready to go except one thing and I think I need another set of eyes to solve this one. The loop will not exit even though I have countered it within in the loop. I have made comments next to the potential problems. Please take a look and let me know your thoughts.

void DayOfTheWeek::getDay()
{
string Day[7] = {"sun","mon","tues","wed","thur","fri","sat"};
string Days[7] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
string choice;
int dayFound = 0;
int x = 0;
int y = 0;
bool dayCount = false;

cout << "\n What day are you looking for? " << endl << endl;
cout << " sun - Sunday\n";
cout << " mon - Monday\n";
cout << " tues - Tuesday\n";
cout << " wed - Wednesday\n";
cout << " thur - Thursday\n";
cout << " fri - Friday\n";
cout << " sat - Saturday\n";
cin >> choice;

for (x;x< 7;x++)
{
if(Day[x] == choice)
{
dayFound = x;
dayCount = true;
break;
}
}

while(y < 7 && dayCount == false) //here is problem
{
cout << "Invalid entry. Please try again";
cout << "\n What day are you looking for? " << endl << endl;
cout << " sun - Sunday\n";
cout << " mon - Monday\n";
cout << " tues - Tuesday\n";
cout << " wed - Wednesday\n";
cout << " thur - Thursday\n";
cout << " fri - Friday\n";
cout << " sat - Saturday\n";
cin >> choice;

for (x;x< 7;x++)
{
if(choice == Day[x])
{
dayFound = x;
dayCount = true;
break;
}

} //dayCount does not stay true and re-enters loop??? thoughts??
y++;
}

if(x < 7)
{
day = Days[dayFound];
}
}
Use [code][/code] tags next time...I'm posting this so I can see the code better:
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
void DayOfTheWeek::getDay()
{
	string Day[7] = {"sun","mon","tues","wed","thur","fri","sat"};
	string Days[7] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
	string choice;
	int dayFound = 0;
	int x = 0;
	int y = 0;
	bool dayCount = false;

	cout << "\n What day are you looking for? " << endl << endl;
	cout << " sun - Sunday\n";
	cout << " mon - Monday\n";
	cout << " tues - Tuesday\n";
	cout << " wed - Wednesday\n";
	cout << " thur - Thursday\n";
	cout << " fri - Friday\n";
	cout << " sat - Saturday\n";
	cin >> choice;

	for (x;x< 7;x++)
	{
		if(Day[x] == choice)
		{
			dayFound = x;
			dayCount = true;
			break;
		}
	}

	while(y < 7 && dayCount == false) //here is problem
	{
		cout << "Invalid entry. Please try again";
		cout << "\n What day are you looking for? " << endl << endl;
		cout << " sun - Sunday\n";
		cout << " mon - Monday\n";
		cout << " tues - Tuesday\n";
		cout << " wed - Wednesday\n";
		cout << " thur - Thursday\n";
		cout << " fri - Friday\n";
		cout << " sat - Saturday\n";
		cin >> choice;

		for (x;x< 7;x++)
		{
			if(choice == Day[x])
			{
				dayFound = x;
				dayCount = true;
				break;
			}

		} //dayCount does not stay true and re-enters loop??? thoughts??
		y++;
	}

	if(x < 7)
	{
		day = Days[dayFound];
	}
}
Hm, it looks like it should work...are you sure that dayCount is being set to true? Try debugging and stepping through it.
You don't set x back to zero on your second loop, but let me restructure your code to avoid repetition:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
do{
	cout << "\n What day are you looking for? " << endl << endl;
	cout << " sun - Sunday\n";
	cout << " mon - Monday\n";
	cout << " tues - Tuesday\n";
	cout << " wed - Wednesday\n";
	cout << " thur - Thursday\n";
	cout << " fri - Friday\n";
	cout << " sat - Saturday\n";
	//Don't use std::cin >>. Trust me, std::getline() will save you many headaches.
	std::getline(std::cin,choice);

	for (x=0;x<7 && !dayCount;x++){
		if(Day[x] == choice){
			dayFound = x;
			dayCount = true;
		}
	}
}while (!dayCount);


By the way, did you know you can do this?
1
2
3
4
for (int a=0;a<10;a++)
	std::cout <<a<<std::endl;
for (int a=0;a<10;a++)
	std::cout <<a<<std::endl;
Wow! A little miscue such as setting x = 0 was the problem. I can't thank you all enough for the assistance. That one hiccup was holding everything back. Also, Helios thanks for the example on the for loops; that is definitely an easier way than me writing everything out. Again, thank you and have a great day!
Topic archived. No new replies allowed.