如何使用用户输入作为函数参数

How do I use a user input as a function parameter?

本文关键字:函数 参数 输入 何使用 用户      更新时间:2023-10-16

我有一个函数void mainRoutine(string searchStrings, string fileBeingSearched, char K, char B);

我希望我的mainRoutine函数,searchStrings和filebeingsearches中的参数是用户键入的文件的名称。(例如,我输入"b.txt",这是searchStrings应该接受的字符串).

所以在我提示用户输入文件名之后:

string userInput;
cout << "Please enter the string file to be searched: " << endl;
getline(cin, userInput);
ifstream filename(userInput.c_str());
while(filename.good()&&filename.peek()!=EOF)
{
        cout << (char)filename.get();
}
cout << "n";
string userInput2;
cout << "Please enter the file to be searched: " << endl;
getline(cin, userInput2);
ifstream fileToBeSearched(userInput.c_str());
while(fileToBeSearched.good()&&fileToBeSearched.peek()!=EOF){
        cout << (char)fileToBeSearched.get();
}
cout << "n";

我如何连接它,以便userInput是函数参数的字符串?

mainRoutine(userInput, userInput2, K, B);
假设K和B是char类型