|
-
November 17th, 2009, 01:52 AM
#1
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;
}
-
November 23rd, 2009, 03:14 AM
#2
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[])
-
November 23rd, 2009, 09:14 AM
#3
Re: making a string array of words in main.
Before post, make an effort yourself, try googling or search here.
When posting, give a proper description of your problem, include code* and error messages.
*All code should include code tags
-
November 23rd, 2009, 09:41 AM
#4
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|