开发者

MVS C++ error : string subscript out of range

开发者 https://www.devze.com 2023-02-20 01:15 出处:网络
I want to solve following task : There is given a text file \"pesel.txt\", which contains 150 national identities. Each line contains one national identity, which is a 11-digital number. First two di

I want to solve following task :

There is given a text file "pesel.txt", which contains 150 national identities. Each line contains one national identity, which is a 11-digital number. First two digits from the left determine year, which a person was born in, next two digits determine month and next two determine day.

To shorten :

digits 0-1 = year digits 2-3 = month digits 4-5 = day digits 6-11 = determine something else, what is not important here

I need to read the file, check how many people were born in december. I am trying to this in the following way :

  • read each line until end of file is reached
  • at each line I check whether third character in the string equals 1 and if fourth character equals 2, if yes I increment variable, that is my counter for people born in december, else next loop iteration is executed

Here is the code :

int _tmain(int argc, _TCHAR* argv[])
{

    ifstream file( "C:\\Kuba\\Studia & Nauka\\MATURA XDDD
                                  \\INFA\\1\\Dane_PR\\pesel.txt" );

    string line;
    int bornInDecember=0;

    if( !file.is_open() ){

        cout << "Cannot read the file." << endl ;

    }else{

        while( file.good() ){

            getline( file, line );

            if(  line[2] == '1' &&am开发者_运维知识库p; line[3] == '2'  ){ 

                bornInDecember++ ; // 0-1 year, 2-3 month, 4-5 day

            }

        }

        cout << "Amount of people born in december : "<< bornInDecember<< endl;

        file.close();
    }

    system("pause");

    return 0;
}

the problem is that I get the following error and I've no idea why..

http://img10.imageshack.us/i/mvserr.png/


while file.good() is wrong - getline will still fail. You read the last line of the file, process it, file.good() is still true, then you try to read one more line and getline fails.

You also need to check that the line is long enough before you access line[n] - or you'll get exactly the error you do get.

int _tmain(int argc, _TCHAR* argv[])
{
    ifstream file( "C:\\Kuba\\Studia & Nauka\\MATURA XDDD\\INFA\\1\\Dane_PR\\pesel.txt" );
    string line;
    int bornInDecember=0;
    if( !file.is_open() ){
        cout << "Cannot read the file." << endl ;
    } else {
        while (getline(file, line)) { // While we did read a line
            if (line.size() >= 4) { // And the line is long enough
            if(  line[2] == '1' && line[3] == '2'  ){  // We check the condition
                bornInDecember++ ; // 0-1 year, 2-3 month, 4-5 day
            }
            }
        }
        cout << "Amount of people born in december : "<< bornInDecember<< endl;
        file.close();
    }
    system("pause");
    return 0;
}


before the if, print out the line and see if it has the correct value, you can also check the length of the line before accessing it:

std::getline( file, line );
std::cout << line << std::endl;
if( line.size() >= 4 && line[2] == '1' && line[3] == '2'  )
...

You should also use while(std::getline(file, line)) instead of while(file.good())

If you write code and you expect a value to be something specific, you can use assert if the value is not as expected and you catch the error immediately.

#include <cassert>
assert(line.size() == 10 && "line size is not equal to 10");


Well. Obviously as assertion message states std::string subscript used in your program is out of range that is either subscript 2 (from line[2]) or subscript 3 (from line[3]). It means that one of the lines read is shorter than 4 characters and thus you have no such thing as fourth char (line[3]). May be that is the last line in a file that is possibly empty if you have trailing in your file.

As hidayat and Erik already written in their posts the least you can do is to check if the line is long enough.

0

精彩评论

暂无评论...
验证码 换一张
取 消