【发布时间】:2016-03-28 05:18:26
【问题描述】:
我正在处理我的任务,我遇到了一个我想理解的错误(假设这是一个错误,而不仅仅是我过度思考了这个问题)。我当前的输出一次给我一个单词,而我想一次显示一行。这是我的代码:
/*
///////////////////////////////////////////////////////////////////////////
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;
}
这是输出:
I
ordered
this
sandwich
once
with
paper .....
而我想要这个:
I ordered this sandwich once with paper-thin carrots in it.
I can’t remember what else.
I tried to recreate it.
【问题讨论】:
-
尝试删除
endl,直到您打印的文本中有句号或句子结尾。