简体   繁体   English

C++ 无法解决我的问题。 我有控制台错误

[英]C++ can't solve my problem. I have console error

I dont know C++ but I need help, solution.我不知道 C++ 但我需要帮助,解决方案。 I want to read from file words.txt strings and if separator ',' comes then program should cut off this line and go further to the next one.我想从文件 words.txt 字符串中读取,如果出现分隔符“,”,那么程序应该切断这一行,go 进一步到下一行。 Its working 50/50 because I have error in console:它的工作率为 50/50,因为我在控制台中有错误:

terminate called after throwing an instance of 'std::lenght_error'
what(): basic_string::_M_replace_aux

Source code:源代码:

#include <iostream>
#include <string>
#include <map>
#include <cmath>
#include <fstream>
#include <stdio.h>

using namespace std;

void print( string::size_type n, string const & s )
{
    if( n == string::npos ) {
        std::cout << "not found\n";
    } else {
        cout << "found: " << s.substr( n ) << '\n';
    }
}


int main()
{
    map < char, int > pythagorean;

    pythagorean[ 'a' ] = 7;
    pythagorean[ 'b' ] = 8;
    pythagorean[ 'c' ] = 1;
    pythagorean[ 'd' ] = 2;
    pythagorean[ 'e' ] = 3;
    pythagorean[ 'f' ] = 4;
    pythagorean[ 'g' ] = 5;
    pythagorean[ 'h' ] = 6;
    pythagorean[ 'i' ] = 7;
    pythagorean[ 'j' ] = 8;
    pythagorean[ 'k' ] = 9;
    pythagorean[ 'l' ] = 1;
    pythagorean[ 'm' ] = 2;
    pythagorean[ 'n' ] = 3;
    pythagorean[ 'o' ] = 4;
    pythagorean[ 'p' ] = 5;
    pythagorean[ 'q' ] = 6;
    pythagorean[ 'r' ] = 7;
    pythagorean[ 's' ] = 8;
    pythagorean[ 't' ] = 9;
    pythagorean[ 'u' ] = 1;
    pythagorean[ 'v' ] = 2;
    pythagorean[ 'w' ] = 3;
    pythagorean[ 'x' ] = 4;
    pythagorean[ 'y' ] = 5;
    pythagorean[ 'z' ] = 6;


    string::size_type n;
    ifstream input( "words.txt" );
    ofstream output( "Results.txt" );
    string str;
    int counter = 0, cnt = 0;


    while( getline( input, str ) )
    {
        int sum = 0;

        n = str.find( ',' );
        str.resize( n );

        for( char charr: str )
        {
            sum += pythagorean[ charr ];
        }

        if( sum == 11 )
        {
            cout << str << "(" << sum << ")" << '\n' << "Pythagorean: " << '\t';
            output << "Pythagorean: " << '\t' << str << " = " << sum << endl;
            counter++;
        }
    }

    cout << "\n\n You have found: " << counter << " words" << '\n';
    output << "\n\n You have found: " << counter << " words" << '\n';

    return 0;
}

Can someone give me a solution?有人可以给我一个解决方案吗? Im really bad in C++ and I need it to check a lot of datas in.txt file.我在 C++ 中真的很糟糕,我需要它来检查 .txt 文件中的大量数据。 Please:)请:)

In

n = str.find( ',' );
str.resize( n );

if there is no comma in str , find will return npos and npos is an incredibly large number that a std::string cannot be resized to fit.如果str中没有逗号, find将返回npos并且npos是一个非常大的数字, std::string无法调整大小以适应。 This is precisely why npos is used to signal no match found: npos cannot possibly be a valid index in the string.这正是使用npos表示未找到匹配项的原因: npos不可能是字符串中的有效索引。

To avoid the problem test n against npos为了避免针对npos测试n的问题

n = str.find( ',' );
if (n != std::string::npos)
{ // found a comma
    str.resize( n );
}
else
{ // did not find a comma
    // Handle the error as best fits the needs of the program.
    // Perhaps this is not an error and the program can continue parsing with 
    // the whole string.
    // But if there must be a comma and one cannot be found, there is a protocol
    // error in the file and the user should be notified before continuing or 
    // halting the program.
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 提交 Kattis “OddGnome”问题时出错。 我在本地得到正确的输出,我的代码有什么问题? C++ - Getting error on submission for Kattis "OddGnome" problem. I am getting the right output locally, what is wrong with my code? C++ 链表 - AppendLastNToFirst (C++),我只写了问题的逻辑。 请告诉我更正 - Linked list - AppendLastNToFirst (C++), I have written only the logic for the problem. Please tell me the correction Objective C和c ++导入问题。 很简单,但我不明白 - Objective C and c++ import problem. Simple but I don't get it 我如何在我的代码 C++ 中解决这个错误 - How can i solve this error in my code C++ C ++:可能的Xcode(Mac)问题。 无法使用getline()从外部文件中读取数字 - C++: possible Xcode (Mac) problem. Can't read in numbers from external file using getline() C++ 链表问题。 我想知道为什么我的 output 是正确的。 我想我还没有连接第一个节点 - C++ Linked list problem. i want to know why my output is correct. i think i didn't connect first node yet 我不明白如何解决 memory 泄漏 C++ 的问题 - I can't understand how to solve problem with memory leak C++ 我找不到什么问题。 莫吉烤,unicode - i can't find what is problem. mojibake, unicode C++ 出现控制台错误。 我无法识别我的源代码的问题 - C++ getting console error. I cant identify problem with my source code C ++共同祖先问题。 它是什么? - C++ Common Ancestor Problem. What is it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM