为什么这个课程会冻结程序?

时间:2011-09-09 12:28:48

标签: c++ class freeze void

这个类应该从数据中读取一些行,从它们读取变量,我可以在进一步的操作中使用这些行。但由于某种原因,如果它检测到数据并尝试加载它会冻结。为什么? 我无法看到我的错误希望你能帮助我。

顺便说一下:班上使用的奇怪语言是德语^^希望这没关系。

void Kunde::laden(){
        string inhalt_anrede, inhalt_vname, inhalt_nname, inhalt_knummer, inhalt_pin, inhalt_guthaben;
        int anrede, vorname, nachname, knummer, pin, guthaben;

        system("cls");
        cout << "wie ist ihr nachname?" << endl;
        cin  >> nname;

        user1.open(nname, ios::in);

        if(!user1.is_open()){
            cout << "Datei nicht gefunden" << endl;
        }

        if(user1.is_open()){

         for ( int anrede=0;!user1.eof();anrede++){
             if (anrede==1){
                 getline(user1, inhalt_anrede);       
                 strcpy(Anrede,inhalt_anrede.c_str());
             }
         }
         for ( int vorname=0;!user1.eof();vorname++){
             if (vorname==2){
                 strcpy(vname,inhalt_vname.c_str());
             }
         }
         for ( int nachname=0;!user1.eof();nachname++){
             if (nachname==3){
                 getline(user1, inhalt_nname);       
                 strcpy(nname,inhalt_nname.c_str());
             }
         }
         for ( int knummer=0;!user1.eof();knummer++){
             if (knummer==4){
                 getline(user1, inhalt_knummer);       
                 echte_kontonummer=atol(inhalt_knummer.c_str());
             }
         }
         for ( int pin=0;!user1.eof();pin++){
             if (pin==5){
                 getline(user1, inhalt_pin);       
                 echte_pin=atoi(inhalt_pin.c_str());
             }
         }
         for ( int guthaben=0;!user1.eof();guthaben++){
             if (guthaben==6){
                 getline(user1, inhalt_guthaben);       
                 Guthaben=atoi(inhalt_guthaben.c_str());
             }
         }
         cout << "Daten erfolgreich geladen." << endl;

         }
         user1.close();
    }

我将用一个例子

来解释一个循环的结构
     for ( int guthaben=0;!user1.eof();guthaben++){ //i think this is understandable
         if (guthaben==6){ //in this case the desired value is on line 6
             getline(user1, inhalt_guthaben);       
             Guthaben=atoi(inhalt_guthaben.c_str()); //from string to int to the desired value Guthaben.
         }
     }
希望你能帮助我。

3 个答案:

答案 0 :(得分:2)

如果guthaben6,您只会读取一行 - 否则您无限期地旋转(直到eof - 可能不是这种情况)。对于guthaben的所有其他值,您不会从流user1中消耗 - 所以您希望如何点击eof

答案 1 :(得分:2)

我在你的循环中看到的是,如果你不在你想要的路线上,你就不会读取任何一行。但是,由于您没有阅读任何一行,您将无法进一步了解您想要阅读的行。这不仅会阻止您到达您实际想要读取的行,而且还会阻止您在可接受的时间内到达eof(在guthaben溢出的次数与文件中的行数相同之后)。所以你要做的就是在所有情况下阅读该行,如果你不需要它,请丢弃该值。 尝试:

for ( int guthaben=0;!user1.eof();guthaben++){ //i think this is understandable
         if (guthaben==6){ //in this case the desired value is on line 6
             getline(user1, inhalt_guthaben);       
             Guthaben=atoi(inhalt_guthaben.c_str()); //from string to int to the desired value Guthaben.
         }else
             getline(user1, inhalt_guthaben);
     }

for ( int guthaben=0;!user1.eof();guthaben++){ //i think this is understandable
             getline(user1, inhalt_guthaben);       
        if (guthaben==6){ //in this case the desired value is on line 6
             Guthaben=atoi(inhalt_guthaben.c_str()); //from string to int to the desired value Guthaben.
          }
     }

请注意,这会导致您当前的阅读方式出现问题。如e.James建议的那样,将所有内容放在一个循环中会更好:

for ( int zeile=1;!user1.eof();zeile++){//Natural counting (beginning at 1)!
    std::string inhalt;
    getline(user1, inhalt);
    switch(zeile){
             case 1:
                strcpy(Anrede,inhalt.c_str());
             break;
             case 2:
                 strcpy(vname,inhalt.c_str());
             break;
             case 3:
                 strcpy(nname,inhalt.c_str());
             break;
             case 4:
                 echte_kontonummer=atol(inhalt.c_str());
             break;
             case 5:
                 echte_pin=atoi(inhalt.c_str());
             break;
             case 6:
                 Guthaben=atoi(inhalt.c_str());
             break;
         }
}

另请注意,让变量Guthabenguthaben容易混淆(如果它们尚未存在)是非常糟糕的风格。我建议您将guthaben重命名为zeile,因为它定义了您想要阅读的行。

答案 2 :(得分:1)

您的代码是C和C ++的混合,但这不是(必然)的问题。主要问题是您处理文件输入循环的方式。作为已经发布的答案的补充,这里是一个C ++循环,应该做你想要的:

std::string anrede;
std::string vorname;
...
for (int line_number=0; input_file.good(); line_number++){
    getline(input_file, input_line);
    if (line_number==1) { anrede = input_line; }
    if (line_number==2) { vorname = input_line; }
    ...
}
相关问题