Well Hello, I'm pretty new in this community but I need a little help with this...
I'm need to read a paragraph with C++ but when I'm running the program after some letters I can't type anymore... So I can't write the whole information to read in a single variable...
Do you know how can I do that? I've been looking like all day and I saw something about "flush" and "buffer" but I'm pretty lost...
My code is pretty simple 'cuz I just want to read the paragraph
Here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
void main ()
{
char ca1[500];
clrscr();
printf("Write your paragraph:\n");
flush(ca1); //this is me trying to make it read a lot more lol
gets(ca1);
getch()
;
}
It seems you are fairly familiar with C. If you intend to use C++, you should take a look through the documentation and tutorials on this site (they are really good).
Typically plain-text documents have two newlines between paragraphs. This is due to the tradition (from when people used console programs all the time). The user can press ENTER at the edge of the screen to cause the cursor to move to the next line without having the word "sepa
rated" in odd places.
Now, do you want to read the paragraph from your file "fichero.h"? Typically only C and C++ header files are named "anything.h". And "fichero" is not a very a very descriptive name... If you just want something generic you might try "datos.txt" or the like... In any case...
To read from fichero or console, you can choose either:
#include <fstream>
#include <iostream>
#include <string>
usingnamespace std;
string leer_parrafo( istream &in )
{
string result, line;
// read and concatenate lines until two newlines are read
while (getline( in, line ))
if (line.empty()) break;
else result += line +' ';
// get rid of that last space
result.erase( result.length() -1 );
return result;
}
int main()
{
cout << "Reading paragraph from file...\n";
ifstream file( "fichero.txt" );
string fichero_parrafo = leer_parrafo( file );
file.close();
cout << "The file's text is:\n"
<< fichero_parrafo
<< endl;
cout << "Please enter a paragraph (press ENTER twice to stop):\n";
string console_parrafo = leer_parrafo( cin );
cout << "You entered the text:\n"
<< console_parrafo
<< endl;
cout << "Bye!\n";
return 0;
}