making a string array of words in main.
I'm trying to put a list of words into an array and I need the array list to be located in the main function.
I also need to know how high the index goes on the string array.
getting error "cannot convert from 'std::string' to 'char'"
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream> //for external input, output files.
using namespace std;
const int Max_Words = 100;
int pickWord(string wordList[]);
int main()
{
string wordList[Max_Words];
for(int i = 0; i<100;i++){
cout << pickWord(wordList)<<endl;
}
}
//===================================
int pickWord(string wordList){
//1+rand()% Max_Words;
int i = 0;
//read the sample txt file
ifstream infile("C:\\Documents and Settings\\chillycobra\\Desktop\\PJ851words.txt"); //input
// if no infile then cerr error msg
if (!infile) {
cerr << "Error: cannot open sales.txt\n";//stays at top unlike cout
}
while(infile){
string word;
infile >> word;
wordList[i]= word;
i++;
if(i>Max_Words)
cerr << "too many words in list\n";
}
return i;
}
Re: making a string array of words in main.
You need to change the definition so that it matches with the declaration:
int pickWord(string wordList[])
Re: making a string array of words in main.
Re: making a string array of words in main.
I don't see any good reason for you to be calling pick_word multiple times in a for loop.
For that matter, I don't see how pick_word actually picks a word, so the function name is misleading.