HELP PLEASE, CODE DUE IN 1 HOUR!

I have my entire code written for this assignment (which reads values from an input file) as well as the data set, but when I run it using the data set nothing happens and I have to use "control X + C" to stop running it. Can someone help me find what the problem is, and why no values are being displayed for this? I've spent hours on this code and still can't find the problem ... and it's due in an hour!

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
using namespace std;

const int SMALL = 30; // Cost to plant a small tree
const int MED = 75; // Cost to plant a medium tree
const int LARGE = 120; // Cost to plant a large tree
const int FLATFEE = 100; // Cost of flat fee to remove tree/stump
const int FEELESS = 10; // Cost of fee (per inch) for stumps with a diameter less than 6 inches
const int FEEMORE = 15; // Cost of fee (per inch) for stumps with a diameter of 6 inches or more
const int YARDMAINT = 25; // Cost (per hour) for general yard maintenance

int main()
{
  char charreader; // Used to input the first character of each line in the code
  char plantsize; // Used to input the plant sizes
  char codeL; // First letter of job code
  char codeN1; // First number of job code
  char codeN2; // Second number of job code
  char codeN3; // Third number of job code

  int smallcounter; // Counts number of small trees planted
  int medcounter; // Counts number of medium trees planted
  int largecounter; // Counts number of large trees planted
  int removecounter; // Counts number of trees being removed
  int stumpdiameter; // Diameter of stump being removed
  int lessthan6counter; // Counts number of trees being removed with a diameter of less than 6 inches
  int morethan6counter; // Counts number of trees being removed with a diameter of 6 or more inches

  double hours; // Number of hours spent doing general maintenance work
  double hoursB; // Only used if there is more than one line per job indiciating number of hours spent doing gnereal maintenance work
  double plantcost; // Total cost for trees to be planted for one whole job
  double removecost; // Total cost for trees and their stumps to be removed for one whole job
  double maintcost; // Total cost for general maintenance work for one whole job
  double jobtotal; // Total cost for one whole job
  double grandtotal; // Total cost for all jobs in the input file

  cout << "Dayton Flores - Assignment 7 - Section 1004" << endl;
  cout << " " << endl;
  cout << setw(28) << "TREE" << setw(16) << "MAINTAIN" << endl;
  cout << "JOB" << setw(14) << "PLANTING" << setw(13) << "REMOVAL" << setw(12) << "YARD" << setw(18) << "TOTAL" << endl;

  while(cin)
    {
      cin >> codeL >> codeN1 >> codeN2 >> codeN3;
      cin >> charreader;
      while(charreader != '*')
        {
          if(charreader = 'P')
            {
              cin >> charreader;
              while(charreader != '#')
                {
                  if(charreader = 'S')
                    {
                      smallcounter++;
		    }
                  if(charreader = 'M')
                    {
                      medcounter++;
                    }
                  if(charreader = 'L')
                    {
                      largecounter++;
                    }
                }
            }
          if(charreader = 'R')
            {
              cin >> removecounter;
              for(int i = 0; i < removecounter; i++)
                {
                  cin >> stumpdiameter;
		  if(stumpdiameter < 6)
                    {
                      lessthan6counter++;
                    }
		  if(stumpdiameter >= 6)
		    {
                      morethan6counter++;          
                    }
                }
            }
          if(charreader = 'M')
            {
              if(hours == 0)
                {
                  cin >> hours;
                }
              if(hours > 0)
                {
                  cin >> hoursB;
                  hours += hoursB;
                }
            }
          if(charreader = '*')
            {
              plantcost = (smallcounter * SMALL) + (medcounter * MED) + (largecounter * LARGE);
              removecost = ((lessthan6counter + morethan6counter) * 100);
              maintcost = (YARDMAINT * hours);
	      jobtotal = plantcost + removecost + maintcost;
              cout << fixed<< showpoint <<setprecision(2);
              cout << left << codeL << codeN1 << codeN2 << codeN3 << setw(4) << "   $ " << right;
              cout << setw(8) << plantcost << "   $" << setw(9) << removecost << "    $ " << setw(8) << maintcost << "      $  " << setw(7) << jobtotal << endl;
              smallcounter = 0;
              medcounter = 0;
              largecounter = 0;
              lessthan6counter = 0;
              morethan6counter = 0;
              hours = 0;
            }
        }
      grandtotal += jobtotal;
    }
  cout << "TOTAL" << setw(79) << grandtotal;
  return 0;
}


EDIT: It seems as though the very first "while(cin)" loop isn't even starting... I am running the program with the data set by using "./a.out < hw07data" after compiling it. Any suggestions?
Last edited on
Did you find that out by debugging? If not, you should try debugging to see if you can get any details about the program's state that help.
Last edited on
How do I debug it? I'm running this in Terminal on Mac OS X 10.8.5.
One problem I see is that you mess up the comparisons. For example:

1
2
3
if(charreader = 'P') // this is an assignment, single equal sign

if(charreader == 'P') // this is a comparison, double equal sign 

Thank you for the help! I changed them all and it's still giving me the same results! It's as if I'm running the program without using an input file the way I have to quit it from running even though nothing is being output!

I also rewrote the code except using "switch" statements, if you guys could possibly analyze... same results with this one!

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
using namespace std;

const int SMALL = 30; // Cost to plant a small tree
const int MED = 75; // Cost to plant a medium tree
const int LARGE = 120; // Cost to plant a large tree
const int FLATFEE = 100; // Cost of flat fee to remove tree/stump
const int FEELESS = 10; // Cost of fee (per inch) for stumps with a diameter less than 6 inches
const int FEEMORE = 15; // Cost of fee (per inch) for stumps with a diameter of 6 inches or more
const int YARDMAINT = 25; // Cost (per hour) for general yard maintenance

int main()
{
  char charreader; // Used to input the first character of each line in the code
  char plantsize; // Used to input the plant sizes
  char codeL; // First letter of job code
  char codeN1; // First number of job code
  char codeN2; // Second number of job code
  char codeN3; // Third number of job code

  int smallcounter; // Counts number of small trees planted
  int medcounter; // Counts number of medium trees planted
  int largecounter; // Counts number of large trees planted
  int removecounter; // Counts number of trees being removed
  int stumpdiameter; // Diameter of stump being removed
  int lessthan6counter; // Counts number of trees being removed with a diameter of less than 6 inches
  int morethan6counter; // Counts number of trees being removed with a diameter of 6 or more inches

  double hours; // Number of hours spent doing general maintenance work
  double hoursB; // Only used if there is more than one line per job indiciating number of hours spent doing gnereal maintenance work
  double plantcost; // Total cost for trees to be planted for one whole job
  double removecost; // Total cost for trees and their stumps to be removed for one whole job
  double maintcost; // Total cost for general maintenance work for one whole job
  double jobtotal; // Total cost for one whole job
  double grandtotal; // Total cost for all jobs in the input file

  cout << "Dayton Flores - Assignment 7 - Section 1004" << endl;
  cout << " " << endl;
  cout << setw(28) << "TREE" << setw(16) << "MAINTAIN" << endl;
  cout << "JOB" << setw(14) << "PLANTING" << setw(13) << "REMOVAL" << setw(12) << "YARD" << setw(18) << "TOTAL" << endl;

  while(cin)
    {
      cin >> codeL >> codeN1 >> codeN2 >> codeN3;
      cin >> charreader;
      while(charreader != '*')
        {
          switch(charreader)
            {
	      case 'P':
                cin >> plantsize;
	        while(plantsize != '#')
	     	 {
		   if(plantsize == 'S')
		     {
		       smallcounter++;
		     }
		   if(plantsize == 'M')
		     {
		       medcounter++;
		     }
		   if(plantsize == 'L')
		     {
		       largecounter++;
		     }
		 }
	       break;

	      case 'R': 
	        cin >> removecounter;
	        for(int i = 0; i < removecounter; i++)
		 {
		   cin >> stumpdiameter;
		   if(stumpdiameter < 6)
		     {
		       lessthan6counter++;
		     }
		   if(stumpdiameter >= 6)
		     {
		       morethan6counter++;          
		     }
		 }
	       break;

	      case 'M':
	        if(hours == 0)
		 {
		   cin >> hours;
		 }
	        if(hours > 0)
		 {
		   cin >> hoursB;
		   hours += hoursB;
		 }
	       break;

	      case '*':
		plantcost = (smallcounter * SMALL) + (medcounter * MED) + (largecounter * LARGE);
		removecost = ((lessthan6counter + morethan6counter) * 100);
		maintcost = (YARDMAINT * hours);
		jobtotal = plantcost + removecost + maintcost;
		cout << fixed<< showpoint <<setprecision(2);
		cout << left << codeL << codeN1 << codeN2 << codeN3 << setw(4) << "   $ " << right;
		cout << setw(8) << plantcost << "   $" << setw(9) << removecost << "    $ " << setw(8) << maintcost << "      $  " << setw(7) << jobtotal << endl;
		smallcounter = 0;
		medcounter = 0;
		largecounter = 0;
		lessthan6counter = 0;
		morethan6counter = 0;
		hours = 0;
		break;
            }
	}
      grandtotal += jobtotal;
    }
  cout << "TOTAL" << setw(79) << grandtotal;
  return 0;
}
Topic archived. No new replies allowed.