Need help with compile issue c++14

I got the below error from below code. any idea what is wrong here?

main.cpp: In function ‘std::string i2r(unsigned int)’:
main.cpp:37:59: error: could not convert ‘'I'’ from ‘char’ to ‘std::basic_string’
array<string, 7> r_d = {'I',"IV","V","IX","X","XL","L"};



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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <bits/stdc++.h>
#include <iostream>
#include <array>
#include <vector>
#include <string>
#include <sstream>
#include <map>
#include <unordered_map>
#include <algorithm>
using namespace std;

string ltrim(const string &);
string rtrim(const string &);

unsigned int r2i( const string& r)
{
    unordered_map<char, unsigned int> r2i_lut = { {'I', 1},{'V', 5},{'X', 10},{'L', 50} };
    if(r.size()==0) return 0;
    if(r.size()==1) return r2i_lut[r[0]];
    unsigned int res =0;
    for(unsigned int i =0; i<r.size(); ++i)
    {
        if((i<r.size()-1) && (r2i_lut[r[i+1]] > r2i_lut[r[i]]))
        {
            res += (r2i_lut[r[i+1]] - r2i_lut[r[i]]);
            i++;
        }
        else res += r2i_lut[r[i]];
    }
    return res;
}

string i2r(unsigned int n)
{
    string res;
    array<unsigned int, 7> r_n = {1,4,5,9,10,40,50};
    array<string, 7> r_d = {'I',"IV","V","IX","X","XL","L"};
    //unsigned int i = 7;
    for(unsigned int i=6; (i>=0) && (n>0); --i)
    {
        unsigned int d = n / r_n[i];
        n = n % r_n[i];
        for(unsigned int j = 0; j<d; ++j) res+=r_d[i];
    }
    return res;
}

vector<string> tokenize(const string& s, const char c)
{
    vector<string> res;
    auto ss = stringstream(s);
    string temp;
    while(getline(ss, temp, c)) res.push_back(temp);
    return res;
}

vector<string> getSortList(const vector<string>& names) 
{
    vector<string> res;
    map<string, vector<unsigned int>> d;
    for(const auto& e: names)
    {
        auto s = tokenize(e, ' ');
        d[s[0]].push_back(r2i(s[1]));
    }
    
    for(auto& name : d)
    {
        sort(name.second.begin(), name.second.end());
        for(const auto & r: name.second)
        {
            res.push_back(name.first + " " + i2r(r));
        }
    }
    return res;
}

string str_to(const vector<string>& s)
{
    string res="";
    for(const auto& e:s) res += e + "\n";
    return res;
}

int main()
{
    ofstream fout(getenv("OUTPUT_PATH"));

    string names_count_temp;
    getline(cin, names_count_temp);

    int names_count = stoi(ltrim(rtrim(names_count_temp)));

    vector<string> names(names_count);

    for (int i = 0; i < names_count; i++) {
        string names_item;
        getline(cin, names_item);

        names[i] = names_item;
    }

    vector<string> res = getSortList(names);

    for (int i = 0; i < res.size(); i++) {
        fout << res[i];

        if (i != res.size() - 1) {
            fout << "\n";
        }
    }

    fout << "\n";

    fout.close();

    return 0;
}

string ltrim(const string &str) {
    string s(str);

    s.erase(
        s.begin(),
        find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
    );

    return s;
}

string rtrim(const string &str) {
    string s(str);

    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
        s.end()
    );

    return s;

}

Last edited on
Do you know the difference between a char constant and a string constant?

Hint a string uses " ".

By the way you really should start using meaningful variable names. Single letter names should be reserved for loop control variables, or variables of very local scope because they make searching and following very difficult.

You need to learn to compile early and often to reduce the number of errors/warnings you are dealing with at one time.

Thanks for your suggestion.
I found the mistake was in line 37
Topic archived. No new replies allowed.