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:
What I want to do is read in and calculate the average rating for product 1 (the first number)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
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"; }



LinkBack URL
About LinkBacks


