0

I need to compare 2 (or more) string/char inputs and store them in a vector. For example, "I love mondays" and "I love mondays" should return true.

the problem is that for a string input, strcmp() does not work because it needs char. But if the input is the type char array it does not register the spaces and only stores the "I".

so I tried with cin.getline() but it did not work properly in a loop. so I used cin.ignore() in the loop and everything works fine! ...except now the inputs ignores the first character (result: " love mondays").

AjayKumarBasuthkar
  • 2,885
  • 2
  • 23
  • 27
  • I think it's time to invest in [a couple of good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). They should teach you how to compare [`std::string`](https://en.cppreference.com/w/cpp/string/basic_string) objects. They should hopefully also teach you how to [read whole lines](https://en.cppreference.com/w/cpp/string/basic_string/getline) from any input stream. – Some programmer dude Sep 26 '19 at 07:34
  • 2
    Please provide your code – RoQuOTriX Sep 26 '19 at 07:34
  • 1
    `getline` will work in a loop, compare will simply work with the `==` operator for strings. Please provide your code to let us see what you did wrong. – Klaus Sep 26 '19 at 07:35
  • Your main problem is that you're using `>>` to read user input elsewhere. Don't do that. – melpomene Sep 26 '19 at 07:36
  • 1
    It seems you have a question about std::cin. Perhaps you can change the title to reflect that. std::string comparison works by using str1 == st2; strcmp expects const char* arguments which a char array converts to. – gast128 Sep 26 '19 at 07:42

5 Answers5

2

You can read from input with getline(cin, string); read more: http://www.cplusplus.com/reference/string/string/getline/

You can simply compare two strings with == which returns true if the strings are equal and false otherwise.

Edit:

Here is a code with education purpose:

 

#include <iostream>

using namespace std;

int main()
{
    //try to always use string w/ C++
    std::string myStr1, myStr2;
    //read first string
    std::cout << "Input first string: ";
    std::getline(std::cin, myStr1);
    //read second string
    std::cout << "Input second string: ";
    std::getline(std::cin, myStr2);

    //compare strings, this is case sensitive
    if(myStr1 == myStr2 )
     std::cout << "The string are the same";
    else
     std::cout << "The string are not the same";

    return 0;
}

It's that easy, have fun learning.

1m2r3a
  • 76
  • 5
0

Try to always use strings instead of char arrays.

You can use .compare() in that case - http://www.cplusplus.com/reference/string/string/compare/

You can also use .c_str() to compare them using strcmp - http://www.cplusplus.com/reference/string/string/c_str/

Xellos
  • 153
  • 4
0

I thing you can use strings and strcmp with the string method c_str() that return a char array.

Mauricio Ruiz
  • 322
  • 2
  • 10
0

Maybe you like to start with this example:

#include <iostream>

int main()
{
    std::string a;
    std::string b;

    for(;;)
    {
        std::getline(std::cin, a);
        std::getline(std::cin, b);

        // terminate the loop if no more input comes in or
        // something goes wrong. To stop input, use CTRL-D on linux like shell
        if ( std::cin.eof() || std::cin.fail() ) break;

        std::cout << "First string: " << a << std::endl;
        std::cout << "Second string: " << b << std::endl;

        // Compare strings is quite simple in C++, use operator==
        if ( a == b )
        {
            std::cout << "Strings are equal" << std::endl;
        } else {
            std::cout << "Strings are NOT equal" << std::endl;
        }
    }
}

Further readings for operators provides from std::string: https://en.cppreference.com/w/cpp/string/basic_string/operator_cmp

Klaus
  • 24,205
  • 7
  • 58
  • 113
0

You can read a line of text at a time by using

#include <iostream>
#include <string>

using namespace std;
…
string readString;
getline(cin, readString);

You can find more about getline at cplusplus.com and cppreference.com.

You can simply compare two strings using the comparison operators declared for std::string.

#include <iostream>
#include <string>

using namespace std;

int main() {

  string myStr1, myStr2;
  // read first string
  cout << "Input first string: ";
  getline(cin, myStr1);

  // read second string
  cout << "Input second string: ";
  getline(cin, myStr2);

  // compare strings, this is case sensitive
  if (myStr1 == myStr2){
    cout << "The string are the same" << endl;
  }
  else {
    cout << "The string are not the same" << endl;
  }

  return 0;
}
Tarick Welling
  • 3,119
  • 3
  • 19
  • 44