Problem with Enters

I want to write a program which will be able read from keyboard(but when there are Enter entered from keyboard program will be finished!) and write it in the text file!
|
Last edited on
Why not just have the user enter a special character or number to end? It would make it much easier.
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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

int main()
{
  vector<string> data;
  string temp  = "";

  while(true)  
  {
    cout << "Enter some data: ";
    getline(cin, temp);
    
    if( (temp == "q") || (temp == "Q") )
      break;
    
    data.push_back(temp);
  }

  // Write the data
  ofstream out("out.txt", ios::app);
  for(vector<string>::const_iterator i = data.begin(); i != data.end(); ++i)
  {
    out << *i << endl;
  }

  return 0;
}
Last edited on
Topic archived. No new replies allowed.