Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

For Loop Countdown

countdown for loop loop

  • Please log in to reply
1 reply to this topic

#1 taggize

taggize

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 05 December 2010 - 06:46 PM

I've been working at modifying my program for a few hours now and find I'm stumped. I'm trying to create a display message for every time a user enters an incorrect letter that let's them know how many guesses they have remaining.

I created a for loop that counts down the numbers but I can't seem to get it right. Any suggestions on how to fix the issue would be appreciated. Here's the code ( the for loop is towards the bottom of the code):

#include <iostream>
#include <string>
using namespace  std;

int main()
{
	//declare the variables
	string origWord = "";
	string letter = "";
	char dashReplaced ='N';
	char gameOver ='N';
	int numIncorrect = 0;
	string displayWord = "-----";

	//get original word
	do //begin loop
	{
		cout << "Enter a 5-letter word in uppercase: ";
		getline(cin, origWord);
	}   while (origWord.length() != 5);
	//clear the system
	system("cls");

	//start guessing
	cout << "Guess this word: " <<
	displayWord << endl;
	while (gameOver == 'N')
	{
		cout << "Enter an uppercase letter: ";
		cin >> letter;

		//search for the letter in the original word
		for (int x = 0; x < 5; x += 1)
		{
			//if the current character matches
			//the letter, replace the corresponding
			//dash in the displayWord variable and then
			//set the dashReplaced variable to 'Y'
			if (origWord.substr(x, 1) == letter)
			{
				displayWord.replace(x, 1, letter);
				dashReplaced = 'Y';
			}   //end if
		}   //end for

		//if a dash was replace, check whether the
		//displayWord variable contains any dashes
		if (dashReplaced == 'Y')
		{
			//if the displayWord variable does not
			//contain any dashes, the game is over
			if (displayWord.find("-", 0) == -1)
			{
				gameOver = 'Y';
				cout << endl << "Yes, the word is "
					<< origWord << endl;
				cout << "Great guessing!" << endl;
			}
			else //otherwise, continue guessing
			{
				cout << endl << "Guess this word: "
					<< displayWord << endl;
				dashReplaced = 'N';
			} //end if
		}
		else //processed when dashReplaced contains 'N'
		{
			//add 1 to the number of incorrect guesses
			numIncorrect += 1;
			for (int numIncorrect = 9; numIncorrect >= 1; numIncorrect-- )
			{
				cout << endl << "Number of guesses remaining: "
					<< numIncorrect << endl;
			}

			//if the number of incorrect guesses is 10,
			//the game is over
			if (numIncorrect == 10)
			{
				gameOver = 'Y';
				cout << endl << "Sorry, the word is "
					<< origWord << endl;
			} //end if
		} //end if

	} //end while

	system("pause");
	return 0;
} //end of main function

  • 0

#2 Red Lightning

Red Lightning

    CC Lurker

  • Just Joined
  • Pip
  • 7 posts

Posted 05 December 2010 - 11:31 PM

At first glance,the problem seems to be with the foll part of the code:

for (int numIncorrect = 9; numIncorrect >= 1; numIncorrect-- )
			{
				cout << endl << "Number of guesses remaining: "
					<< numIncorrect << endl;
			}

Using a for loop here will not give you the desired output...Use the foll line of code instead:
else //processed when dashReplaced contains 'N'
		{
			//add 1 to the number of incorrect guesses
			numIncorrect += 1;
				cout << endl << "Number of guesses remaining: "<< 10-numIncorrect << endl;
			}


Hope that was the error you were pondering over...
  • 0





Also tagged with one or more of these keywords: countdown, for loop, loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download