C++ C++;获取控制台错误。我无法识别源代码的问题

C++ C++;获取控制台错误。我无法识别源代码的问题,c++,C++,我的代码有问题,我不知道它到底在哪里。我将尝试向您解释我的程序是如何工作的。 例如,字母表中的一些字母 A = 1 B = 3 C = 7 D = 4 ... 我将这些值定义为字母 我将用程序短语abcd写,它的和为1+3+7+4=15,它将检查文件words.txt,如果它包含任何与字母组合的短语,总和等于15。例如,如果在words.txt中不是abcd,而是dbac,它也会显示这个短语,因为这个短语的总和也等于15。我希望你能理解 这是源代码: #include <iostream

我的代码有问题,我不知道它到底在哪里。我将尝试向您解释我的程序是如何工作的。 例如,字母表中的一些字母

A = 1
B = 3
C = 7
D = 4
...
我将这些值定义为字母

我将用程序短语abcd写,它的和为1+3+7+4=
15
,它将检查文件
words.txt
,如果它包含任何与字母组合的短语,总和等于
15
。例如,如果在
words.txt
中不是
abcd
,而是
dbac
,它也会显示这个短语,因为这个短语的总和也等于
15
。我希望你能理解

这是源代码:

#include <iostream>
#include <string>
#include <map>
#include <cmath>
#include <fstream>
#include <sstream>
#include <numeric>
#include <limits>
#include <utility>
#include <vector>
#include <algorithm>
#include <functional>
#include <array>

using namespace std;

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

map < char, int > make_pythagorean_map()
{
    return {
        { 'A', 1 },
        { 'a', 1 },
        { 'B', 2 },
        { 'b', 2 },
        { 'C', 3 },
        { 'c', 3 },
        { 'D', 4 },
        { 'd', 4 },
        { 'E', 5 },
        { 'e', 5 },
        { 'F', 6 },
        { 'f', 6 },
        { 'G', 7 },
        { 'g', 7 },
        { 'H', 8 },
        { 'h', 8 },
        { 'I', 9 },
        { 'i', 9 },
        { 'J', 1 },
        { 'j', 1 },
        { 'K', 2 },
        { 'k', 2 },
        { 'L', 3 },
        { 'l', 3 },
        { 'M', 4 },
        { 'm', 4 },
        { 'N', 5 },
        { 'n', 5 },
        { 'O', 6 },
        { 'o', 6 },
        { 'P', 7 },
        { 'p', 7 },
        { 'Q', 8 },
        { 'q', 8 },
        { 'R', 9 },
        { 'r', 9 },
        { 'S', 1 },
        { 's', 1 },
        { 'T', 2 },
        { 't', 2 },
        { 'U', 3 },
        { 'u', 3 },
        { 'V', 4 },
        { 'v', 4 },
        { 'W', 5 },
        { 'w', 5 },
        { 'X', 6 },
        { 'x', 6 },
        { 'Y', 7 },
        { 'y', 7 },
        { 'Z', 8 },
        { 'z', 8 }
    };
}

map < char, int > make_simpleeng_map()
{
    return {
        { 'A', 1 },
        { 'a', 1 },
        { 'B', 2 },
        { 'b', 2 },
        { 'C', 3 },
        { 'c', 3 },
        { 'D', 4 },
        { 'd', 4 },
        { 'E', 5 },
        { 'e', 5 },
        { 'F', 6 },
        { 'f', 6 },
        { 'G', 7 },
        { 'g', 7 },
        { 'H', 8 },
        { 'h', 8 },
        { 'I', 9 },
        { 'i', 9 },
        { 'J', 10 },
        { 'j', 10 },
        { 'K', 11 },
        { 'k', 11 },
        { 'L', 12 },
        { 'l', 12 },
        { 'M', 13 },
        { 'm', 13 },
        { 'N', 14 },
        { 'n', 14 },
        { 'O', 15 },
        { 'o', 15 },
        { 'P', 16 },
        { 'p', 16 },
        { 'Q', 17 },
        { 'q', 17 },
        { 'R', 18 },
        { 'r', 18 },
        { 'S', 19 },
        { 's', 19 },
        { 'T', 20 },
        { 't', 20 },
        { 'U', 21 },
        { 'u', 21 },
        { 'V', 22 },
        { 'v', 22 },
        { 'W', 23 },
        { 'w', 23 },
        { 'X', 24 },
        { 'x', 24 },
        { 'Y', 25 },
        { 'y', 25 },
        { 'Z', 26 },
        { 'z', 26 }
    };
}

map < char, int > make_gfive_map()
{
    return {
        { 'A', 7 },
        { 'a', 7 },
        { 'B', 8 },
        { 'b', 8 },
        { 'C', 1 },
        { 'c', 1 },
        { 'D', 2 },
        { 'd', 2 },
        { 'E', 3 },
        { 'e', 3 },
        { 'F', 4 },
        { 'f', 4 },
        { 'G', 5 },
        { 'g', 5 },
        { 'H', 6 },
        { 'h', 6 },
        { 'I', 7 },
        { 'i', 7 },
        { 'J', 8 },
        { 'j', 8 },
        { 'K', 9 },
        { 'k', 9 },
        { 'L', 1 },
        { 'l', 1 },
        { 'M', 2 },
        { 'm', 2 },
        { 'N', 3 },
        { 'n', 3 },
        { 'O', 4 },
        { 'o', 4 },
        { 'P', 5 },
        { 'p', 5 },
        { 'Q', 6 },
        { 'q', 6 },
        { 'R', 7 },
        { 'r', 7 },
        { 'S', 8 },
        { 's', 8 },
        { 'T', 9 },
        { 't', 9 },
        { 'U', 1 },
        { 'u', 1 },
        { 'V', 2 },
        { 'v', 2 },
        { 'W', 3 },
        { 'w', 3 },
        { 'X', 4 },
        { 'x', 4 },
        { 'Y', 5 },
        { 'y', 5 },
        { 'Z', 6 },
        { 'z', 6 }
    };
}

map < char, int > select_map( int choice )
{
    switch( choice )
    {
    case 1:
        {
            return make_pythagorean_map();
        }
    case 2:
        {
            return make_simpleeng_map();
        }
    case 3:
        {
            return make_gfive_map();
        }
    }
}

int main()
{
    int cho = 0, d, help;
    char o;
    string phrasetwo;

    while( 1 )
    {
        system( "cls" );
        cout << "You want to use: " << endl << "1) Pythagorean Gematria" << endl << "2) English/Simple Gematria" << endl << "3) Extra: " << endl << "Gematria with G = 7" << endl << endl;
        cin >> phrasetwo;
        istringstream asd( phrasetwo );

        if( !( asd >> d ) || asd >> o )
        {
            system( "cls" );
            cout << "You want to use: " << endl << "1) Pythagorean Gematria" << endl << "2) English/Simple Gematria" << endl << "3) Extra: " << endl << "Gematria with G = 7" << endl << endl;
        }
        else
        {
            cho = stoi( phrasetwo );
            if( cho <= 3 && cho >= 1 )
            {
                break;
            }
        }
    }

    auto m = select_map( cho );

    string::size_type n;
    ifstream input( "words.txt" );
    ofstream output( "results.txt" );
    string str, phrase;
    int counter = 1, cnt = 0, choice = 0, chosennum = 0, asd = 0, f = 0, x;
    bool flag;
    char c;

    while( 1 )
    {
        system( "cls" );
        cout << "You want to check:" << '\n' << "1) Number" << '\n' << "2) Phrase" << endl;
        cin >> phrase;
        istringstream s( phrase );

        if( !( s >> x ) || s >> c )
        {
            system( "cls" );
            cout << "You want to check:" << '\n' << "1) Number" << '\n' << "2) Phrase" << endl;
        }
        else
        {
            choice = stoi( phrase );
            if( choice <= 2 && choice >= 1 )
            {
                break;
            }
        }
    }

    switch( choice )
    {
    case 1:
        {
            cout << "Which number do you want to check?: ";
            while( 1 )
            {
                cin >> phrase;
                istringstream s( phrase );

                if( !( s >> x ) || s >> c )
                {
                    cout << endl << "Which number do you want to check?: ";
                }
                else
                {
                    chosennum = stoi( phrase );
                    break;
                }
            }
            cout << endl << "All results found: " << endl;
            break;
        }

    case 2:
        {
            do
            {
                flag = false;
                cout << "Which phrase do you want to check?: ";

                cin.ignore( numeric_limits < streamsize >::max(), '\n' );
                getline( cin, phrase );

                for( int i = 0; i < phrase.length(); i++ )
                {
                    if( isdigit( phrase[ i ] ) )
                    {
                        flag = true;
                    }
                }
                cout << endl;
            } while( flag == true );

            for( char charr: phrase )
            {
                chosennum += m[ charr ];
            }
            cout << '\t' << phrase << " in gematria = " << chosennum << endl;
            break;
        }
    }
    int tablica[4], b = 0, easy, H = 1, sum;
    string str2;
    string::size_type G;
    vector<pair< int, string > > Datas;

    while( getline( input, str ) )
    {
        sum = 0;
        n = str.find( ',' );
        G = n;

        if( n != string::npos )
        {
            do
            {
                tablica[ b ] = n;
                b++;
                n = str.find( ',', n + 1 );
            } while( n != string::npos );

            str2 = str.substr( tablica[ 3 ] + 1 );
            easy = stoi( str2 );
            str.resize( G );
            b = 0;
        }

        for( int i = 0; i < str.length(); i++ )
        {
            if( ( ( int ) str[ i ] ) > 127 || ( ( int ) str[ i ] ) < 0 )
            {
                str.clear();
            }
        }

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

        if( sum == chosennum )
        {
            Datas.emplace_back(easy, str);
            help = sum;
            counter++;
        }
    }
    sort( Datas.begin(), Datas.end(), greater< pair< int, string > >() );

        for (const auto& e : Datas)
        {
            cout << "No[" << H << "]: " << e.second << '\t' << '\t' << '\t' << "Value In Gematria: " << help << '\t' << '\t' << '\t' << "Search Number: " << e.first <<'\n';
            output << "No[" << H << "]: " << e.second << '\t' << '\t' << '\t' << "Value In Gematria: " << help << '\t' << '\t' << '\t' << "Search Number: " << e.first <<'\n';
            H++;
        }

    cout << "\n\n You have found: " << ( counter - 1 ) << " phrases" << '\n';
    output << "\n\n You have found: " << ( counter - 1 ) << " phrases" << '\n';

    return 0;
}
但是当我将文件
words.txt
更改为包含近1,5mln行的文件时,控制台中出现了错误。以下是错误的屏幕截图:


我不知道发生了什么。期待帮助。谢谢您的回答。

放置一个
捕捉
并查看str2上的内容:

try {
    easy = stoi( str2 );
} catch (const std::invalid_argument & ex) {
    std::cerr << "Error string: " << str2 << ", " << ex.what() << std::endl;
}
试试看{
easy=stoi(str2);
}catch(const std::无效的参数&ex){

std::cerr向函数传递的参数无效,这会导致函数引发异常。很可能输入文件中有错误。我建议您输出传递给stoi的每个字符串,然后刷新输出(例如,使用
std::endl
),这是非常重要的。然后查看崩溃前的最后一个字符串输出。这将为您提供一些有关问题的线索。然后您可以从那里向后工作。这是一个很好的示例,说明了如何查找错误,编写一些代码以帮助您跟踪错误。对于将来的问题,请花一些时间阅读,然后进行SO,re广告,以及。最后,请学习如何回答您的问题,以及如何创建一个(强调最小部分)。我的输入文件有1,5mln行,看起来与上面显示的短部分完全一样。它只是更大。我如何解决我的问题?“看起来完全一样”.著名的临终遗言。好的,我发现了问题。谢谢兄弟。别让我们悬而未决-问题出在哪里?:-)
try {
    easy = stoi( str2 );
} catch (const std::invalid_argument & ex) {
    std::cerr << "Error string: " << str2 << ", " << ex.what() << std::endl;
}