简体   繁体   English

C++ 出现控制台错误。 我无法识别我的源代码的问题

[英]C++ getting console error. I cant identify problem with my source code

I have problem with my code and I dont know where exactly it is.我的代码有问题,我不知道它到底在哪里。 I will try explain you how my program works.我将尝试向您解释我的程序是如何工作的。 For example some letters of the alphabet;例如一些字母表中的字母;

A = 1
B = 3
C = 7
D = 4
...

I defined these values to letters我将这些值定义为字母

And I will write in program phrase abcd and it will sum 1+3+7+4= 15 and it will check file words.txt also if it contains any phrase combined with letters which sum equals to 15 .我将写在程序短语 abcd 中,它的总和为 1+3+7+4= 15 ,它还将检查文件words.txt是否包含任何与总和等于15的字母组合的短语。 If in words.txt would be for example not abcd but lets say dbac it will show you this phrase too because sum of this phrase also equals to 15 .例如,如果在words.txt中不是abcd ,而是说dbac ,它也会向您显示这个短语,因为这个短语的总和也等于15 I hope you understand.我希望你明白。

This is source code:这是源代码:

#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;
}

And program works properly.并且程序正常工作。 Everything is fine if my file words.txt looks in that way:如果我的文件words.txt看起来像这样,一切都很好:

it wasnt humans, 1609, 1092, 182, 18
a worm wearing a human suite, 2787, 1674, 279, 18
a human full of worms, 1732, 1308, 218, 0
they call it symbiosis, 1436, 1470, 245, 6
angry abortions, 950, 1068, 178, 4

But when Im changing file words.txt to the file which contains almost 1,5mln lines I have error in console.但是,当我将文件words.txt更改为包含近 150 万行的文件时,我在控制台中出现错误。 Here is screenshot of error: Screenshot Of Error这是错误的屏幕截图:错误的屏幕截图

And I dont know what is going on.而且我不知道发生了什么。 Looking forward for help.期待帮助。 Thanks for answers.感谢您的回答。

Put a catch and see what is on str2 : catch看看str2上有什么:

try {
    easy = stoi( str2 );
} catch (const std::invalid_argument & ex) {
    std::cerr << "Error string: " << str2 << ", " << ex.what() << std::endl;
}

暂无
暂无

声明:本站的技术帖子网页,遵循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++ 没有匹配到&#39;operator &lt;&#39;错误。 C ++ - Getting no match for 'operator<' error. C++ 为什么我在执行 C++ 代码时出错 - why I'm getting error in executing my C++ code 我收到一段代码的警告,但 bjarne stroustrup 的 c++ 书说它应该是一个错误。 这里有什么? - I am getting the warning for a piece of code, but the c++ book by bjarne stroustrup is saying it should be an error. what is right here? C++ 无法解决我的问题。 我有控制台错误 - C++ can't solve my problem. I have console error 我的C ++代码出现错误! &如何在c ++中找到矩阵的行列式和逆矩阵? - I'm getting error in my c++ code! & How to find determinant and inverse of matrix in c++? 我正在将官方SQLite源代码添加到我的C ++项目中,并出现链接器错误 - i was adding offocial SQLite source code to my C++ project and gets a linker error 如何识别源代码中的所有 C++ 标准库调用? - How to identify all C++ standard library calls in source code? 获得“ISO C ++禁止声明......”错误。 - Getting “ ISO C++ forbids declaration …” error.? 为什么我的 C++ 代码中出现错误“预期类型名称”错误? - Why am I getting error ''Type name expected' error in my C++ code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM