I'm having trouble adding white space to the 2D array "item". In the end, I essentially want the data in file (quote.txt) to be able to index properly with its line number. I've made the array 9(row) by 20(col) which is largest sentence in my file and where ever there are not 20 column units of data I want to populate it with a white space so I can index my array accordingly.
I've tried using vector of vectors but it gets super confusing.
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <array>
#include <string>
using namespace std;
int main()
{
string file_name;
ifstream fin("quote.txt");
while(!fin)
{
cout << "Error Opening File! Try again!" << endl;
cout << "Enter file name: ";
cin >> file_name;
}
string item[9][20];
for (int row = 0; row < 9; row++)
{
for (int col = 0; col < 20; col++)
{
fin >> item[row][col];
//cout << item[row][col] << endl;
}
}
for (int k = 0; k < 20; k++)
{
cout << item[0][k] << endl;
}
}
Explanation: I'm trying to populate my item 2d array with the contents in quote.txt but since the sentence length varies I cannot use for loop and say column is 20 units because it bleeds into the next row and screws up the indexing. My solution is that I want to add a white space (filler) so that I can iterate over with my for loop and every content in each row has 20 columns. That way I can use the index of rows to look at each row in the text file. Basically, I want the text file to be a 2D array in which I can find each element(word) through using [row][col] indices.
Text File: "quote.txt"
People often say that motivation doesn t last Well neither does bathing that s why we recommend it daily Ziglar
Someday is not a day of the week Denise Brennan Nelson
Hire character Train skill Peter Schutz
Your time is limited so don t waste it living someone else s life Steve Jobs
Sales are contingent upon the attitude of the salesman not the attitude of the prospect W Clement Stone
Everyone lives by selling something Robert Louis Stevenson
If you are not taking care of your customer your competitor will Bob Hooey
The golden rule for every businessman is this: Put yourself in your customer s place Orison Swett Marden
If you cannot do great things do small things in a great way Napoleon Hill
What the Program is suppose to do?
The program is suppose to allow me to find a word by user input. say the word is "of" it is suppose to output which line numbers it is on. Similarly if I input "of People" it outputs the line number