开发者

Why does this class freeze the program?

开发者 https://www.devze.com 2023-04-03 14:56 出处:网络
This class should read some lines from a data and from them to variables which i can use in further operations. but for some reason it freezes if it detected the data and try to load it. why?

This class should read some lines from a data and from them to variables which i can use in further operations. but for some reason it freezes if it detected the data and try to load it. why? i can't see my mistake hope u can help me.

btw: the strange language which is used in the class is german^^ hope this doesn't matter.

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())开发者_StackOverflow{
            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();
    }

I will explain the structer of one loop with an example

     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.
         }
     }

hope u can help me.


What I see in your loop is, that you don't read any line if you are not at the line you want to be at. But as you don't read any line, you will not get further to the line you want to read. That does not only prevent you from reaching the line you actually want to read, but also from reaching the eof in acceptable time (you will, after guthaben was overflowed as many times as you have lines in your file). So what you have to do is read the line in all circumstances and discard the value if you don't need it. Try:

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);
     }

or

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.
          }
     }

Note that this will lead to issues with your current style of reading. it would be better to have everything in one loop, as e.James suggested:

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;
         }
}

Also note that it is very bad style to have the variables Guthaben and guthaben which can be confused easily (if they aren't already). I would suggest to you that you rename guthaben to zeile because it defines on which line you want to read.


You only read a line if guthaben is 6 - else you spin indefinitely (until eof - which may not be the case). For all other values of guthaben, you are not consuming from the stream user1 - so how do you expect to hit eof?


Your code is a mix of C and C++, but that is not (necessarily) a problem. The main problem is the way you are handling the file input loop. As a supplement to the already posted answers, here is a C++ loop which should do what you want:

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; }
    ...
}
0

精彩评论

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

关注公众号