0

I've been doing a program that recieves N number of words that are entered by user. All letters that are contained in those words should be printed only once.

#include <iostream>
#include <cstdlib>
#include <ctype.h>
#include <string>

using namespace std;

int main(){
    int N=0;
    string allCharacters;

    while(N<=0){

        cout << "Enter number of words you would like to type (positive number):";

        cin>>N;
        if(cin.fail())
        {
            cin.clear();
            cin.ignore(256,'\n');
            N=0;
        }
    }   

    for(int i=0; i<N; i++)
    {
        string input;
        cin>>input;

        for(int j=0; j<input.size(); j++)
        {
            if(allCharacters.find(tolower(input[j])) == string::npos) // if not found
                allCharacters.append(tolower(input[j])); // ERROR HERE
        }
    }

    for(int i=0; i<allCharacters.size();i++)
    {
        cout << allCharacters[i]<<endl;
    }

    return 0;
}

The error is: Line 33: [Error] invalid conversion from 'int' to 'const char*' [-fpermissive]"

But I do not understand it. I entered a string called "input" and should check each character in that string, but it says that input is int?

DylanD
  • 1
  • 3
  • where do you get that error? Which line? – Joe Mar 02 '18 at 19:24
  • The error message also had **a line number**. Where in this code did the error occur? – Pete Becker Mar 02 '18 at 19:24
  • Oh, I'm sorry. It says line 33."if(allCharacters.find(tolower(input[j])) == string::npos) // if not found allCharacters.append(tolower(input[j]));" – DylanD Mar 02 '18 at 19:26
  • Also if the error was for a particular line, you could have taken just a few more minutes to put together a 3 or 4 line `main` function demonstrating the error. Entering the number of words to read is not important to addressing the issue. – PaulMcKenzie Mar 02 '18 at 19:26
  • I think this is what you are looking for: [https://stackoverflow.com/questions/1472048/how-to-append-a-char-to-a-stdstring](https://stackoverflow.com/questions/1472048/how-to-append-a-char-to-a-stdstring) – Victor Padureanu Mar 02 '18 at 19:31
  • [mcve] example [here](https://www.ideone.com/w7PllH) – PaulMcKenzie Mar 02 '18 at 19:32
  • `tolower` takes an int and outputs an int. – stark Mar 02 '18 at 19:33
  • @VictorPadureanu - yeah. It works now. Thank you. But I still don't get what was the problem in my above code? Hmm... – DylanD Mar 02 '18 at 19:34
  • Instead of `append`, try `allCharacters.push_back(tolower(input[j]));` – Wander3r Mar 02 '18 at 19:35
  • As mentioned tolower() returns an integer, your call to append() expects a char, compiler throws error. look here: https://stackoverflow.com/questions/25963248/toupper-returns-integer-rather-than-char – Nicko Po Mar 02 '18 at 19:36
  • @DylanD -- [Look at the documentation](http://en.cppreference.com/w/cpp/string/basic_string/append). You don't see a version of append that takes an `int` as an argument. You do have one that takes an `size_type` and then a `char`, but that is not what you used. – PaulMcKenzie Mar 02 '18 at 19:37
  • Oh, so tolower returns and int (ASCII) of character that is being checked and append does not accept that int type. Is that right? @PaulMcKenzie – DylanD Mar 02 '18 at 19:45
  • @DylanD The `append` doesn't even accept a `char` type as a single parameter. Look again at the documentation. If you have only a single character to append, then the `append()` version you should have used is `append(1, tolower(input[j]));` That version takes an integer as a count argument, and then the single character. – PaulMcKenzie Mar 02 '18 at 21:28

0 Answers0