Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C# problem - Reading and adding from file

  • 26-04-2006 04:01PM
    #1
    Registered Users, Registered Users 2 Posts: 10,205 ✭✭✭✭


    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, Registered Users 2 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