0
嗨我正在为我的班级工作,并且遇到问题。我想要做的是从文件中获取单词列表,然后将一个随机单词放到一个char数组中,但我不完全确定我应该如何将文本形式的字符串数组转换为字符数组我的代码看起来是这样的目前将字符串数组转换为字符数组
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
int main(){
ifstream infile;
string words[25];
string wordss;
char cword[];
int index=0;
infile.open("c:\\words.txt)
while (infile>>words){
words[index]=words;
index=index+1;
}
}
现在本来我得只是简单地通过随机选择号码进行cword阵列的话阵列的一个随机字状cword =字[0]但没”工作。所以我想知道如何将从字符串数组中选择的单词转换为用于char数组?
'char cword []'对于数组声明是错误的。您必须在声明期间指定尺寸。然后你可以说'cword = words [0] .c_str()''char cword [];''const char * cword' – theAlias