Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C# problem - Reading and adding from file

Options
  • 26-04-2006 4:01pm
    #1
    Registered Users Posts: 10,210 ✭✭✭✭


    I'm doing a little practise before my exams in 2 weeks.

    Basicly, im writing a program that reads info from a file and displays it on screen, nice and simple.

    Now, lets say I have the following situation
    Name Sales Comission
    John 50 5
    Mary 100 10

    Now, ive gotten the info from the file using field[0], field[1] etc....

    BUT, how would I go about Adding the total sales together? Any help much appreciated! (Or a link to info on the web!)


Comments

  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    Why not as you read in a record you read each into an array of strings called field like you suggested. Then add name's to a string array, sales to an int array and commission to an int array.

    Something like this
    string [] field = new string [ 3 ] ;
    ArrayList names = new ArrayList ( ) ;
    ArrayList sales = new ArrayList ( ) ;
    ArrayList commision = new ArrayList ( ) ;
    
    while ( records to read )
    {
      //... Read in record 
      names.Add ( field [ 0 ] ) ;
      sales.Add ( field [ 1 ] ) ;
      commision.Add ( field [ 2 ] ) ;
    }
    
    int total_sales = 0 ;
    for ( int i = 0 ; i < sales.Count ; i++ )
       total_sales += (int)sales [i] ;
    


Advertisement