无法解析文件C ++

时间:2017-11-09 20:13:46

标签: c++ parsing getline

所以我变得非常沮丧,我的大脑对我的代码变得混乱。我试图通过一个mile.dat文件进行解析,该文件的格式为:

Youngstown, OH[4110,8065]115436
Yankton, SD[4288,9739]12011
966
Yakima, WA[4660,12051]49826
1513 2410
Worcester, MA[4227,7180]161799
2964 1520 604
Wisconsin Dells, WI[4363,8977]2521
1149 1817 481 595
Winston-Salem, NC[3610,8025]131885
927 729 2742 1289 494

我试图提取城市的名称,然后在下一行,上面所有以前的城市都有成本。我的代码非常接近,但它不断地将每个数字放在一起,而不仅仅是整个数字。我希望代码输出" 1513"和" 2410"所以我可以将这些数字放入我的邻接矩阵中。

目前,输出为:

Stored: Youngstown, OH
City Count: 1
Press any key to continue . . .
Stored: Yankton, SD
City Count: 2
Press any key to continue . . .
Line: 966
Inputted the cost: 966
Stored: Yakima, WA
City Count: 3
Press any key to continue . . .
Line: 1513 2410
Inputted the cost: 1
Inputted the cost: 15
Inputted the cost: 151
Inputted the cost: 1513
Inputted the cost: 2
Inputted the cost: 24
Inputted the cost: 241
Inputted the cost: 2410
Stored: Worcester, MA
City Count: 4
Press any key to continue . . .
Line: 2964 1520 604
Inputted the cost: 2
Inputted the cost: 29
Inputted the cost: 296
Inputted the cost: 2964
Inputted the cost: 1
Inputted the cost: 15
Inputted the cost: 152
Inputted the cost: 1520
Inputted the cost: 6
Inputted the cost: 60
Inputted the cost: 604
Stored: Wisconsin Dells, WI
City Count: 5

我的代码是:

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
    //Turn bracket into int value of "91" (it's ascii code)
    string brack = "[";
    char b = brack[0];
    int bracket = int(b);
    //Variable Declarations
    int city_cost[127][127];
    string cities[127];
    string cost = "";
    int cost_bit = 0;
    int nl_bit = 0;
    int line_count = 1, city_count = 0;
    string line;

    ifstream myfile("miles.dat");
    if (myfile) 
    {
        while (getline(myfile, line))  
        {
            //if the first letter is a alphanumeric letter then dont enter the cost entry section
            if (isalpha(line[0])) {
                cost_bit = 0;
            }
            //Store costs to every other city
            if (cost_bit == 1) {
                cout << "Line: " << line << endl;

                //Seperate numbers in each line char by char, combine, then put that cost in the appropriate city
                for (unsigned int i = 0; i < line.length(); i++) {
                    //find "space" (32 in ascii table) and put the cost in the array, reseting that cost variable each time one is put in
                    char ch = line[i];
                    int ascii = int(ch);//convert char to int
                    if (city_count == 2) {
                        cost = line;
                        cout << "Inputted the cost: " << cost << endl;
                        break;
                    }
                    if (ascii == 32 || ascii == 10 || ascii == 13) {
                        cost = "";
                        continue;
                    }
                    cost = cost + char(ascii);
                    //iterate through each of the previous cities covered and put the cost into them
                    for (int city_count_dec = city_count; city_count_dec != -2; city_count_dec--) {
                        cout << "Inputted the cost: " << cost << endl;
                        city_cost[city_count][city_count_dec] = 0;//replace with cost!!!!!!!! first need to convert to integer (maybe) or just make the city_cost list a string list
                        break;
                    }
                }
                cost_bit = 0;
                cost = "";
            }
            //END store costs to every city

            string new_line = "";
            for (int i = 0; i < line.length(); i++) {
                char c = line[i];
                int ai = int(c);//convert char to int

                //skip useless data
                //store city name in array
                if (ai == bracket) {
                    cities[city_count] = new_line;
                    cout << "Stored: " << cities[city_count] << endl;
                    city_count++;
                    cout << "City Count: " << city_count << endl;
                    system("pause");
                    //set cost bit so we know that the next line will be the cost of the cities before it
                    cost_bit = 1;
                    continue;
                }
                else if(cost_bit == 0) {
                    new_line = new_line + c;
                }
            }
            line_count++;
        }
        myfile.close();
    }
    else cout << "Something went wrong!\n";
    cin.get();
    return 0;
}

1 个答案:

答案 0 :(得分:1)

代码从不谎言。某些时候评论

for循环只会迭代一次,因为你立即中断。它也位于代码中的错误位置。

                    //iterate through each of the previous cities covered and put the cost into them
                    for (int city_count_dec = city_count; city_count_dec != -2; city_count_dec--) {
                        cout << "Inputted the cost: " << cost << endl;
                        city_cost[city_count][city_count_dec] = 0;//replace with cost!!!!!!!! first need to convert to integer (maybe) or just make the city_cost list a string list
                        break;
                    }

您的代码始终打印当前步骤,即使您不在数字的末尾。以下是您希望处理找到的数字的代码中的位置。

                if (ascii == 32 || ascii == 10 || ascii == 13) {
                    // This means that you finished parsing a number.
                    // Here, you should print the cost at the screen.
                    cost = "";
                    continue;
                }
相关问题