提问者:小点点

将char**转换为int C++


所以我有一个符号数组,其中有另一个符号数组(如果我没有弄错的话)。我需要做的是更改第三个元素(codewords2/code)。这第三个元素肯定会是一个数字。我得把这个数字增加15%。因此,我需要将转换为

功能:

void create(char* str) {
    char** words = new char* [strlen(str)];
    int count = 0;
    for (char* part = strtok(str, " "); part != NULL; part = strtok(NULL, " ")) {
        words[count] = _strdup(part);
        count++;
    }

    cout << "\nNew sentence with edited values:" << endl;
    words[2] = words[2] + ((int)words[2] / 100) * 15; //the main problem as I guess
    for (int i = 0; i < count; i++) {
        printf("%s ", words[i]);
    }
    cout << endl;
    delete[] words;
}

完整代码:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <conio.h> 
#include <string.h>
using namespace std;

void create(char*);

void main() {
    const int maxLength = 100;
    char* str = new char[maxLength];
    cout << "Enter sentence:\n";
    cin.getline(str, maxLength);
    create(str);
    delete[] str;
}

void create(char* str) {
    char** words = new char* [strlen(str)];
    int count = 0;
    for (char* part = strtok(str, " "); part != NULL; part = strtok(NULL, " ")) {
        words[count] = _strdup(part);
        count++;
    }

    cout << "\nNew sentence with edited values:" << endl;
    words[2] = words[2] + ((int)words[2] / 100) * 15; 
    for (int i = 0; i < count; i++) {
        printf("%s ", words[i]);
    }
    cout << endl;
    delete[] words;
}

无法使用执行此操作。实际上,这是我在一所大学的作业,不使用是条件


共1个答案

匿名用户

您需要将转换为,如下所示。转换不起作用

int a = atoi(words[2]); // make sure words[2] is null terminated

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(char|转|换为|int|c++)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?