-1

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;

}

Edited by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

Votes + Comments
10 posts, and no code tags - shameful
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by iamthwee
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.