字符串不返回主函数

时间:2013-12-18 19:45:14

标签: c++ string function return main

我刚开始使用c ++,这是我正在研究的项目。问题是我无法将SearchFunction中的字符串返回给main。搜索功能本身工作正常,很容易找到并显示它应该找到的行但是尽管我从搜索功能返回一个字符串,但字符串temp仍为空。因此,DeleteFunction无法正常工作,因为它没有传递它应该删除的字符串。

我尝试过使用指针而不是返回值,但结果仍然相同。请帮我理解我哪里出错了。

#include<iostream>
#include<stdio.h>
#include<string>
#include<fstream>
using namespace std;

string data,temp;

string SearchFunction(string);                  
void DeleteFunction(string);                    

int main()
{
    int choice=0,choice3=0;
    char yn1;
    string search;

    cout<<"1. Press 1 to delete."<<endl;
    cin>>choice;
    cin.clear();                                        
    cin.ignore(1000,'\n');                              

    if(choice==1)
    {   
        cout<<"Enter RegNo. of record to be deleted: ";
        getline(cin,search);                        
        search="RegNo.: "+ search;                  //Concatenate with "RegNo: " to ensure that the search is done "by RegNo".
        temp=SearchFunction(search);                

        cout<<"1. "<<temp<<"\n\n";
        cout<<temp.length()<<endl;

        cout<<"Are you sure you want to delete the above record of"<<search<<"? Y/N";
        yn1=getchar();
        cin.clear();                                        
        cin.ignore(1000,'\n');                          

        if(!(yn1=='y' || yn1=='Y' || yn1=='n' || yn1=='N'))
        {
            do
            {
                cout<<"Enter 'Y' or 'N': ";
                yn1=getchar();
            }while(!(yn1=='y' || yn1=='Y' || yn1=='n' || yn1=='N'));
        }

        if(yn1=='y' || yn1=='Y')
        {
            DeleteFunction(temp);                   //Call delete function to delete record.
        }
    }
    return 0;
}


string SearchFunction(string search)
{

    int found=0, check=0;                               //Declare and initialize both variables to 0.
    ifstream outfile;                                   //Create object for reading file.

    outfile.open("student.txt");                        //Open file.

    while(!outfile.eof())                               //Continue loop until the end of file.
    {
        found=0, check=0;                               //Initialize both variables to 0 again in anticipation of repititions.

        getline(outfile, data);                         //Input one row from file to string variable data.
        found=data.find(search, found);                 //Search for the search term in string data.
        if(found!=string::npos)                         //If search term found.
        {
            cout<<data<<endl;                           //Display row.
        }
    }
    outfile.close();

    return data;
}

void DeleteFunction(string temp)
{
    string line;
    ifstream in("student.txt");
    if( !in.is_open())
    {
        cout << "Input file failed to open\n";
    }

    ofstream out("temp.txt");

    while( getline(in,line) )
    {
        if(line != temp )
            out << line << "\n";
    }
    in.close();
    out.close();    

    remove("student.txt");
    rename("temp.txt","student.txt");
}

2 个答案:

答案 0 :(得分:1)

找到要查找的行后,您停止阅读该文件。也许您想将功能更改为:

string SearchFunction(string search)
{

    int found=0, check=0;                               //Declare and initialize both variables to 0.
    ifstream outfile;                                   //Create object for reading file.

    outfile.open("student.txt");                        //Open file.

    // Also check if found!!!
    while(!outfile.eof() && !found)                     //Continue loop until the end of file.
    {
        found=0, check=0;                               //Initialize both variables to 0 again in anticipation of repititions.

        getline(outfile, data);                         //Input one row from file to string variable data.
        found=data.find(search, found);                 //Search for the search term in string data.
        if(found!=string::npos)                         //If search term found.
        {
            cout<<data<<endl;                           //Display row.
        }
    }
    outfile.close();

    return data;
}

答案 1 :(得分:1)

找到数据后,您需要突破while循环。一个简单的方法就是return

除非你有充分的理由,否则不要使用全局变量。如上所述,用作刮板变量的全局变量只是Evil™。