I am working on my assignment and I have come across an error that i would like to understand (assuming this is an error and not just me overthinking the question). My current output is giving me one word at a time whereas i would like to display a line at a time. Here is my code:
/*
///////////////////////////////////////////////////////////////////////////
Write a program that creates an array of 100 string objects. Fill the
array by having your program open a (text) file and read one line of the
file into each string until you have filled the array. Display the array
using the format “line #: <string>,” where # is the actual line number
(you can use the array counter for this value) and <string> is the stored
string.
///////////////////////////////////////////////////////////////////////////
*/
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main ()
{
string story[100]; // create array of 100 string objects
string filename = "testfile.txt";
ifstream file(filename); //open file
// go through array till index 100
for( int x=0; x<= 100; x++)
{
file >> story[x]; // get line and store into array
cout << "Line " << x << ":" << story[x] << endl; // display
}
return 0;
}
And here is the output:
I
ordered
this
sandwich
once
with
paper .....
whereas i want this:
I ordered this sandwich once with paper-thin carrots in it.
I can’t remember what else.
I tried to recreate it.