-3

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
Community
  • 1
  • 1
Gaurav K
  • 2,864
  • 9
  • 39
  • 68
  • This is a duplicate of that and many others just by searching `[c++] cin buffer` or `[c++] cin skipping`. So -1 for no research effort. Also consider using `std::string` instead of C strings with possible buffer overflow problems. – Rapptz Apr 16 '13 at 06:06
  • @Rapptz I have already tried to flush the input stream (flush & clear) but still the same issue. I do not understand your -ve vote and also I have to implement C type string functions manually that's why not using C++ strings. Solution would be appreciated instead of -ve vote. – Gaurav K Apr 16 '13 at 06:11
  • 1
    [This is really basic](http://ideone.com/uQpXIp). – Rapptz Apr 16 '13 at 06:21

2 Answers2

1
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s1 = "";
    string s2 = "";

    cout << "Enter first string > ";

    getline(cin, s1);

    cout << "Enter second string > ";

    getline(cin, s2);

    if(strcmp(s1,s2))
        cout << s1 << " is equal to " << s2 << endl;
    else
        cout << s1 << " is not equal to " << s2 << endl;

    // copy string s1 into C-String str
    char * str = new char [s1.length()+1];
    std::strcpy (str, s1.c_str());

    return 0;
}
Khaled.K
  • 5,828
  • 1
  • 33
  • 51
  • What would this even do? Did you try running it before posting it? – Rapptz Apr 16 '13 at 06:16
  • I do not have to use string class, I need to implement null terminated string functions like string.h library. – Gaurav K Apr 16 '13 at 06:21
  • @Rapptz << it says: use string and getline instead of the mess he have above. @Gaurav K << `string a = "string"; a.c_str();` will return the null-terminated C-String you need from the string – Khaled.K Apr 16 '13 at 06:25
-1

std::cin.ignore(100,'\n'); did the trick.

bjb568
  • 11,089
  • 11
  • 50
  • 71
Gaurav K
  • 2,864
  • 9
  • 39
  • 68