Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6571951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:56:45+00:00 2026-05-25T14:56:45+00:00

This class should read some lines from a data and from them to variables

  • 0

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()){
            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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:56:46+00:00Added an answer on May 25, 2026 at 2:56 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are many classes having this provider suffix. (Data,membership,modelmetadata,...). When should be a class
According to this article from Herb Sutter, one should always pick Class Specializing over
Background: I'm working on some code to read in data from a file. Examples
Ok, this should be dirt simple. I'm trying to read charactes from a serial
how should I document this piece of code: // Is this class? colors =
When should the keyword 'this' be used within C# class definitions? Is it standard
Edited Question: This should be clear. using System; namespace UpdateDateTimeFields { class Program {
Suppose I have a class with a string instance attribute. Should I initialize this
I'm working on watin script which read forms on e-shop. In this test I
My first template class try for a simplified converter from some boost compressed_matrix to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.