字符串未返回到主函数
String not returning to main function
我刚刚开始使用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
。
不要使用全局变量,除非你有充分的理由。如上所述,用作暂存器变量的全局变量只是邪恶™的。
相关文章:
- 从函数返回const char*数组
- 检查函数返回类型是否与STL容器类型值相同
- 从 c 或 cpp 系列子函数返回到主函数
- 从函数返回任意简单类型的数据
- 警告:在函数返回类型 [-Wignore 限定符] 时忽略类型限定符
- 从 C++ 中的函数返回数组地址问题
- VirtDisk.h QueryChangesVirtualDisk() 函数返回 RangeCount 为 0
- 为什么glGetSubroutineIndex为不同的函数返回相同的值?
- 在 c++ 中将函数返回类型指定为模板参数
- 从封装在对象中的函数 C++ 返回时为空的列表
- 为什么向量内部的指针在从函数返回时会发生变化?
- 函数返回的 rvalue 引用(表达式)是 xvalue - 但没有标识?
- 程序中的布尔函数返回输入的范围无论如何都是无效的
- 寿命延长从函数返回引用
- 程序不向函数返回值
- 字符串函数返回奇怪的值
- 类的大问题,以及从空函数c++返回
- 为什么在尝试测量函数返回所需的时间时,我的运行时编号是错误的?
- C++二叉搜索树模板从函数返回节点
- NVCC 错误:string_view.h:constexpr 函数返回是非常量