Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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
Loops 在C++;如何将搜索功能和开关结合起来,以便开关可以要求用户从搜索结果中进行选择并显示结果信息?_Loops_Search_Switch Statement - Fatal编程技术网

Loops 在C++;如何将搜索功能和开关结合起来,以便开关可以要求用户从搜索结果中进行选择并显示结果信息?

Loops 在C++;如何将搜索功能和开关结合起来,以便开关可以要求用户从搜索结果中进行选择并显示结果信息?,loops,search,switch-statement,Loops,Search,Switch Statement,代码要求用户从球队中挑选一名球员,并显示球员的统计信息。搜索也必须能够进行部分输入搜索。这是我到目前为止提出的代码。但我被困在开关工作正常或搜索功能之间,但无法使它们一起工作。谢谢你的帮助 #include <iostream> #include <string> #include <fstream> using namespace std; int main() { int index = 0; int choice; int counter = 1;

代码要求用户从球队中挑选一名球员,并显示球员的统计信息。搜索也必须能够进行部分输入搜索。这是我到目前为止提出的代码。但我被困在开关工作正常或搜索功能之间,但无法使它们一起工作。谢谢你的帮助

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
int index = 0;
int choice;
int counter = 1;
string player, result;


cout << "\n\nThis program will ask the user for a player name from\n";
cout << "the team Dallas Mavericks and give the statistics of the player.\n";
cout << "  *Last Name First\n";
cout << "  *Seperated By Comma\n";
cout << "  *Use correct capitalization\n";
cout << "Please enter player's name: ";
getline(cin, player);

ifstream inputFile;
inputFile.open("statisticsMavs.txt");
if (inputFile.fail())
{
    cout << "Could not open file";
    return 1;
}

ofstream outputFile;
outputFile.open("playerStats.txt");

if (outputFile.fail())
{
    cout << "Could not open file";
    return 1;
}

for (index = 0; getline(inputFile, result); index++) 
{
    if (result.find(player) != string::npos)
    {
        cout << endl << counter++ << "  " << result.substr(0, 20) << endl;
        continue;
    }

    else
    {
        continue;
        cout << "Could not find player! Please enter another name: ";
        getline(cin, player);
    }
}


cout << "Please choose the number from the correct search result: ";
cin >> choice;
switch (choice)
{
case 1: outputFile << result << endl;
    return 1;
case 2: outputFile << result << endl;
    return 1;
case 3: outputFile << result << endl;
    return 1;
case 4: outputFile << result << endl;
    return 1;
default: cout << choice << " is not a valid input" << endl;
}


inputFile.close();
outputFile.close();



}
#包括
#包括
#包括
使用名称空间std;
int main()
{
int指数=0;
智力选择;
int计数器=1;
弦乐演奏者,结果;

cout我相信下面的内容,虽然没有使用switch,但确实实现了您的期望,即找到最多4个匹配结果,然后给出所选的结果。由于我没有文本文件,我不确定它是否可以正常工作,但它可以编译,而且似乎应该。我尽量简化,同时保持原始意图t(但我认为原始值不可用的地方是结果保存了最后一个读取匹配字符串,因此无法选择较早的字符串

#include <iostream>
#include <string>
#include <fstream>

using namespace std;
int main()
{
int choice;
int counter = 0;
string player, result;
string results[4];


cout << "\n\nThis program will ask the user for a player name from\n";
cout << "the team Dallas Mavericks and give the statistics of the player.\n";
cout << "  *Last Name First\n";
cout << "  *Seperated By Comma\n";
cout << "  *Use correct capitalization\n";
cout << "Please enter player's name: ";
getline(cin, player);

ifstream inputFile;
inputFile.open("statisticsMavs.txt");
if (inputFile.fail())
{
    cout << "Could not open file";
    return 1;
}

ofstream outputFile;
outputFile.open("playerStats.txt");

if (outputFile.fail())
{
    cout << "Could not open file";
    inputFile.close();
    return 1;
}

while ((counter<4) && getline(inputFile, result)) 
{
    if (result.find(player) != string::npos)
    {   
        results[counter++]=result;
        cout << endl << counter << "  " << result.substr(0, 20) << endl;

    }


}
if (counter == 0)
    {
        cout << "Could not find player! Terminating."<<endl;
    }
else
{
cout << "Please choose the number from the correct search result: ";
cin >> choice;

if ((choice>=1) && (choice<=counter))
{
    outputFile << results[choice-1] << endl;
    inputFile.close();
    outputFile.close();
    return 1;
}
else
{
cout << choice << " is not a valid input" << endl;
}
}

inputFile.close();
outputFile.close();



}
#包括
#包括
#包括
使用名称空间std;
int main()
{
智力选择;
int计数器=0;
弦乐演奏者,结果;
字符串结果[4];

cout我相信下面的内容,虽然没有使用switch,但确实实现了您的期望,即找到最多4个匹配结果,然后给出所选的结果。由于我没有文本文件,我不确定它是否可以正常工作,但它可以编译,而且似乎应该。我尽量简化,同时保持原始意图t(但我认为原始值不可用的地方是结果保存了最后一个读取匹配字符串,因此无法选择较早的字符串

#include <iostream>
#include <string>
#include <fstream>

using namespace std;
int main()
{
int choice;
int counter = 0;
string player, result;
string results[4];


cout << "\n\nThis program will ask the user for a player name from\n";
cout << "the team Dallas Mavericks and give the statistics of the player.\n";
cout << "  *Last Name First\n";
cout << "  *Seperated By Comma\n";
cout << "  *Use correct capitalization\n";
cout << "Please enter player's name: ";
getline(cin, player);

ifstream inputFile;
inputFile.open("statisticsMavs.txt");
if (inputFile.fail())
{
    cout << "Could not open file";
    return 1;
}

ofstream outputFile;
outputFile.open("playerStats.txt");

if (outputFile.fail())
{
    cout << "Could not open file";
    inputFile.close();
    return 1;
}

while ((counter<4) && getline(inputFile, result)) 
{
    if (result.find(player) != string::npos)
    {   
        results[counter++]=result;
        cout << endl << counter << "  " << result.substr(0, 20) << endl;

    }


}
if (counter == 0)
    {
        cout << "Could not find player! Terminating."<<endl;
    }
else
{
cout << "Please choose the number from the correct search result: ";
cin >> choice;

if ((choice>=1) && (choice<=counter))
{
    outputFile << results[choice-1] << endl;
    inputFile.close();
    outputFile.close();
    return 1;
}
else
{
cout << choice << " is not a valid input" << endl;
}
}

inputFile.close();
outputFile.close();



}
#包括
#包括
#包括
使用名称空间std;
int main()
{
智力选择;
int计数器=0;
弦乐演奏者,结果;
字符串结果[4];

如果他们选择了玩家,而你找到了,他们会再次选择,因为你可能有两个史密斯在同一个团队中?哦,我明白了问题。我打赌如果你只匹配1,效果会很好。但是如果有多个匹配,结果只保留最后一个匹配。感觉将结果制作一个数组会有所帮助。具体来说,将结果移动到新行并声明它字符串result[4]。使getline(inputFile,result)getline(inputFile,result[counter-1])索引。使用[counter-1]查找,使用计数器-2查找subst,并且在每种情况下(只要他们说1到4,您甚至不需要开关)输出结果[counter-1]另外,在我看来,你不应该认为你在圈外之前没有找到玩家。好的。我通过将其移出圈外来修复问题。因此,没有方法让切换和搜索功能一起工作吗?如果他们选择了玩家,而你找到了,他们会再次选择,因为你可能有两个史密斯在同一个团队中?Oh、 我明白问题所在。我打赌如果只匹配1,效果会很好。但如果有多个匹配项,结果只保留最后一个匹配项。感觉将结果设置为数组会有所帮助。具体地说,将结果移动到新行并声明为字符串结果[4]。将getline(inputFile,result)getline(inputFile,result[counter-1])索引。使用查找[counter-1]和带有counter-2的subst,在每种情况下(只要他们说1到4,你甚至不需要开关)输出结果[counter-1]另外,在我看来,你不应该得出结论,直到你在你的循环之外,你才找到玩家。好的。我通过将它移到循环之外解决了问题。那么,没有办法让切换和搜索功能一起工作吗?