Extract specific data

I am trying to extract data from a txt file. the file has 16 columns and 10 rows. I need to read in the first 4 columns as they are, then use the last 12 columns in a function that outputs the sum after using a switch to determine a points system value... I think I worded that correctly.
It's supposed to be like: MemberID, LastName, FirstName, Phone#... then how many visits per month (12 colums, 12 months)
This is what I have:

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

using namespace std;

int main()
{
/// Declare variables
string memberID, lastName, firstName;
int phoneNumber, visits, points;
int i = 1, j = 1, sum ;

/// Print welcome
cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << endl;
cout << "* Welcome to Norfolk Garden Rewarding Program! *" << endl;
cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << endl;
cout << endl;

/// Declare input data and open file
ifstream inData;
inData.open("garden.txt");

/// start loop

cout << left
<< setw(12) << "Member ID"
<< setw(13) << "Last Name"
<< setw(13) << "First Name"
<< setw(15) << "Phone Number"
<< setw(15) << "Total Points"
<< endl;

for (i = 1; i <=3; i++)
{
/// Read in input data
inData >> memberID >> lastName >> firstName >> phoneNumber >> visits;

cout << left
<< setw(12) << memberID
<< setw(13) << lastName
<< setw(13) << firstName
<< setw(15) << phoneNumber;




for (j = 1; j <=15; j++)
{
switch (visits)
{
inData >> visits;
case '0':
points = 0;
break;
case '1':
points = 5;
break;
case '2':
points = 15;
break;
case '3':
points = 30;
break;
default:
points = 60;
break;

}
cout << setw(15) << sum;
sum = sum + visits;

}
cout.ignore(100, '\n');
}

cout << endl;
inData.close();

return 0;
}


This is the input text:

35247 Wang James 1571767873 1 1 4 6 6 6 10 8 1 3 2 9
10001 Smith Mary 1579837617 0 8 3 3 4 8 6 7 1 2 5 3
11002 Johnson Jennifer 1571672876 7 2 8 4 9 1 2 3 2 3 3 3
74647 Yang Susan 1574762892 2 5 4 4 3 0 8 9 5 1 1 8
32110 Liu Sarah 1577837262 4 7 4 3 3 8 7 5 7 6 3 3
24351 Williams Karen 1579483736 8 2 6 5 5 1 0 4 10 4 2 9
27648 Brown David 1578674874 6 8 4 0 8 4 9 1 1 5 9 8
86957 Jones Thomas 1570948372 3 9 5 2 10 3 0 5 8 3 2 8
56242 Miller Matthew 1570948332 6 8 8 7 7 10 8 5 3 8 9 6
38759 Davis Mark 1578725413 3 0 1 1 1 9 3 8 4 6 3 9
Last edited on
You need to read in all 12 of the visit values. For example:

inData >> memberID >> lastName >> firstName >> phoneNumber >> JanVisits >> FebVisits >> MarVisits >> AprVisits >> MayVisits >> JunVisits >> JulyVisits >> AugVisits >> SepVisits >> OctVisits >> NovVisits >> DecVisits ;

Then do I need to run the switch for each Visits int?
Getting closer. This has the correct formatting, but the totals column is still wrong (only outputs default from switch). I think that my switch is in the wrong place??

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

int main()
{
/// Declare variables
    string memberID, lastName, firstName;
    int phoneNumber, points;
    int janVisits, febVisits, marVisits, aprVisits, mayVisits, junVisits, julyVisits, augVisits, sepVisits, octVisits, novVisits, decVisits;
    int i = 1, j = 1, sum ;

/// Print welcome
    cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << endl;
    cout << "* Welcome to Norfolk Garden Rewarding Program! *" << endl;
    cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << endl;
    cout << endl;

    cout << left
         << setw(12) << "Member ID"
         << setw(13) << "Last Name"
         << setw(13) << "First Name"
         << setw(15) << "Phone Number"
         << setw(15) << "Total Points"
         << endl;
        cout << left
         << setw(12) << "========="
         << setw(13) << "========="
         << setw(13) << "=========="
         << setw(15) << "============"
         << setw(15) << "============"
         << endl;

/// Declare input data and open file
    ifstream inData;
    inData.open("garden.txt");

/// Start loop
    for (i = 1; i <=10; i++)
      {
        /// Read in input data
            inData >> memberID >> lastName >> firstName >> phoneNumber >> janVisits >> febVisits >> marVisits >> aprVisits
                   >> mayVisits >> junVisits >> julyVisits >> augVisits >> sepVisits >> octVisits >> novVisits >> decVisits;

        cout << left
             << setw(12) << memberID
             << setw(13) << lastName
             << setw(13) << firstName
             << setw(15) << phoneNumber;

            for (j = 1; j <=1; j++)
            {

            ///Calculate points
            cout << setw(15) << sum << endl;
            sum = sum + i;


             switch (janVisits, febVisits, marVisits, aprVisits, mayVisits, junVisits, julyVisits, augVisits, sepVisits, octVisits, novVisits, decVisits)
                {
                    inData >> janVisits>> febVisits >> marVisits >> aprVisits >> mayVisits >> junVisits
                           >> julyVisits >> augVisits >> sepVisits >> octVisits >> novVisits >> decVisits;
                    case '0':
                        points = 0;
                        break;
                    case '1':
                        points = 5;
                        break;
                    case '2':
                        points = 15;
                        break;
                    case '3':
                        points = 30;
                        break;
                    default:
                        points = 60;
                        break;

                }
            }
      }

/// Close input file
    cout << endl;
    inData.close();

    return 0;
}
Last edited on
Here is the output so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
* Welcome to Norfolk Garden Rewarding Program! *
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Member ID   Last Name    First Name   Phone Number   Total Points
=========   =========    ==========   ============   ============
35247       Wang         James        1571767873     0
10001       Smith        Mary         1579837617     60
11002       Johnson      Jennifer     1571672876     60
74647       Yang         Susan        1574762892     60
32110       Liu          Sarah        1577837262     60
24351       Williams     Karen        1579483736     60
27648       Brown        David        1578674874     60
86957       Jones        Thomas       1570948372     60
56242       Miller       Matthew      1570948332     60
38759       Davis        Mark         1578725413     60


Process returned 0 (0x0)   execution time : 0.285 s
Press any key to continue.
This is the txt input file:
1
2
3
4
5
6
7
8
9
10
35247	Wang	 James	   1571767873	1	1	4	6	6	6	10	8	1	3	2	9	
10001	Smith	 Mary	   1579837617	0	8	3	3	4	8	6	7	1	2	5	3
11002	Johnson	 Jennifer  1571672876	7	2	8	4	9	1	2	3	2	3	3	3
74647	Yang	 Susan     1574762892	2	5	4	4	3	0	8	9	5	1	1	8
32110	Liu	 Sarah	   1577837262	4	7	4	3	3	8	7	5	7	6	3	3
24351	Williams Karen	   1579483736	8	2	6	5	5	1	0	4	10	4	2	9
27648	Brown 	 David	   1578674874	6	8	4	0	8	4	9	1	1	5	9	8
86957	Jones	 Thomas	   1570948372	3	9	5	2	10	3	0	5	8	3	2	8
56242	Miller	 Matthew   1570948332	6	8	8	7	7	10	8	5	3	8	9	6
38759	Davis	 Mark	   1578725413	3	0	1	1	1	9	3	8	4	6	3	9
Have you studied structures/classes, std::vector, std::array, or even arrays yet?

Not yet. Only basic loops, get, peek, ignore, assert. Very basic stuff. We've done a similar project with just the "points" portion and the instructor's example was an if/else cluster instead of a switch.
case '1': This checks for the ASCII character '1', not the integer value 1. It should be case 1:

for (i = 1; i <=10; i++)

I would use a loop to read the last 12 columns. After reading each value, convert it to points
and add to the sum:
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

int
main()
{
/// Declare variables
    string memberID, lastName, firstName;
    int phoneNumber, points;
    int visit;
    int i = 1, j = 1, sum;

/// Print welcome
    cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << endl;
    cout << "* Welcome to Norfolk Garden Rewarding Program! *" << endl;
    cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << endl;
    cout << endl;

    cout << left
	<< setw(12) << "Member ID"
	<< setw(13) << "Last Name"
	<< setw(13) << "First Name"
	<< setw(15) << "Phone Number" << setw(15) << "Total Points" << endl;
    cout << left
	<< setw(12) << "========="
	<< setw(13) << "========="
	<< setw(13) << "=========="
	<< setw(15) << "============" << setw(15) << "============" << endl;

    /// Declare input data and open file
    ifstream inData;
    inData.open("garden.txt");

    /// For each input line
    for (i = 0; i < 10; i++) {
	/// Read the first 4 columns
	inData >> memberID >> lastName >> firstName >> phoneNumber;

	// Read the 12 monthly visits and add the appropriate number of
	// points
	sum = 0;
	for (j = 0; j < 12; ++j) {
	    inData >> visit;
	    switch (visit) {
	    case 0:
		points = 0;
		break;
	    case 1:
		points = 5;
		break;
	    case 2:
		points = 15;
		break;
	    case 3:
		points = 30;
		break;
	    default:
		points = 60;
		break;
	    }
	    sum += points;
	}

	// Print out the results
	cout << left
	    << setw(12) << memberID
	    << setw(13) << lastName
	    << setw(13) << firstName
	    << setw(15) << phoneNumber << setw(12) << sum << '\n';
    }

/// Close input file
    cout << endl;
    inData.close();

    return 0;
}

Awesome!! Thank you!
It works, I was just asking the TA if I could read all months at once with a single "visit" int.
Topic archived. No new replies allowed.