No clue how to deal with this error

Aug 15, 2018 at 3:52pm
I'm trying to make a program which gives me all the words that have a certain number of letters, it gives me the error: "invalid conversion from 'char' to 'const char*' [-fpermissive]". I have googled and searched around for a solution and have read the references but I don't know what to change.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <fstream>
#include <math.h>
#include <string.h>


using namespace std;

int main()
{
    ifstream f("words.txt");
    int nr_words;
    f >> nr_words;            //first line of the txt file is the nr of words
    char v[nr_words];

    for(int i=1; i<nr_words; i++)
    {
        f >> v[i];
        cout<<v[i]<<" ";
    }

    cout<<endl;
    cout<<endl;

    int nr_letters;
    cout<<"number of letters the word has: ";
    cin>>nr_letters;

    for(int i=1; i<nr_words; i++)
    {
        if(strlen(v[i])==nr_letters)
            cout<<v[i];
    }


    return 0;
}
Aug 15, 2018 at 4:12pm
The function strlen accepts an input parameter of type char-pointer.

v[i] is a char.

See the difference. It wants a char-pointer, you're passing it a char.

A char-pointer is not the same thing as a char.
Aug 15, 2018 at 4:13pm
Nothing you do here makes any sense.

char v[nr_words];
This creates an array of char. Each char can hold ONE letter. Yet you seem to be trying to store entire words in them. This makes no sense.
Aug 15, 2018 at 4:19pm
Ah, I see you had this problem before and you've ignored what I said last time: http://www.cplusplus.com/forum/beginner/238999/
Aug 15, 2018 at 4:40pm
At line 14 you define an array of single 'char'-types. but in line 19 you're probably try reading a whole c-string word into a single 'char' storage space!

You could expand your 'char' array at line 14 to a 2-dimensional type: char v[nr_words][32];

But be aware that using raw arrays for is error prone. c++ offers better alternatives. Here a suggestion by using std::vector and std::string objects:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <fstream>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    ifstream f("words.txt");
    if (!f) 
    {
        cerr << "Couldn't open file.";
        return 1;
    }

    int nr_words;
    f >> nr_words;

    vector<string> v(nr_words);

    for (int i = 0; i < nr_words; ++i) 
    {
        f >> v[i];
    }

    cout << "\n\n";

    size_t nr_letters;
    cout << "number of letters the word has: ";
    cin >> nr_letters;

    for ( string & word : v)
    {
        if (word.length() == nr_letters)
            cout << word;
    }

    return 0;
}
Last edited on Aug 15, 2018 at 4:45pm
Aug 15, 2018 at 4:41pm
also, @holyheck, is there a reason you're trying to work with C-style strings? You're using C++ input/output operations, C++ file operations, and then suddenly C character arrays and related functions.

Many times when including with the angle brackets but providing a .h file, like #include <string.h> will bring in a file from the C standard lib (unless you have specific custom changes to your project's include directories). This can be made clearer by putting a "c" in front and removing the ".h", as in #include <cstring> or #include <cmath> . Readers now know you're specifically bringing in something from the C libs.

I'd try to avoid including C stuff altogether and use as many C++ libs as possible

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <fstream>
#include <string>  // C++ std::string

using namespace std;

int main()
{
    string file_name("words.txt");
    // Just slightly clearer to name your variable to show it's not just any stream but an input file stream
    ifstream ifs(file_name);
    if (!ifs)
    {
        cout << "Unable to open \""<<file_name<<"\".  Please check it exists and not in use.\n";
        return -1;
    }

    int nr_letters;
    cout << "Letters in word? ";
    cin >> nr_letters;


    int nr_words;
    ifs >> nr_words;  // It's cool that your file has this at the top, but it's not really needed

    string word;
    while (ifs >> word)
    {
        // Do stuff if word.size() is correct...
    }

    return 0;
}



Last edited on Aug 15, 2018 at 4:41pm
Topic archived. No new replies allowed.