How can I read a paragraph in C++?

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()
;
}


Thanks!
Last edited on
You will need to use something like getline();

e.g.
1
2
3
4
5
6
string s;
string paragraph;
do {
 getline(cin, s);
 paragraph += s + "\n";
} while (s.length() > 0);
Last edited on
Thanks Zaita =D !!

ok... right now I'm like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#include <string.h>
#include <fstream.h>


void main ()
{
char ca1[500];
char *s;
char *par;

ifstream in("fichero.h");

clrscr();

printf("Write your paragraph:\n");
do {
in.getline(cin, s);
par += s + "\n";
} while (s.lenght() >0);


getch()
;
}


I'm honest, first time I see "getline" so I don't know how to use it... :(
Last edited on
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <fstream>
#include <iostream>
#include <string>
using namespace 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;
  }


Hope this helps.
Last edited on
Wow thanks for the help Duoas, I'll see what I can do with this =D

Thanks a lot!

I'm more from visual studio .net not C++, but this is a homework with that program
Last edited on
Topic archived. No new replies allowed.