字符串未返回到主函数

String not returning to main function

本文关键字:函数 返回 字符串      更新时间:2023-10-16

我刚刚开始使用c ++,这是针对我正在从事的一个项目。问题是我无法将字符串从搜索函数返回到 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<<"nn";
        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 openn";
    }
    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");
}

找到要查找的行后,您已停止读取文件。也许您想将函数更改为:

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;
}
找到

数据后,您需要打破while循环。一个简单的方法是在这一点上return

不要使用全局变量,除非你有充分的理由。如上所述,用作暂存器变量的全局变量只是邪恶™的。