无法将我的输入显示在.txt文件

时间:2016-04-05 07:48:54

标签: c++ c++11

我让用户输入一个名称列表(getNames),然后将这些名称写入选定的txt文件。我正在尝试将名称显示在文本文件中,但没有任何工作。我尝试了很多不同的东西,但无法弄明白。

getFile函数在开头就是正确的。我需要'fout'显示用户输入的名称。如何让.txt显示用户从getNames输入的名称?

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <fstream>

using namespace std;
char getMenu(); // list of functions
int getNames(vector<string>& v);
void displayNames(vector<string>& v);
void sortNames(vector<string>& v);
int findName(vector<string>& v, string name);
void removeNames(vector<string>& v, string name);
void getFile(vector<string>& v);

void getFile(vector<string>& v)
{
    string fileName;        
    cout << "Enter file name: ";
    getline(cin, fileName);
    ofstream fout(fileName.c_str(),ios::app);
    fout << ""; **// need to display names here**
    fout.close();

}

char getMenu() 
{
    char letter;  
         cout << "(G)etNames, (D)isplayNames, (S)ortNames, (R)emoveNames, (F)indNames," << endl << "(W)rite to file, (Q)uit:" << endl;      
    cin >> letter;  
    cin.ignore(256, '\n'); // cleans upto 256 characters
    return toupper(letter); //toupper makes lowercase=uppercase
}

int main() 
{ 
  vector<string> v; 
  string name; 

  bool quit = true;

  do
  {
    switch(getMenu())
    {
      case 'G': getNames(v); break; 
      case 'S': sortNames(v); cout << endl << "Sorted names:" << endl;
                displayNames(v); break;
      case 'D': displayNames(v); break;
      case 'F': findName(v, name); break;
      case 'R': removeNames(v, name); break;
      case 'W': getFile(v); break;
      case 'Q': quit = false; break;
      default: "Error. Try again: "; cout << endl; break;
    } 

  }
  while(quit);
  cout << endl << "Program terminated." << endl;

}


int getNames(vector<string>& v)
{
    string tmp; // gets names
    while(true) 
    {
        cout<<"Enter a name, quit to stop:";
        cin>>tmp;
        if(tmp=="quit")
        {
            break;
        }
        v.push_back(tmp);
    }
}

void displayNames(vector<string>& v)
{
    for(int i=0; i<v.size(); i++) // displays names
    {
        cout<<v.at(i)<<endl;
    }
}

void sortNames(vector<string>& v) // sort names
{
  sort(v.begin(),v.end()); 
}

int findName(vector<string>& v, string name)
{
    cout << "Enter name to find : " << endl;
    cin >> name;
    int pos = find(v.begin(), v.end(), name) - v.begin();
        if (pos<v.size())
    {
    cout << "The name, " << name << ", was found." << endl;
    }
    else
    {
        cout << "Name not found." << endl;
    }    
}

void removeNames(vector<string>& v, string name)    
{{
    string name;
    if(v.size() > 0) 
    {    
    cout << "Enter name to remove: " << endl;
    }
    cin >> name;

    int pos = find(v.begin(), v.end(), name) - v.begin();
    if (pos >= 0)    

    {
      v.erase(v.begin() + pos); //goes to 
                                //(first value + the position) and erases       
      cout << name << " has been removed." << endl; 
    }
    else
    {
      cout << "That name does not exits." << endl;
    }


    }}

1 个答案:

答案 0 :(得分:2)

最简单的解决方案似乎是:

for(auto& name : v)
    fout << name << '\n';

我不确定他们应该如何分开。