CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2009
    Posts
    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;
    }

  2. #2
    Join Date
    Oct 2008
    Location
    Singapore
    Posts
    195

    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[])

  3. #3
    Join Date
    Jan 2003
    Posts
    615

    Re: making a string array of words in main.

    Use code tags.
    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

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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
  •  





Click Here to Expand Forum to Full Width

Featured