Need help finding the maximum, minimum and average of the sales amount(from a file) without using an array.

#include<iostream>
#include<string>
#include<fstream>

using namespace std;

enum MonthType { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC };


MonthType stringtoMonthType(string m) {
if (m == "January") return JAN;
else if (m == "February") return FEB;
else if (m == "March") return MAR;
else if (m == "April") return APR;
else if (m == "May") return MAY;
else if (m == "June") return JUN;
else if (m == "July") return JUL;
else if (m == "August") return AUG;
else if (m == "September") return SEP;
else if (m == "October") return OCT;
else if (m == "November") return NOV;
else return DEC;


}
struct Date {
int year;
int day;
MonthType month;
};



struct SalesPerson {

string firstName, lastName;
int age, ssn;
double salesAmount;
Date dateOfLastSale;
};


void PrintRecord(SalesPerson& sp) {
cout << "First Name = " << sp.firstName << endl;
cout << "Last Name = " << sp.lastName << endl;
cout << "Age = " << sp.age << endl;
cout << "Social Security Number = " << sp.ssn << endl;
cout << "Sales Amount = " << sp.salesAmount << endl;


cout << "Date Of Last Sale = " << sp.dateOfLastSale.month << "/"
<< sp.dateOfLastSale.day << "/" << sp.dateOfLastSale.year << endl;

}

int main()
{
ifstream infile;
infile.open("sales-info.txt");
string firstName, lastName, monthStr;
int age, ssn, year, day;
double salesAmount;
while (infile >> firstName >> lastName >> age >> ssn >> salesAmount
>> year >> monthStr >> day) {
cout << "Line:" << firstName << lastName << age << ssn << salesAmount
<< year << monthStr << day << endl;
SalesPerson sp;
sp.firstName = firstName;
sp.lastName = lastName;
sp.age = age;
sp.ssn = ssn;
sp.salesAmount = salesAmount;
sp.dateOfLastSale.year = year;
sp.dateOfLastSale.day = day;
sp.dateOfLastSale.month = stringtoMonthType(monthStr);

PrintRecord(sp);
}

int a;
cin >> a;

return 0;
}
Last edited on
I can provide the file and the output if anyone need to see them.
It's always a good idea to provide the input file so people can run the program.
What actually is your problem ?
Teresa Aikman 59 393326979 24281 2018 April 3
Chris Woodley 36 313724612 30170 2018 July 21
Teresa Cohen 25 477186851 18010 2017 January 29
Barack Baldwin 34 400875308 34894 2018 January 1
Marilyn Speith 17 469553693 34519 2017 November 25
Vito Fitzgerald 21 384869870 37277 2017 January 17
Troy Johnson 64 545843439 32509 2018 May 28
Jennifer Akleman 21 343442984 25955 2018 July 2
Anne Adams 67 475076525 12525 2018 August 18
Ezekiel Lind 25 387956497 17603 2018 August 8
Mary Hill 35 427315876 38909 2017 December 9
Ron Akleman 35 507119347 20840 2017 August 28
Darius Fitzpatrick 23 513452090 14879 2018 March 24
Dan Akleman 48 398422017 23640 2017 September 10
Name the file "sales-info". Need to find the highest and lowest sales amount out of all of them. Also the average.
> Need to find the highest and lowest sales amount out of all of them. Also the average.

For doing this, we need to extract only one field (the sales amount) from each sales record (line).

Something like this would suffice:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

struct sales_rec
{
    std::string text ; // the full test of the record
    double amount ; // the sales amount extracted from the text
};

bool parse( std::string line, sales_rec& sales )
{
    // create a string stream to extract the fields from the line
    std::istringstream stm(line) ;

    std::string first_name, last_name ;
    unsigned long long age, ssn ;
    double sales_amount ;

    if( stm >> first_name >> last_name >> age >> ssn >> sales_amount )
    {
        sales = { line, sales_amount } ;
        return true ; // successfully parsed
    }

    else return false ; // parse error
}

// read in one sales record; put the stream into a failed state if input failed
std::istream& operator>> ( std::istream& stm, sales_rec& sales )
{
    std::string line ;

    if( std::getline( stm, line ) )
    {
        if( line.empty() ) return stm >> sales ; // skip empty lines
        if( !parse( line, sales ) ) stm.clear(stm.failbit) ; // parse error: input failed
    }

    return stm ;
}

bool print_min_max_avg_sales( std::istream& stm )
{
    sales_rec min_sales { "", 1.e+50 } ; // initialise with impossibly high amount
    sales_rec max_sales { "", -1.e-50 } ; // initialise with impossibly low amount
    double total = 0.0 ;
    std::size_t cnt = 0 ;

    sales_rec curr {} ;
    while( stm >> curr ) // for each sales record read in successfully
    {
        ++cnt ;
        total += curr.amount ;
        if( curr.amount < min_sales.amount ) min_sales = curr ;
        if( curr.amount > max_sales.amount ) max_sales = curr ;
    }

    if( cnt > 0 )
    {
        std::cout << " #records: " << cnt << '\n'
                  << std::fixed << std::setprecision(2)
                  << "min sales: " << min_sales.amount << "  (" << min_sales.text << ")\n"
                  << "max sales: " << max_sales.amount << "  (" << max_sales.text << ")\n"
                  << "total amt: " << total << '\n'
                  << "  average: " << total / cnt << '\n' ;
    }

    return cnt > 0 ; // true if at least one record was read and results were printed
}

int main()
{
    std::istringstream test_stm( // we use a string stream for testing
R"(Teresa Aikman 59 393326979 24281 2018 April 3
Chris Woodley 36 313724612 30170 2018 July 21
Teresa Cohen 25 477186851 18010 2017 January 29
Barack Baldwin 34 400875308 34894 2018 January 1
Marilyn Speith 17 469553693 34519 2017 November 25
Vito Fitzgerald 21 384869870 37277 2017 January 17
Troy Johnson 64 545843439 32509 2018 May 28
Jennifer Akleman 21 343442984 25955 2018 July 2
Anne Adams 67 475076525 12525 2018 August 18
Ezekiel Lind 25 387956497 17603 2018 August 8
Mary Hill 35 427315876 38909 2017 December 9
Ron Akleman 35 507119347 20840 2017 August 28
Darius Fitzpatrick 23 513452090 14879 2018 March 24
Dan Akleman 48 398422017 23640 2017 September 10
)"
                               ) ;

    if( !print_min_max_avg_sales(test_stm) )
        std::cout << "failed to read even one sales record\n" ;
}

http://coliru.stacked-crooked.com/a/1f25954c275ec5b8
Thanks I test this out when I have the time(probably tomorrow). Also the file I share wasn't the whole thing since the length was about 25,000, which the comment only allow up to 8,192.
Topic archived. No new replies allowed.