Below code tries to input multiple strings with white spaces and then I need to do comparisons among them. The problem I am facing is that, it is not able to input strings beyond first one. I suppose it the the 'enter' remaining in the input buffer causing this behavior i.e. to skip further input of strings. Any suggestion how to overcome this?
Refered: How to cin Space in c++?
Edited: Clear and Flush I have tried to but still same issue. I need to implement C style string functions, so can not use string class, the functions strcmp etc have to be implemented by me rather than using library functions.
int main()
{
char s[100];
char s1[100];
char s2[100];
char* sub;
struct countSpaces cs;
cout << "Enter a String : ";
cin.get( s, 100 );
std::cin.clear();
cs=count(s);
cout << s << " contains " << cs.letters << " letters and " << cs.spaces << " spaces" << endl;
cout << "Length of " << s << " is " << strlen(s) << endl;
cout << "Enter First String : ";
cin.get( s1, 100 );
std::cin.clear();
cout << "Enter second String : ";
cin.get( s2, 100 );
std::cin.clear();
if( strcmp(s1,s2) )
cout << s1 << " is equal to " << s2 << endl;
else
cout << s1 << " is not equal to " << s2 << endl;
return 0;
}
Output:
$ ./String
Enter a String : Herbert Schildt
Herbert Schildt contains 14 letters and 1 spaces
Length of Herbert Schildt is 15
Enter First String : Enter second String : is not equal to