Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

When I try to compile my code this error pops out:

invalid conversion from 'int' to 'const char*'

My task is to write a program that calculates the sum of numbers with odd index.

Please don't roast me (I'm learning how to code in c++), and give some tips how to fix it and get my code working.

#include <bits/stdc++.h>
#include <cmath>
#include <string>
#include <iostream>

using namespace std;

int main()
{
    string text;
    cin >> text;

    int len = text.length(), sum = 0, number = 0, a = 0;

    for (int i = len; i > 0; i++) {
        a = text[i];
        if (i % 2 == 1) {
            number = atoi(a);
            sum = sum + number;
        }
    }
    cout << sum;

    return 0;
}
question from:https://stackoverflow.com/questions/65831210/invalid-conversion-from-int-to-const-char-fpermissive-beginner

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
748 views
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...