string_array[i] = my_str?

This is a discussion on string_array[i] = my_str? within the C++ Programming forums, part of the General Programming Boards category; Hey guys I have been trying to get make a simple program to store all of the names, in a ...

  1. #1
    NotSoAvgProgrammer
    Join Date
    Jul 2007
    Location
    Virginia, U.S.
    Posts
    57

    string_array[i] = my_str?

    Hey guys I have been trying to get make a simple program to store all of the names, in a file, in an array of strings. But I'm not quite sure how. I got to this till I couldn't figure it out any further. Is the code below on the right track, and if so, why am I getting the error?
    Code:
    // reading a text file
    #include <iostream>    //import
    #include <fstream>
    #include <string>
    using namespace std;    //namespace std
    
    char names[25][10];    //make an array of 25 strings, 10 chars long
    int i = 0;    
    
    int main () {
      string line;        //initiate a string
    
    
      ofstream people ("example.txt");         //open file
    
      people<< "Joe \nBob \nBill";       //write names
      people.close();          ///close file, so it can be opened in read mode
    
      ifstream myfile ("example.txt");      //read file
    
      if (myfile.is_open())        //check if file is there
    
      {
    
        while (! myfile.eof() )      // set up loop
    
        {
    
          getline (myfile,line);      //read line
          names[i] = line;         //put string in array<!--gets error-->
          i++;                           //sets array position
    
        }
        i = 0;
    
        while(i < 3)
        {
            cout<< names[i];           //reads array of strings
            i++;
        }
    
        myfile.close();       //closes file
    
      }
    
      else cout << "Unable to open file";       //If file is not there
    
      return 0;
    }

    Which gives the error
    Code:
    main.cpp: In function `int main()':
    main.cpp:30: error: incompatible types in assignment of `std::string' to `char*[50]'
    main.cpp:49:2: warning: no newline at end of file
    Process terminated with status 1 (0 minutes, 0 seconds)
    Why is it not allowing me to enter the string into the array?

    Thanks for any help,

    Joe

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    You can't assign a std::string object to a char array, they are completely different types. You could strcpy the contents of the std::string into the char array like so:
    Code:
    strcpy(names[i],line.c_str());
    However you should prefer the use of one of the handy STL containers to store your strings into such as a vector. They can grow and shrink as necessary so you aren't limited to a hard-coded value like 25.

    Code:
    #include <vector>
    #include <algorithm> // For the "copy" STL function
    #include <iterator> // For "ostream_iterator"
    ...
    
    vector<string> names;
    
    ...
    
    if (myfile.is_open())        //check if file is there
    {
        // Read the contents of the file into the string vector. //
        while ( getline(myfile,line) )
            names.push_back(line);
    
        // Output contents of vector using cout. //
        copy(names.begin(),names.end(),ostream_iterator<string>(cout,"\n"));
    
        myfile.close();       //closes file
    
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    And to point out an important design difference between hk_mp5kpdw's code and yours...
    Code:
    while (! myfile.eof() )      // set up loop
    
        {
    
          getline (myfile,line);      //read line
    That set up is going to set you up for corrupted data. See hk_mp5kpdw's
    Code:
    while ( getline(myfile,line) )
            names.push_back(line);
    You put the read-in code as the conditional.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21