所以我有一个符号数组,其中有另一个符号数组(如果我没有弄错的话)。我需要做的是更改第三个元素(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;
}
无法使用
您需要将转换不起作用
int a = atoi(words[2]); // make sure words[2] is null terminated