Menu

reading from a file into an array

2009-08-03
2012-09-26
  • Holly Benton

    Holly Benton - 2009-08-03

    Basic 3 stuff.

    Dev-C++ 4.9.9.2, Windowz XP Pro

    Basic code I just moved to a clean compllier to run and fix what i needed to fix.

    I am haveing problems with reading from a file and storing the infomation to a struct.
    I get the program to read and print out on the screen ( that is how i test to make sure it opens).

    I also have some code after the file to make sure that it prints out after the file. the code between that and the inData.seekg(0); is my problem. I have posted my recent one.

    this is no error message, but when i compile it it just ends at the end of the file and does not print out the bottom message.

    trying to print info from the struct.

    thanks for helping, i think i got everthing.

    Akumanight

    cout << "total customers: " << total_customer << endl;

    include <iostream>

    include <fstream>

    include <string>

    include <iomanip>

    using namespace std;

    enum account_type
    {
    unknown, Checking, Savings, Credit_Card
    }; //customers account type
    account_type registered;

    struct Bank_Customer_Record
    {
    char name[100];
    float amount;
    account_type account;
    };

    void database();

    int main()
    {
    database();

    system("PAUSE"); //needed in bloodshed to pause program
    return EXIT_SUCCESS;
    }

    //reads the database of names and stores them in a struct
    void database()
    {
    cout << fixed << showpoint << setprecision(2);

     ifstream inData;
    
     inData.open(&quot;bank.txt&quot;);  //opens the file
    
     string line;
     char username[10];
     float amount;
     string type;
     int counter;
     int total_customer =0;
     Bank_Customer_Record customer;
     int total_amount = 0;
     char name[100];
    
        while(!inData.eof())
        {
            getline(inData, line);
            cout &lt;&lt; line &lt;&lt; endl;
    
        }
        cout &lt;&lt; endl; //adds some space between the echo text 
    cout &lt;&lt; endl; //file and the averages.
    // Must clear the end-of-file flag to be able to rewind and re-read from the beginning.
    inData.clear();
    // 'Rewind' the input stream to the beginning - i.e. byte 0
    inData.seekg(0);
    
    //this is my problem code
    while(!inData.eof())
    {
    
          for(counter =0; counter &lt; 100; counter++)
          {
              inData &gt;&gt; customer.name
                     &gt;&gt; customer.amount;
                    // &gt;&gt; customer.registered;
               total_customer++;   
           }  
           // Must clear the end-of-file flag to be able to rewind and re-read from the beginning.
    inData.clear();
    // 'Rewind' the input stream to the beginning - i.e. byte 0
    inData.seekg(0);   
    }
    
    cout &lt;&lt; &quot;total customers: &quot; &lt;&lt; total_customer &lt;&lt; endl;
    cout &lt;&lt; &quot;total amount: &quot; &lt;&lt; total_amount &lt;&lt; endl;
    cout &lt;&lt; &quot;\n&quot; &lt;&lt; endl;
    
    cout &lt;&lt; &quot;total amount of customers: &quot; &lt;&lt; total_customer &lt;&lt; endl;
    cout &lt;&lt; &quot;name: &quot; &lt;&lt; customer.name &lt;&lt; endl;
    cout &lt;&lt; &quot;amount: &quot; &lt;&lt; customer.amount &lt;&lt; endl;
    //cout &lt;&lt; &quot;type: &quot; &lt;&lt; customer.type &lt;&lt; endl;
    cout &lt;&lt; &quot;\n&quot; &lt;&lt; endl;
    
        inData.close();  //closes the file
        //needs to be able to close 
        //dose not right now
    

    }

    Basic code I just moved to a clean compllier to run and fix what i needed to fix.

     
    • Holly Benton

      Holly Benton - 2009-08-03

      alright i tried

      compiles but now is stuck on an infinet loop with the first name in the text file.

      while(!inData.eof())
      {
      getline(inData, line);
      cout << line << endl;
      for(counter =0; counter < 100; counter++)
      {
      inData >> customer.name
      >> customer.amount;
      // >> customer.registered;
      total_customer++;
      }

          }
      

      cout << endl; //adds some space between the echo text
      cout << endl; //file and the averages.
      // Must clear the end-of-file flag to be able to rewind and re-read from the beginning.
      inData.clear();
      // 'Rewind' the input stream to the beginning - i.e. byte 0
      inData.seekg(0);

      cout &lt;&lt; &quot;total customers: &quot; &lt;&lt; total_customer &lt;&lt; endl;
      cout &lt;&lt; &quot;total amount: &quot; &lt;&lt; total_amount &lt;&lt; endl;
      cout &lt;&lt; &quot;\n&quot; &lt;&lt; endl;
      
      cout &lt;&lt; &quot;total amount of customers: &quot; &lt;&lt; total_customer &lt;&lt; endl;
      cout &lt;&lt; &quot;name: &quot; &lt;&lt; customer.name &lt;&lt; endl;
      cout &lt;&lt; &quot;amount: &quot; &lt;&lt; customer.amount &lt;&lt; endl;
      //cout &lt;&lt; &quot;type: &quot; &lt;&lt; customer.type &lt;&lt; endl;
      cout &lt;&lt; &quot;\n&quot; &lt;&lt; endl;
      

      thanks

       
    • Holly Benton

      Holly Benton - 2009-08-03

      the bank.txt file is just a basic file like this

      Name
      amount
      type

      Ron Harold
      1467.45
      Checking

      Tiffany Mace
      345.23
      Credit Card

      Thanks

       
    • Holly Benton

      Holly Benton - 2009-08-03

      i have tried

      no error when i check, but when i compile it is stuck on an infitle loop with hello.

      while(inData)
      {
      cout << "hello" << endl;
      for(counter =0; counter < 100; counter++)
      {
      inData >> customer.name
      >> customer.amount;
      // >> customer.registered;
      total_customer++;
      }
      // Must clear the end-of-file flag to be able to rewind and re-read from the beginning.
      inData.clear();
      // 'Rewind' the input stream to the beginning - i.e. byte 0
      inData.seekg(0);
      }

      cout &lt;&lt; &quot;total customers: &quot; &lt;&lt; total_customer &lt;&lt; endl;
      

      inData.close(); //closes the file
      //needs to be able to close
      //dose not right now
      }


      tried this one does the same thing

      no error when i check, but when i compile it is stuck on an infitle loop with hello.

      while(!inData.eof())
      {
      cout << "hello" << endl;
      for(counter =0; counter < 100; counter++)
      {
      inData >> customer.name
      >> customer.amount;
      // >> customer.registered;
      total_customer++;
      }
      // Must clear the end-of-file flag to be able to rewind and re-read from the beginning.
      inData.clear();
      // 'Rewind' the input stream to the beginning - i.e. byte 0
      inData.seekg(0);
      }

      so i am guessing that it is the second while(!inData.eof()) that is wrong.

      Reading it and saving it to the struct at the same time should work right? researching it now.

      Thanks

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.