ok, so I've been running in circles for a while and I really need help with this.
How do I extract the line of text from the file? I can print the contents of the file but I can figure out how to extract the text line by line.
using namespace std;
//------------------------------global variables
int NumberOfLines = 0;
//-----------------------------function declarations
string GetStudentList();
string GetClassHours ();
string OpenStudentFile (string);
void PrintLine (string);
//-----------------------------create structures
struct Student_Info {
string FirstName;
string LastName;
int StudentID;
int GPA;
};
//-----------------------------main body
int main()
{
string File1;
cout << "Welcome to the GradeCaculator Deluxe." << endl;
cout <<"Enter the file name for the list of Students, including the extension::" << endl;
getline(cin, File1);
OpenStudentFile(File1);
// GetStudentList ();
// GetClassHours ();
// getline(File1) ;
return 0;
}//end main
string OpenStudentFile(string File1)
{
ifstream inputFile;
string word;
inputFile.open(File1.c_str());
if(!inputFile.is_open())
{
exit(EXIT_FAILURE);
}
cout << "\nThe file has been successfully opened for reading.\n";
while (getline (inputFile, File1))
{
++NumberOfLines;
cout << File1 << endl; //prints contents of text file
}
cout << "Number of Lines in text file is: " << NumberOfLines << endl;//prints the number of lines in the text file
#include<string>
#include <iostream>
#include <fstream>
#include <cmath>
#include<cstdlib>
#include <vector>// ******************************************NEW LINE***********************
usingnamespace std;
//------------------------------global variables
int NumberOfLines = 0;
//-----------------------------function declarations
string GetStudentList();
string GetClassHours();
string OpenStudentFile(string);
void show_lines(vector<string>&);//********************NEW LINE************************
void PrintLine(string);
//-----------------------------create structures
struct Student_Info {
string FirstName;
string LastName;
int StudentID;
int GPA;
};
//-----------------------------main body
int main()
{
vector<string>lines;//************************************NEW LINE**************************
string File1;
cout << "Welcome to the GradeCaculator Deluxe." << endl;
cout << "Enter the file name for the list of Students, including the extension::" << endl;
getline(cin, File1);
OpenStudentFile(File1);
lines.reserve(NumberOfLines);//****************************************NEW LINE******************************
//to avoid reallocation - vector size is set to number of lines
//now content of text file is loaded into vector:
ifstream inputFile;
string word;
inputFile.open(File1.c_str());
while (getline(inputFile, word))
{
lines.push_back(word);
}
//show contents of separate lines, asking for line number; You can cut any line and write results to You structure
show_lines(lines);//********************************NEW LINE****************************
// GetStudentList ();
// GetClassHours ();
// getline(File1) ;
return 0;
}//end main
string OpenStudentFile(string File1)//******************************vector<string>ln*********************
{
ifstream inputFile;
string word;
inputFile.open(File1.c_str());
if (!inputFile.is_open())
{
exit(EXIT_FAILURE);
}
cout << "\nThe file has been successfully opened for reading.\n";
while (getline(inputFile, File1))
{
++NumberOfLines;
cout << File1 << endl; //prints contents of text file**********************************excluded from program, contents in container
}
cout << "Number of Lines in text file is: " << NumberOfLines << endl;//prints the number of lines in the text file
inputFile.close();//*********************NEW LINE***close stream**********************
return File1;
}
//function to show contents of lines, by numbers, without error checking!!
void show_lines(vector<string>&ln_full)
{
int vect_size = ln_full.size();
cout << "Lines in file: " << vect_size << endl;
cout << "Enter a number to show line:\n";
int number = 0;
while (number < vect_size)
{
cin >> number;
cout << ln_full[number-1];
}
}