Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 程序终止于';std::超出范围';基本字符串::子字符串:\u位置_C++_Runtime Error - Fatal编程技术网

C++ 程序终止于';std::超出范围';基本字符串::子字符串:\u位置

C++ 程序终止于';std::超出范围';基本字符串::子字符串:\u位置,c++,runtime-error,C++,Runtime Error,我正在为一个类编写一个程序,在这个类中,我必须使用getline从文件中提取字符串,然后使用一种格式将其打印到一个新文件中。我编译得很好,但由于某种原因,我的程序崩溃了,尽管我认为所有substr调用都应该在字符串的范围内。这是我的密码: #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <cctype> #inc

我正在为一个类编写一个程序,在这个类中,我必须使用getline从文件中提取字符串,然后使用一种格式将其打印到一个新文件中。我编译得很好,但由于某种原因,我的程序崩溃了,尽管我认为所有substr调用都应该在字符串的范围内。这是我的密码:

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <cctype>
#include <cstdlib>    
#include <ctime>

using namespace std;

struct information{
    string first, middle, last, contribution;
    int birthyear;

};

int main()
{
    int i;
    information employee[1000];
    ifstream fin;
    ofstream fout;
    string input;
    int comma; //stores the location of the comma
    int space, space2, space3; //store the location of the spaces.
   int dollarsign; //checks where the dollar sign is and then the numbers                 after. 


fin.open("oldretirement.txt"); //opening input..
if (fin.fail()){
    fout<<"\nError! failed to open file!";
    exit(1);
}

fout.open("newretirement.txt");
if (fout.fail()){
    fout<<"\nError! failed to open file!";
    exit(1);
}
while(!fin.eof()){ //loops till file failure
    getline(fin, input);

    comma = input.find(",");

    if (isalpha(input[comma-1])){
        space = input.find(" "); //finds first space

        employee[i].last = input.substr(0, space-2);

        space2 = input.find(" ", space + 1); //finds the next space

        employee[i].first = input.substr(space+1, space2-space); //gets the substring of the area between spaces

        space3 = input.find(" ", space2+1); //you get the idea

        employee[i].middle = input.substr(space2+1, space3-space2);

        employee[i].birthyear = atoi((input.substr(space3+1, 4)).c_str());

        employee[i].contribution = input.substr(space3+5);
    }
    else{
        space = input.find(" ");

        employee[i].first = input.substr(0, space-1);

        space2 = input.find(" ", space+1);

        employee[i].middle = input.substr(space+1, space2-space);

        space3 = input.find(" ", space2+1);

        employee[i].last = input.substr(space2+1, space3-space2);

        employee[i].birthyear = atoi((input.substr(space3, 4)).c_str());

        dollarsign = input.find("$");

        employee[i].contribution = input.substr(dollarsign+2);
    }
    i++;
}
i--; //makes sure to make the number of indexes not too many.
int money_made; //something to save how much money they made the company other than a string, so that it is easier to compare.
string conversion; //something to store a string into to make things easier.
for (int j= 0;j<i;j++){ //looping for printing stuff
    fout<<employee[j].last<<", "<<employee[j].first<<" "<<employee[j].last<<" "<<employee[j].birthyear<<employee[j].contribution;

    conversion = employee[j].contribution;

    conversion.erase((conversion.find(","), 1)); //erases the comma to make conversion to int easier.

    money_made = atoi(conversion.c_str()); //converts string to integer

    int age = (2017 - employee[j].birthyear);
    if( (money_made > 300000)&&(age >= 60))
       fout<<" Ready\n"; //
    else
        fout<<endl;
}
fin.close();
fout.close();

}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
结构信息{
先串、中间串、最后串、贡献串;
int生日;
};
int main()
{
int i;
信息员工[1000];
流鳍;
流式流量计;
字符串输入;
int逗号;//存储逗号的位置
int space,space2,space3;//存储空格的位置。
int dollarsign;//检查美元符号的位置,然后检查后面的数字。
fin.open(“oldretirement.txt”);//打开输入。。
if(fin.fail()){

foutFind可能会失败。您需要检查这一点,特别是因为您使用的是
eof()
,这通常是错误的。请使用
while(getline(…)
。我将while(!fin.eof())替换为while(getline(fin,input))正如你所建议的,但是我仍然有问题。这个程序的目的是只读取上面描述的那些类型的行,所以当查找一行时,find应该始终返回一个空格,当我需要查找这些行时,find应该始终返回一个美元符号。正如@retiredInja所说,
while(!fin.eof())
可能没有达到预期效果。声明
请注意,此函数返回的值取决于对流执行的最后一个操作(而不是下一个操作)。
So
fin.eof()
在读取并点击EOF之后为真,而不是之前。可能是substr上的数学错误,或是查找失败。是时候使用调试器了。