getline for reading string file

I am having trouble loading the txt data into my array with the getline function.
[code]
/* Global Headers*/


#include <iostream> // include standard I/O library
#include <fstream> // opening and closing files
#include <cstdlib>
#include <string> // set string class

using namespace std;



/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/

/* Global Constants */


const int NUM_GUESTS = 25 ; // number of guests on list
const string BLANKS = " " ; // input spacing
std::string guestlist[NUM_GUESTS]; // array
int count; //for counter

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/



/* Function Prototypes */

void PrintIntro();
void OpenFile(ifstream& inFile);
void ReadFile ();
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
/* main function */

int main ()

{



ifstream inFile; // input data file

//Print intro for user
PrintIntro();

//open file that contains stars name
OpenFile(inFile);
//read and file array//
ReadFile();


return 0;

}




/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/

/* Functions */

void PrintIntro()

{ // Print intro

cout << "********************************************************"
<< endl;
cout << " *The L.A. Enquirer Welcomes the Top 25 Hollywood Stars*" << endl;
cout << "********************************************************"
<< endl;

return;

} // Print intro

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
void OpenFile(ifstream& inFile)


{
inFile.open("partyGuests.txt");

if (!inFile)
{

cout<< ("This file is unaccessable at this time!")<< endl;
exit (1);

}

else
cout<< "file open" << endl;

return;
}
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
// read from file and build list//

void ReadFile(ifstream& inFile)



//close file
inFile.close();

return;
}


[code/]

I don't know how to construct a getline that will read the file into the array so that I can build a parallel array to compare with the names in the original
array.
Try focusing your question and posting only the code which relates to the problem.
I would try using a for loop ex)

1
2
3
4
5
6
7
string guest_name;

for (count = 0, count < NUM_GUESTS, count++)
{
     getline(cin,guest_name);
     guestlist[count] = guest_name;
}


That should work. Getting it to write it to a file would be the same concept. Remember, an array of 25 would has elements starting with 0 to 24.
Last edited on
ljrobison would the for loop read white spaces the list is for 25 guests first and last names.I tried

a for loop earlier and got cout that looked like this
John
Hughes
Hugh
Jackman
Every White space skipped line does the get line in your code solve this issue
Thank You so much for the time
That should work. I haven't tried it or anything but strings can contain whitespaces. Try it out =D

Just make sure you dont press enter after the first name.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
void Program::setInFileName(string inFlNm)
{
    inFile = inFlNm;
}
string Program::getInFileName()
{
    return inFile;
}

void Program::processFile2Array()
int count=0;
try
{
    ifstream input(getInFileName().c_str());
    
    if (input.is_open())
    {
        while (! input.eof())
        {
            getline(input,array[count]);

            ++count;
        }
    }
    else
    {
        cout << "Error opening file: " << getInFileName() << endl;
        exit(1);
    }
}
catch (...)
{
    cerr << "Error processing file" << endl;
    exit(1);
} 
Last edited on
Topic archived. No new replies allowed.