Jump to content
  • Advertisement
Sign in to follow this  
jordi_0071

for loop doesnt work as i want

This topic is 4731 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey, i made this code:
#include <iostream>
using namespace std;

int main()
{
	int iCount;
	char cAgain;
	
	for(iCount = 10; iCount >= 0; cAgain =!"N", iCount--)
	{
	cout << iCount << "\n";
	
	cout << "\nDo it again?(Y/N): " << endl;

	cin >> cAgain;
	
	}

	system("pause");

	return 0;
}

but if you run it it shows: 10 Do it again?(Y/N): Y 9 Do it again?(Y/N): N 8 so with the N and Y it doesnt work and the number doesnt work to. it should be doing this: 10 9 8 7 6 5 4 3 2 1 0 Do it again?(Y/N): Y 10 9 8 7 6 5 4 3 2 1 0 etc, and if you press N or something else it should exit the program. how do i do this right?

Share this post


Link to post
Share on other sites
Advertisement
#include <iostream>
using namespace std;

int main()
{
char cAgain;

do
{
for(int iCount = 10;iCount >= 0;iCount--)
{
cout << iCount << endl;
}

cout << "\nDo it again?(y/n): " << endl;
cin >> cAgain;
}
while(cAgain != 'n');

return 0;
}

Share this post


Link to post
Share on other sites
If you think about what you want, you should realise that the program design actually contains two loops:

- do
- print the numbers 10 to 0
- ask the user to do it again


Also, a character in C/C++ is enclosed by single quotes; double quotes signify a string value (in this case of length one). So, writing a program that does that:

bool again = true;

// Always do the number loop at least once. A do loop always runs once guaranteed.
do
{
// Print the numbers
for ( int i = 10; i >= 0; i-- )
std::cout << i << std::endl;

// Ask the user to do it again
char input;
std::cout << "Do it again (Y/N)?" << std::endl;
input << std::cin;
bAgain = (input == 'Y' || input == 'y');
} while( bAgain );

Share this post


Link to post
Share on other sites
mike25025 fixed the problem already ...

What you are doing is assigning the negative "N" to cAgain.

Maybe that is what you meant?
for(iCount = 10; iCount >= 0 && cAgain != 'N'; iCount--)
the for loop is left if iCount is < 0 or if cAgain is 'N' ...
Of course if you want to ask only once each turn you need to separate the loops as mike25025 did it.

CTar: the body is executed if the boolean expression is true.
It should be true if cAgain is not 'N' AND if iCount is >=0.
Not || !?
Also the != operator was wrong: =! ...

Share this post


Link to post
Share on other sites
i got it working now:


#include <iostream>
using namespace std;

int main()
{
char cAgain;

do
{
for(int iCount = 10;iCount >= 0;iCount--)
{
cout << iCount << endl;
}

cout << "\nDo it again?(y/n): ";
cin >> cAgain;
cout << endl;
}
while(cAgain == 'y' || cAgain == 'Y');

cout << "You pressed " << cAgain << " and that means the program ends here." << endl;


system("pause");

return 0;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!