好吧,我几乎得到了这个工作,但我坚持b。的第二部分,让它显示在该阵列中的位置的单词。这是我需要做的事情的清单。打印内容的字符串向量
读取从文本文件50个字转换为一个字符串数组
程序将使用随机数为:
A.-它将产生用于2和7之间的随机数在句子中选择要使用的单词
b.-它将生成一个随机数字,用于选择单词。该数字将介于0和49之间,因为这些是数组中字的位置
它将在屏幕上显示该句子。
谢谢你的时间提前的任何建议
#include <string>
#include <iostream>
#include <fstream>
#include <time.h>
#include <stdlib.h>
#include <vector>
using namespace std;
int main() {
ofstream outFile;
ifstream inFile;
string word;
vector <string> words;
srand(time(0));
int Random2 = rand() % 7 + 1;
inFile.open("words.txt");
if (!inFile.is_open()) { //tests to see if file opened corrected
exit(EXIT_FAILURE);
}
while (getline(inFile, word)) { //Puts file info into string
words.push_back(word);
}
for (int i = 0; i < Random2; i++) {
int Random1 = rand() % 49 + 1;
cout << words[Random1] << endl;
}
cin.get();
}
我不太明白。第一个随机数是句子中单词的数量? –
欢迎来到堆栈溢出。 1)孤立地开发新功能*。 2)良好的缩进就像好的修饰。 3)数组和矢量不是一回事。 4)“阵列中那个位置的单词”没有多大意义。 5)在使用它之前,确保你了解'getline'。 6)你从[1,...,50]中抽取一个随机数。 – Beta
你的实际问题是什么?除了你以外,你看起来没问题的代码是从1到49而不是0到49的随机单词。 –