Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.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/8/file/3.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++ c++;向文件中得分最高的用户显示_C++_File - Fatal编程技术网

C++ c++;向文件中得分最高的用户显示

C++ c++;向文件中得分最高的用户显示,c++,file,C++,File,我有一个名为“SCORES.TXT”的文件,其中包含以下内容: Player - Score -------------------- John Miles - 132 Henry - 90 Juliet P - 110 程序必须向用户显示人名和相应的分数,如: John Miles has a score of 132 Henry has a score of 90 Juliet P has a score of 110 我有以下代码,但它不能正常工作。变量昵

我有一个名为“SCORES.TXT”的文件,其中包含以下内容:

Player      - Score
--------------------
John Miles  - 132
Henry       - 90
Juliet P    - 110
程序必须向用户显示人名和相应的分数,如:

John Miles has a score of 132
Henry has a score of 90
Juliet P has a score of 110
我有以下代码,但它不能正常工作。变量昵称只获取第一个名称,如果我添加一个字符串变量来获取第二个名称,程序将无法在只有一个名称的行中运行

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
    string fileName = "SCORES.TXT", line, nickname;
    char c;
    unsigned int score;
    unsigned short int i = 0;
    ifstream file(fileName);
    while (getline(file, line)) { 
        if (i < 2) {              //
            i++;                  // ignoring the header
        }                         //
        else {
            stringstream s(line);
            s >> nickname >> c >> score;
            cout << nickname  << " has a score of " << score;
        }
    }
    file.close();
    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串fileName=“SCORES.TXT”,行,昵称;
字符c;
无符号整数分数;
无符号短整数i=0;
ifstream文件(文件名);
while(getline(文件,行)){
如果(i<2){//
i++;//忽略标题
}                         //
否则{
弦(线);
s>>昵称>>c>>分数;

cout扫描复杂输入线的一种解决方案是使用这种方法,这种方法非常灵活,但可能效率不高

一些提示:

您不应使用
使用命名空间std;

file.close()
不是明确需要的,因为在
main
的作用域结束时,
ifstream
超出作用域并将自动销毁,这也会关闭文件,但之前手动关闭它并不是失败的

请参阅以下示例,了解如何使用
regex
扫描字符串以查找模式以及如何获取搜索结果:

非常清楚:这只是一千种可能的解决方案中的一种!它非常灵活,但正如前面提到的那样效率较低

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

int main()
{
    std::string fileName = "SCORES.TXT";
    std::string line;
    // we search the input line for the following regular expression:
    // first everything which is a charater -> a-z or A-Z and a space and as mutch as we find *
    // after that we search for - and also the following whitspaces
    // next we pick the number
    // the () arround the expression forwards the result of each () in a separate result m[]
    std::regex re("([a-zA-Z ]*)[- ]*([0-9]*)", std::regex::extended );

    std::ifstream file(fileName);
    while (std::getline(file, line)) 
    {
        try  // if regex did not find correct patterns, we ignore that
        {
            std::smatch m;
            std::regex_search( line, m, re );

            // we expect 3 parts as result, first is the full string, second is our nickname, last is the number
            if ( m.size() == 3 ) 
            {
                // as we have also the trailing whitespaces in the nickname part, we remove them here
                std::string nickname = std::regex_replace(std::string(m[1]), std::regex(" +$"), "");
                // create an int from found number string
                int number = std::stoi(m[2]);

                std::cout << nickname << " has a score of " << number << std::endl;
            }
        } catch(...){}
    }   

    return 0;
}
#包括
#包括
#包括
#包括
int main()
{
std::string fileName=“SCORES.TXT”;
std::字符串行;
//我们在输入行中搜索以下正则表达式:
//首先是一个字符->a-z或a-z,还有一个空间,以及我们发现的所有东西*
//在此之后,我们搜索-以及以下空间
//下一步,我们选择数字
//表达式周围的()在单独的结果m[]中转发每个()的结果
std::regex-re(([a-zA-Z]*)[-]*([0-9]*)”,std::regex::extended);
std::ifstream文件(文件名);
while(std::getline(文件,行))
{
试试//如果正则表达式没有找到正确的模式,我们会忽略它
{
std::smatch m;
标准::正则表达式搜索(行、m、re);
//我们期望得到3部分结果,第一部分是完整字符串,第二部分是我们的昵称,最后是数字
如果(m.size()==3)
{
//因为我们在昵称部分也有尾随空格,所以我们在这里删除它们
std::string昵称=std::regex_replace(std::string(m[1]),std::regex(“+$”)”);
//从找到的数字字符串创建int
int number=std::stoi(m[2]);

std::我不能确定stringstream的运算符>>将在第一个空格字符处停止,因此在某些情况下,您的昵称将不完整,提取的其余部分要么不符合您的要求,要么就失败了。我建议在行中找到-的位置,然后根据该信息提取每一半。我假设这就是你的意思是“工作不正常”由于您没有实际指定不起作用的内容。而且我无法理解您为什么需要gdb之类的调试器或Visual Studio中的调试器。使用调试器,您可以一次一行地通过代码,在每一步查看变量。Obs:每个名称少于16个字符长度不是问题。
John Miles>昵称,则代码>是一个问题。更一般地说,当您编写完成多个新任务的新代码时,请一次测试一个。您应该注意到,在尝试获取分数之前,
昵称
捕获了
约翰
,而不是
约翰·迈尔斯
。请参阅上的页面。我从未听说过非常感谢。只是一个问题,我们为什么不使用“using namespace::std”?使程序效率降低?@rique002这里有很多好信息:@rique002而不是说Tanks,请使用UpVote,如果答案解决了你的问题,请接受答案。这就是为什么如此!:-)谢谢!@rique002为什么不使用名称空间:因为特别是在
std
中,定义了很多。有一个很有可能,您将使用相同的名称定义自己的内容,这将导致名称冲突。即使今天没有发生这种情况,STL中也会不时添加更多功能。可能在下一次编译器更新时,您将无法编译,因为您的定义与命名空间中的定义冲突
std::