0

I want to type this form in my program: s1[k[I]], but it doesn't let me. How can I fix this? Here's my code:

#include <iostream>
using namespace std;
int main(){
    int N;
    cin>>N;
    string s1[N];
    for(int k=0;k<N;k++)
    {
        cin >> s1[k];
    }
    int counter=0;
    for(int k=0;k<N-1;k++)
    {
        for(int i=0;i<s1[k].size();k++)
        {
            if(s1[k[i]] == s1[k[i]])
            {
                cout << s1[k][i] << endl;
            }
        }
    }
    return 0;
}
6
  • Please don't spam different languages in the tags. This looks like C++, so I'm going to remove the others Commented Jan 29, 2021 at 16:11
  • 3
    What is k[i] supposed to do? Ask yourself: What is the type of k? Commented Jan 29, 2021 at 16:11
  • What should k[i] mean, when k is declared as int k=0;? Commented Jan 29, 2021 at 16:12
  • 1
    k is not an array so s[k[i]] makes no sense, did you mean s1[k][i] like you wrote on the next line? Commented Jan 29, 2021 at 16:12
  • 1
    Another question to ask yourself, whatever you actually meant by s1[k[i]] when would it ever not be the case that s1[k[i]]==s1[k[i]]? You are compaing to identical expressions with each other, that's always going to be true. Commented Jan 29, 2021 at 16:16

2 Answers 2

0

Using the index ([]) operator is generally for arrays. But in this statement: if(s1[k[i]]==s1[k[i]]), k is an int, not an int[]. So how can you index an integer?

Additional: Don't use using namespace std; as it isn't a good practice.

Sign up to request clarification or add additional context in comments.

2 Comments

@S.M. Yeah geez how did I not see it. Thanks for pointing out! I gotta make more coffee ;)
Thanks, for your answer
0

Looks like you tried to join different ideas that don't realy want work together, mixing char arrays and std::strings can be done but is a no good idea because is messy.

As you can see in the example, use the std::string as intended and that's it.

#include <iostream>

int main(){
    int N, i;

    std::cout << "Add a number -> ";
    std::cin  >> N;

    std::cout << "Add a string -> ";
    std::string string;
    std::cin >> string;


    // recomended method
    std::cout << "From std::string -> " << N << " | "  << string << std::endl;


    char * char_vec = new char [string.length()];
    memcpy(char_vec, string.c_str(), sizeof(char) * string.length());

    std::cout << "From array -> " << N << " | ";
    for(i = 0; i < string.length(); i++){
        std::cout << char_vec[i];
    }
    std::cout << std::endl;
    delete [] char_vec;


    return 0;
}

1 Comment

Thanks, for your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.