Hi, i can't seem to be able to fix this loop ! :( I have tried to use cin.clear() but it still does not work. It goes in an infinite loop. Any ideas? Thanks in advance!

#include <iostream>
#include <cstring>
#include <string>

using namespace std;

int main()
{
	char phrase1[80];
	char phrase2[80];	
	int words1 = 1;
	int words2 = 1;
	int length1, length2;
	char answer;
	do
	{	
		cout << "Please enter a phrase " ;
		cin.get(phrase1, 80);	
		cin.ignore(10, '\n');
		cout << "Please enter another phrase " ;
		cin.get(phrase2, 80);
		length1 = strlen(phrase1);
		for (int size = 0; length1 > size; size++)
				{                 
				if(phrase1[size] == ' ' && phrase1[size-1] != ' ') 
				words1++;
				 }
				if(phrase1[0] == ' ')
					words1--; 	
		cout << "\nThe first phrase " << '"' << phrase1 <<'"' << " has " << words1
			<< " word(s) and is " << strlen(phrase1) << " characters long. " << endl;
		
		length2 = strlen(phrase2);
		for (int size = 0; length2 > size; size++)
				{                 
				if (phrase2[size] == ' ' && phrase2[size-1] != ' ') 
				words2++;
				 }
				if(phrase2[0] == ' ')
					words2--; 	
		cout << "\nThe second phrase " << '"' << phrase2 <<'"' << " has " << words2
			<< " word(s) and is " << strlen(phrase2) << " characters long.\n " << endl;
		if(strcmp(phrase1, phrase2) < 0)
			cout << '"' << phrase2 << '"' << " is greater than " << '"' << phrase1 << '"'<< "." << endl;
		else if(strcmp(phrase1, phrase2) > 0)
			cout << '"' << phrase1 << '"' << " is greater than " << '"' << phrase2 << '"' << "." << endl;	
		else 
			cout << '"' << phrase1 << '"' << " and " << '"' << phrase2 << '"' << " are equal in length.\n " << endl;
	
	cout << "\nContinue y/n? "; 
	cin >> answer;
	
	}while(toupper(answer) == 'Y');
			 	
system("pause");
return 0;

}
Salem commented: 10 posts, and no code tags - shameful -4
Member Avatar for iamthwee

Well that gets the loop done:-

#include <iostream>
#include <cstring>
#include <string>

using namespace std;

int main()
{
   char phrase1[80];

   int words1 = 1;
   int words2 = 1;
   int length1, length2;
   char answer ;

   do
   {
      cout << "Please enter a phrase " ;
      cin >> phrase1;

      cout << "\nContinue y/n? ";
      cin >> answer;

   }
   while ( answer == 'y' );

   system ( "pause" );
   return 0;

}

See if you can modify it with your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.