Logical for loop errors

Hi, I have a program that compiles but I am getting a scramble of numbers as an output.

I am supposed to get this exact output:

Store Statistics
Dept Average Above Below Performance
-----------------------------------------------------------------------
1 37.6 7 5 unsatisfied
2 38.6 8 4 satisfied
3 44.9 7 5 unsatisfied
4 51.1 8 4 satisfied
5 43.0 7 5 unsatisfied
6 57.6 11 1 satisfied
7 48.2 9 3 satisfied



Instead, I keep getting this weird scramble of letters (note, this is not the entire output, I copied only part of it to save space). My gut feeling is that there are some logical for loop errors in the mix. What do I need to modify to get this to output properly?

Store Statistics
Dept Average Above Below Performance
=======================================================================
1 6.95262e-310 -1987484468 1987484480
2 6.95262e-310 -1987484468 1987484480
3 6.95262e-310 -1987484468 1987484480
4 6.95262e-310 -1987484468 1987484480
5 6.95262e-310 -1987484468 1987484480
6 6.95262e-310 -1987484468 1987484480
7 6.95262e-310 -1987484468 1987484480
Store Statistics
Dept Average Above Below Performance
=======================================================================
2 6.95262e-310 -1987484468 1987484480
3 6.95262e-310 -1987484468 1987484480
4 6.95262e-310 -1987484468 1987484480
5 6.95262e-310 -1987484468 1987484480
6 6.95262e-310 -1987484468 1987484480
7 6.95262e-310 -1987484468 1987484480
8 6.95262e-310 -1987484468 1987484480
Store Statistics
Dept Average Above Below Performance
=======================================================================
3 6.95262e-310 -1987484468 1987484480
4 6.95262e-310 -1987484468 1987484480
5 6.95262e-310 -1987484468 1987484480
6 6.95262e-310 -1987484468 1987484480
7 6.95262e-310 -1987484468 1987484480
8 6.95262e-310 -1987484468 1987484480
9 6.95262e-310 -1987484468 1987484480
Store Statistics
Dept Average Above Below Performance
=======================================================================
4 6.95262e-310 -1987484468 1987484480
5 6.95262e-310 -1987484468 1987484480
6 6.95262e-310 -1987484468 1987484480
7 6.95262e-310 -1987484468 1987484480
8 6.95262e-310 -1987484468 1987484480
9 6.95262e-310 -1987484468 1987484480
10 6.95262e-310 -1987484468 1987484480


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
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

//=============================================================================
// Function prototypes
double average_Sales(double [], int);
void Standard_comparison(double [], double [], bool [], int);
int above_below(bool [], int);
void print_Result(int, double, int, int);

//=============================================================================
// Main function
int main()
{
	// Is this the parallel array????
	int average;
	double standard[12]={23.0, 33.1, 21.0, 23.5, 54.0, 34.3,
						35.0, 45.0, 56.3, 45.6, 34.0, 55.0};
			  
	double sales[12]; 
	bool meet[12];

	ifstream fp;
	int id=0;
	
	fp.open("sales.dat");
	
	while(fp>>sales[0])
    {
      // Assign value from 1-7 to each line of data
      id++;

      // Enter the values into the array
	  for (int i=1; i<11; i++)
		{
			fp>>sales[i];
		}


      // Get the average
      double average;
      average = average_Sales(sales, 12);


      // Compare sales and standard, populate the meet array
      Standard_comparison(sales, standard, meet, 12);

	  
	  //compute how many months meet the standard
      int above;
      above = above_below(meet, 12);

      //compute how many months are below standard
      int below =  12-above;  

      //display the department stat in a single line 
      //the performance can be displayed on the fly
      print_Result(id, average, above, below);
    }
}

//============================================================================
// Calculates the annual average for a particular department
double average_Sales(double sales[], int size)
{
	double sum;
	for (int i=0; i<12; i++)
    {
      sum+=sales[i];
    }
	double average;
	average = sum/12;
}

//=============================================================================
// Compare sales figures with standard array and store stats in new (bool) array
void Standard_comparison(double sales[], double standard[], bool meet[], int size)
{
	for (int i=0; i<12; i++)
	{	
	if (sales[i]>standard[i])
		meet[i]=true;
	else
		meet[i]=false;	
	}
}

//=============================================================================
// Determine whether the monthly sales were above or below average
int above_below(bool meet[], int size)
{
	int counter =0;
	for (int i=0; i<12; i++)
    {
      if (meet[i]= true)
		counter ++;
	if (meet [i]= false)
		counter++;
		
    }
}

//=============================================================================
void print_Result(int id, double average, int above, int below)
{
//cout << setprecision(2) << fixed;
cout << "Store Statistics\n";
cout << "Dept  " << "Average  " << "Above     " << "Below     " << "Performance" << endl;
cout << "=======================================================================\n";
for (int i=0; i<7; i++)
	{
		cout << id++ << "  " << average << "  ";
		cout << above << "  " << below << endl;
	}
}
Last edited on
Compile with warnings.
Read the warnings.


> fp.open("sales.dat");
¿how do you expect for us to run your program without your input data?
This is all of the data from the files:

23 33.5 21 23 25 56 54 43 34.2 35.4 34 69.5
24 35.2 24 26 43 56.7 54 32 43 34 34 57.9
24 42 43 35 52 56 67 54 56 45.3 32 32
20 32 45 72 45.4 63.2 45 56 52 65 53 65
34 35 37.5 32 23 45 31 43 52 43 76 65
35 56 63.4 45.2 45.6 56 67.3 45 56.3 67 78 76
34.2 45 62 19 45 39 38 37 82 74 45 58.4
warning: no return statement in function returning non-void [-Wreturn-type]
double average_Sales (double sales[], int size)

In function ‘int above_below(bool*, int)’:
97,99 warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   if (meet[i] = true) counter++;
   if (meet[i] = false) counter++;
                      ^
warning: no return statement in function returning non-void [-Wreturn-type]
int above_below (bool meet[], int size)
Strange, my compiler never spit those errors out. I am using g++34 in Putty. I added a few returns and here is my code so far. I was unsure of how to properly add a return to void Standard_comparison so I left that out. Here is the output I'm getting now (again I'm only posting a portion of the output just so this post doesn't become super long):


Store Statistics
Dept Average Above Below Performance
=======================================================================
1 31.8417 12 0
2 31.8417 12 0
3 31.8417 12 0
4 31.8417 12 0
5 31.8417 12 0
6 31.8417 12 0
7 31.8417 12 0
Store Statistics
Dept Average Above Below Performance
=======================================================================
2 36.7833 12 0
3 36.7833 12 0
4 36.7833 12 0
5 36.7833 12 0
6 36.7833 12 0
7 36.7833 12 0
8 36.7833 12 0


#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

//=============================================================================
// Function prototypes
double average_Sales(double [], int);
void Standard_comparison(double [], double [], bool [], int);
int above_below(bool [], int);
void print_Result(int, double, int, int);

//=============================================================================
// Main function
int main()
{
// Is this the parallel array????
int average;
double standard[12]={23.0, 33.1, 21.0, 23.5, 54.0, 34.3,
35.0, 45.0, 56.3, 45.6, 34.0, 55.0};

double sales[12];
bool meet[12];

ifstream fp;
int id=0;

fp.open("sales.dat");

while(fp>>sales[0])
{
// Assign value from 1-7 to each line of data
id++;

// Enter the values into the array
for (int i=1; i<11; i++)
{
fp>>sales[i];
}


// Get the average
double average;
average = average_Sales(sales, 12);


// Compare sales and standard, populate the meet array
Standard_comparison(sales, standard, meet, 12);


//compute how many months meet the standard
int above;
above = above_below(meet, 12);

//compute how many months are below standard
int below = 12-above;

//display the department stat in a single line
//the performance can be displayed on the fly
print_Result(id, average, above, below);
}
return 0;
}

//===============================================================================
// Calculates the annual average for a particular department
double average_Sales(double sales[], int size)
{
double sum;
for (int i=0; i<12; i++)
{
sum+=sales[i];
}
double average;
average = sum/12;
return average; // NEW ADDITION
}

//================================================================================
// Compare sales figures with standard array and store stats in new (bool) array
void Standard_comparison(double sales[], double standard[], bool meet[], int size)
{
for (int i=0; i<12; i++)
{
if (sales[i]>standard[i])
meet[i]=true;
else
meet[i]=false;
}
}

//==================================================================================
// Determine whether the monthly sales were above or below average
int above_below(bool meet[], int size)
{
int counter =0;
for (int i=0; i<12; i++)
{
if (meet[i]= true)
counter ++;
if (meet [i]= false)
counter++;
}
return counter; // NEW ADDITION
}

//===================================================================================
void print_Result(int id, double average, int above, int below)
{
//cout << setprecision(2) << fixed;
cout << "Store Statistics\n";
cout << "Dept " << "Average " << "Above " << "Below " << "Performance" << endl;
cout << "=======================================================================\n";
for (int i=0; i<7; i++)
{
cout << id++ << " " << average << " ";
cout << above << " " << below << endl;
}
}
1
2
3
4
5
6
7
8
9
10
11
// Determine whether the monthly sales were above or below average
int above_below (bool meet[], int size)
{
	int counter = 0;
	for (int i = 0; i < 12; i++)
	{
		if (meet[i] = true) counter++;
		if (meet[i] = false) counter++;
	}
	return counter; // NEW ADDITION
}
= is assignment, == is comparison
there is no point comparing against true or false, you may just if( meet[i] )
you may use else to get the alternative
both paths do the same operation, counter++
So that function always return 12.

1
2
3
4
5
6
7
8
// Calculates the annual average for a particular department
double average_Sales (double sales[], int size)
{
	double sum;
	for (int i = 0; i < 12; i++)
	{
		sum += sales[i];
	}
Initialize your variables, double sum=0;

1
2
3
4
5
6
7
8
9
void print_Result (int id, double average, int above, int below)
{
	//...
	for (int i = 0; i < 7; i++)
	{
		cout << id++ << " " << average << " ";
		cout << above << " " << below << endl;
	}
}
`average', `above' and `below' do not change on that loop.
You calculate them for just one line of input, then print just one line of output.


> Strange, my compiler never spit those errors out.
you need to enable the warnings
-W{all,extra,pedantic}

> I am using g++34
http://gcc.gnu.org/releases.html (2004)
consider updating your compiler.
Last edited on
I made double sum=0. I wasn't sure exactly how I should make the changes you listed for the above_below function (I got compilation errors when I modified it like you said). Am I supposed to change anything in the void print_Result function or is that good? Here are the errors I got after the changes I made:

In function `int above_below(bool*, int)':
Line 113: error: expected `;' before "counter"


#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

//=============================================================================
// Function prototypes
double average_Sales(double [], int);
void Standard_comparison(double [], double [], bool [], int);
int above_below(bool [], int);
void print_Result(int, double, int, int);

//=============================================================================
// Main function
int main()
{
// Is this the parallel array????
int average;
double standard[12]={23.0, 33.1, 21.0, 23.5, 54.0, 34.3,
35.0, 45.0, 56.3, 45.6, 34.0, 55.0};

double sales[12];
bool meet[12];

ifstream fp;
int id=0;

fp.open("sales.dat");

while(fp>>sales[0])
{
// Assign value from 1-7 to each line of data
id++;

// Enter the values into the array
for (int i=1; i<11; i++)
{
fp>>sales[i];
}


// Get the average
double average;
average = average_Sales(sales, 12);


// Compare sales and standard, populate the meet array
Standard_comparison(sales, standard, meet, 12);


//compute how many months meet the standard
int above;
above = above_below(meet, 12);

//compute how many months are below standard
int below = 12-above;

//display the department stat in a single line
//the performance can be displayed on the fly
print_Result(id, average, above, below);
}
return 0;
}

//===============================================================================
// Calculates the annual average for a particular department
double average_Sales(double sales[], int size)
{
double sum=0;
for (int i=0; i<12; i++)
{
sum+=sales[i];
}
double average;
average = sum/12;
return average; // NEW ADDITION
}

//================================================================================
// Compare sales figures with standard array and store stats in new (bool) array
void Standard_comparison(double sales[], double standard[], bool meet[], int size)
{
for (int i=0; i<12; i++)
{
if (sales[i]>standard[i])
meet[i]=true;
else
meet[i]=false;
}
}

//==================================================================================
// Determine whether the monthly sales were above or below average
int above_below(bool meet[], int size)
{
int counter =0;
for (int i=0; i<12; i++)
{
if (meet[i])
counter ++;
else (meet [i])
counter++;
}
return counter; // NEW ADDITION
}

//===================================================================================
void print_Result(int id, double average, int above, int below)
{
//cout << setprecision(2) << fixed;
cout << "Store Statistics\n";
cout << "Dept " << "Average " << "Above " << "Below " << "Performance" << endl;
cout << "=======================================================================\n";
for (int i=0; i<7; i++)
{
cout << id++ << " " << average << " ";
cout << above << " " << below << endl;
}
}
Also, the output loop executes several times (each time with different numbers). How do I fix it so that the program just processes whats in the data file and then stops?
¿why you aren't indenting your posts?

1
2
3
4
if (meet[i])
   counter ++;
else /*(meet [i])*/
   counter++;
you can't put a condition in an else.
Now read that code. It doesn't matter what `meet[i]' has, you'll increase counter.
¿Does that make sense to you?

1
2
3
4
5
for (int i=0; i<12; i++)
{
   if (meet[i])
      counter ++;
}



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
void print_Result(int id, double average, int above, int below)
{
   cout << id++ << " " << average << " ";
   cout << above << " " << below << endl;
}

//in main
//output the "header" just once
cout << "Store Statistics\n";
cout << "Dept " << "Average " << "Above " << "Below " << "Performance" << endl;
//process by line
while( /*read one line from the file*/ ){
   id++;
   // Get the average
   double average = average_Sales(sales, 12);
   
   // Compare sales and standard, populate the meet array
   Standard_comparison(sales, standard, meet, 12);
   
   //compute how many months meet the standard
   int above = above_below(meet, 12);
   
   //compute how many months are below standard
   int below =  12-above;  
   
   //display the department stat in a single line 
   //the performance can be displayed on the fly
   //print the result of one line
   print_Result(id, average, above, below);
}

Topic archived. No new replies allowed.