-2

Im trying to compare the character count of a string with the index elements of an array, but am having trouble. For example, if the userInput equals XX, the output should be:

XX is not in the array at position 0.

XX is in the array at position 1.

XX is not in the array at position 2.

arr[] = {"X", "XX", "XXX"};
string userInput;
cin >> userInput;

    for (int i = 0; i < userInput.length(); i++)
{
    if (userInput[i] == arr[i])
    {
        cout << userInput << " is in the array at position " << i << endl;
    }
    else
    {
        cout << userInput << " is not in the array at position " << i << endl;

Im recieving this error and am not sure how to fix it. Im fairly new to programming so any help would be great. Thank you.

Invalid operands to binary expression ('int' and 'string' (aka 'basic_string, allocator >'))

Mludbey
  • 1
  • 1
  • 1
  • 1
  • Sorry, should have mentioned. I have: using namespace std; at the beginning of my code. – Mludbey Aug 15 '16 at 10:08
  • 1
    @Mludbey The right tool to solve such problems is to use your debugger, but not to ask at Stack Overflow before you did so. Tell us all your observations you made when inspecting your code stepping through line by line in 1st place. Also you might want to read [**How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)**] At least leave us with a **[MCVE]** that reproduces your problem. (This is a personal stock comment provided by πάντα ῥεῖ™) – πάντα ῥεῖ Aug 15 '16 at 10:10
  • I'm sorry I misread and miinterpreted your program.You have `arr[]` with no type in the beginning that threw me off track. Is this `string arr[]`? If so you just need to say `if (userInput == arr[i])` instead of what you have. – n. m. could be an AI Aug 15 '16 at 10:51

2 Answers2

2
arr[] = {"X", "XX", "XXX"}; 

Assuming the above array is defined as a string.

cin >> userInput;

Must be replaced by

getline(cin,userInput)

As userInput is string. The conditional statement

if (userInput[i] == arr[i])

Should be replaced with

if (userInput == arr[i])

Otherwise, you will be comparing the char of string userInput an index i

Finally, as a whole this is how your code should look

string arr[] = {"X", "XX", "XXX"};

string userInput;
getline(cin,userInput);
for (int i = 0; i < userInput.length(); i++)
{
    if (userInput == arr[i])
    {
        cout << userInput << " is in the array at position " << i << endl;
    }
    else
    {
        cout << userInput << " is not in the array at position " << i << endl;
    }
}
ZerosAndOnes
  • 1,083
  • 1
  • 13
  • 25
1

Your problem is that you are comparing each character of the input with each string in the array. userInput[i] gives you the character in userInput at position i (starting from 0); whereas arr[i] gives you the string in arr at position i (also starting at 0). You'll also get another error (even if it does work) if you accidentally try to access arr[3] which doesn't exist. Use this instead:

#include <iostream>
#include <string>       // Used for std::getline()

// Don't use 'namespace std' - it's bad practice

int main()
{
    int arrSize = 3;    // Now we know the size of the array, we won't exceed arr[2]
    std::string arr[3] = { "X", "XX", "XXX" };
    std::string input;

    std::cout << "Enter input: ";   // A propmt would be useful
    std::getline(std::cin, input);  // Much better way to get input

    for (unsigned int i = 0; i < arrSize; ++i)  // Now we won't exceed the array size
    {
        if (input == arr[i])    // Compare the input string (not character) with each string in arr[]
            std::cout << "Your input is at position " << i << std::endl;
        else
            std::cout << "Your input is not at position " << i << std::endl;
    }

    std::cin.ignore();      // Wait for user to press enter before exiting
    return 0;
}
otah007
  • 505
  • 1
  • 4
  • 17
  • This is helpful but i'm wanting to compare the input character with the array strings. For example, if the input = XXXLM, i want it to read up to XXX and ignore LM – Mludbey Aug 15 '16 at 10:45
  • @Mludbey You should have said so very explicitly at the beginning of your question. As it stands, it is not very clear what you want to compare. – n. m. could be an AI Aug 15 '16 at 11:50
  • @Mludbey It is also not clear why you want to ignore LM and not XLM or XXLM or just M. – n. m. could be an AI Aug 15 '16 at 11:55