Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
字符串未返回到主函数 我刚刚开始用C++,这是我正在做的一个项目。问题是我无法将字符串从SearchFunction返回到main。搜索函数本身工作得很好,很容易找到并显示它应该找到的行,但是字符串temp仍然是空的,尽管我从搜索函数返回了一个字符串。因此,DeleteFunction无法工作,因为它没有被传递应该删除的字符串_C++_String_Function_Return_Main - Fatal编程技术网

字符串未返回到主函数 我刚刚开始用C++,这是我正在做的一个项目。问题是我无法将字符串从SearchFunction返回到main。搜索函数本身工作得很好,很容易找到并显示它应该找到的行,但是字符串temp仍然是空的,尽管我从搜索函数返回了一个字符串。因此,DeleteFunction无法工作,因为它没有被传递应该删除的字符串

字符串未返回到主函数 我刚刚开始用C++,这是我正在做的一个项目。问题是我无法将字符串从SearchFunction返回到main。搜索函数本身工作得很好,很容易找到并显示它应该找到的行,但是字符串temp仍然是空的,尽管我从搜索函数返回了一个字符串。因此,DeleteFunction无法工作,因为它没有被传递应该删除的字符串,c++,string,function,return,main,C++,String,Function,Return,Main,我尝试过使用指针而不是返回值,但结果仍然是一样的。请帮助我理解我的错误所在 #include<iostream> #include<stdio.h> #include<string> #include<fstream> using namespace std; string data,temp; string SearchFunction(string); void DeleteFunction(string

我尝试过使用指针而不是返回值,但结果仍然是一样的。请帮助我理解我的错误所在

#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");
}
#包括
#包括
#包括
#包括
使用名称空间std;
字符串数据,临时;
字符串搜索函数(字符串);
无效删除函数(字符串);
int main()
{
int choice=0,choice3=0;
char-yn1;
字符串搜索;

无法找到要查找的行后,停止读取文件。可能需要将函数更改为:

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;
}
字符串搜索函数(字符串搜索)
{
int found=0,check=0;//声明两个变量并将其初始化为0。
ifstream outfile;//创建用于读取文件的对象。
outfile.open(“student.txt”);//打开文件。
//还要检查是否找到!!!
而(!outfile.eof()&&!found)//继续循环直到文件结束。
{
found=0,check=0;//在预期重复时,将两个变量再次初始化为0。
getline(outfile,data);//从文件到字符串变量数据输入一行。
find=data.find(search,find);//在字符串数据中搜索搜索项。
if(find!=string::npos)//如果找到搜索词。
{

cout当您找到数据时,您需要打破
while
循环。一个简单的方法是在该点返回


除非有很好的理由,否则不要使用全局变量。如上所述,全局变量用作草稿行变量是有害的™.

首先,返回的字符串也没有理由是全局变量。注意数据是全局变量。为什么必须返回它?调用函数后,您可以在main中访问它。为什么?@ThomasMatthews:它们做不同的事情。一个结果是布尔值,另一个结果是三态比较结果。@user3116554,好的,尽快当你再次调用函数时,它会发生变化。如果另一个函数没有预料到这一点,kaboom。多线程更糟糕。如果你发现了它,为什么还要继续运行?为什么不在cout之后返回呢?不,不,这是你正在做的。这是错误的。对不起,我的意思是:“你不能继续运行”这就是为什么如果发现while语句变为true,你必须签入while语句的原因。请参阅我发布的代码。不是我:),是的,你是对的,我混淆了while语句,但我仍然认为最好在if内返回if,然后在while外返回字符串,如果你没有找到任何内容,那么你可以处理Not found(未找到)。@user2957713我同意你的观点,但他说他刚开始用C++,我想给他一个响应,尽量少改变代码。移动返回语句,或者甚至添加另一个语句对他来说可能不是那么容易理解。@ USER 161655也请使用数据和TEMP作为局部变量。全局是坏的。它不会改变任何东西。这部分代码->但它是一个良好的坏习惯的开端,可能会导致未来不想要的行为