[英]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.