Thread: Looping decision?

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    49

    Looping decision?

    Hello,

    I am trying to figure out how or what looping method
    to use to read multiple data from a file.
    The file has several income brackets (A, B, & C), and some ratings for product1 and product2.
    The data file looks like this:
    Code:
    A 5 7
    A 4 8
    A 3 9
    B 2 7
    C 1 6
    C 8 2
    A 7 5
    B 6 9
    What I want to do is read in and calculate the average rating for product 1 (the first number)
    that bracket A rated, then bracket B,
    and finally C.
    I have figured out how to read A or B or C,
    by using an if statement and simply changing
    the argument to A or B or C,
    but I don't know how to have it read the data for A,
    then read B, and then C without intervening.
    Here is the code I have so far:

    Code:
    /* This program will display for each income bracket 
    (A,B, & C) the average rating for
     * product 1. */
    
    #include <iostream>    // cout, cin, <<, >>
    #include <string>      // string
    #include <cstdlib>     // exit()
    #include <cassert>     // assert
    #include <fstream>     // ifstream
    using namespace std;
    
    char income,
              c;
    int count,
    	incomeB5Up,
    	prod2Ave;
    double prod1Total;
    double prod1AveA,
           prod1AveB;
    double prod1,
           prod2;
    
    int main()
    {
    	string fileName;
    	cout << "Enter name of file: ";
    	getline(cin, fileName);
    
    	ifstream inFile(fileName.data());
    	assert(inFile.is_open() );
    
    	while (inFile.get (c) )
    	{
    		
    		 if (c == 'A')  /* get income bracket A information */
    		{
    			count++;
    			inFile >> prod1 >> prod2;			           
    			cout << "Product 1 rating: " << prod1 << " Product 2 rating: " 
    				 <<  prod2 << " Count: " << count << "\n";  // display prod ratings read from file
                prod1Total += prod1;
    			cout << "Prod1Total = " << prod1Total << "\n";    // display prod1 total
    			prod1AveA = prod1Total / count;
    			cout << "Average for Product 1 in income class A: " << prod1Ave << "\n\n";																			   
    		}
    		    
            
    	}
    	cout << "Average for Product 1 in income class A: " << prod1AveA << "\n\n";	
    
        }
    Semper Fi!

  2. #2
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    the structure of the file is illusion

    read the file as a continuous stream of single characters

    encounter A, B, or C - set an index in a two dimensional array of averages

    next character should be a digit - set an indexto the product in your two dimensional array

    next character is your score - combine it into the average: (average[incomebracket][productnumber] + rating) / 2

    hope this helps
    .sect signature

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    49
    I don't know how to use arrays yet.
    Semper Fi!

  4. #4
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    here is forbidden knowledge

    float array[3][3];

    now you can access array[x][y], where 0 <= x <= 2 and 0 <= y <= 2 and use it as a spreadsheet-like block of floats
    .sect signature

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 01:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 06:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 01:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:12 AM