I haven't programmed in some time and need some help here plz. First of all how are individual characters of a string are accessed? second do they start at 0 or 1? I'm probably confusing this with arrays please help me out :). This program will read in a line of text, add "[ b ]" before, add "[ /b ]" after and will replace every letter of the read in string with 1 or 2 letters or a symbol. I dont want any help with the structure of the code I need to get rid of the compiler error:
line 8
1. invalid conversion from `char' to `const char*'
2. initializing argument 1 of `std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'

int main (){
        string line1, line2;
        int numberofwords;
        cin >> line1;
        numberofwords = line1.length();
        line2 = "[b]";
        for(int i = 0; i = numberofwords; i++){
            line2 += converter(line1[i]);
        }
        line2 += "[/b]";
        cout << line2;
        system("pause");
        return 0;
}

Recommended Answers

All 6 Replies

you will have to post the function converter() because we have no idea what it does or what it returns.

I haven't programmed in some time and need some help here plz. First of all how are individual characters of a string are accessed? second do they start at 0 or 1? I'm probably confusing this with arrays please help me out :). This program will read in a line of text, add "[ b ]" before, add "[ /b ]" after and will replace every letter of the read in string with 1 or 2 letters or a symbol. I dont want any help with the structure of the code I need to get rid of the compiler error:
line 8
1. invalid conversion from `char' to `const char*'
2. initializing argument 1 of `std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'

int main (){
        string line1, line2;
        int numberofwords;
        cin >> line1;
        numberofwords = line1.length();
        line2 = "[b]";
        for(int i = 0; i = numberofwords; i++){
            line2 += converter(line1[i]);
        }
        line2 += "[/b]";
        cout << line2;
        system("pause");
        return 0;
}

Can't help with the compile error since you didn't post the converter function, but a string contains an array of characters and you access them like an array, and indexes always start at 0.

#include <iostream>
#include <string>
using namespace std;

int main ()
{
    string aString = "Hello World!";
    cout << aString[0]; // displays 'H'
    cout << aString[1]; // displays 'e'
    return 0;
}

This for(int i = 0; i = numberofwords; i++) is gonna cause problems - you continually set i to be numberofwords, not test i against it. Two strikes - you really need to use the less than operator, and if you did mean equality test, use two equal signs ( == ).

And logically, your variable numberofwords is really number_of_characters.

Your converter( ) function - what's it's parameter, a char or a char array?

string converter (string letter){
       if (letter == "À")
          return "A";
       else if (letter == "à")
          return "a";
.....
}

the whole thing is if / else if/ else if ..... else if.

EDIT: i changed the = to == in my for loop.
the passed variable "letter" can be a char, but the returned variable is sometimes 2 characters so it can't obviously

you can not pass a single character to that function because the function expects a string. Change the function parameter from std::string to char if you want to pass it a single character.

my typical mistake, the condition has to be true for the loop to work not false =/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.