this program is supposed to take in sales figures in dollars, convert them into a salary for the salesmen, and then display the number of salaries that fall within certain ranges (200-299 ect.)
However i keep getting incorrect results
no matter what i enter i keep getting 11 for the range 200-299 and 40 for the range over 1000
i would appreciate help finding the problem

here is the code for the 3 files

sales.h

/ Class automatically generated by Dev-C++ New Class wizard

#ifndef SALESREPORT_H
#define SALESREPORT_H

// No description
class SalesReport
{
	public:
		// class constructor
		SalesReport( int [] );
		int Salary( int );
		void DisplaySalary();
		
	private:
        int sales[50];   // sales
};

#endif // SALESREPORT_H

sales.cpp

// Class automatically generated by Dev-C++ New Class wizard
#include <cstdlib>
#include "salesreport.h" // class's header file
#include <iostream>
#include <iomanip>
using namespace std;

SalesReport::SalesReport(int sArray[]) 
{
    
	DisplaySalary();
}

int  SalesReport::Salary( int oneSales )
{
          return  static_cast<int> ((oneSales* .09) + 200);
    
   //return sales;
}
// End Salary

void SalesReport::DisplaySalary()
{
     int arraysize = sizeof(sales)/sizeof(int);
    
     int rangeA=0, rangeB=0 , rangeC=0, rangeD=0, rangeE=0, rangeF=0, rangeG=0, rangeH=0, rangeI=0;
     for (int i =0; i <= arraysize; i++)
     {    
          int salary = Salary( sales[i] );
           if (200 <= salary && salary <= 299)  
            rangeA += 1;
           
           
           else if (300 <= salary && salary <= 399)  
            rangeB += 1;
     
            
            else if (400 <= salary && salary <= 499)  
             rangeC += 1;
        
            
            else if (500 <= salary && salary <= 599)  
             rangeD += 1;
   
            
            else if (600 <= salary && salary <= 699)  
            rangeE += 1;
      
            
            else if (700 <= salary && salary <= 799)  
            rangeF += 1;
     
            
            else if (800 <= salary && salary <= 899)  
            rangeG += 1;
      
            
            else if (900 <= salary && salary <= 999)  
            rangeH += 1;
  
            
            else 
            rangeI += 1;
   
            }
            // End while statement
            
        
        cout << "    Range         Number" << endl;
        cout << " $200-299         " << rangeA << endl;
        cout << " $300-399         " << rangeB << endl;  
        cout << " $400-499         " << rangeC << endl;
        cout << " $500-599         " << rangeD << endl;
        cout << " $600-699         " << rangeE << endl; 
        cout << " $700-799         " << rangeF << endl; 
        cout << " $800-899         " << rangeG << endl;
        cout << " $900-999         " << rangeH << endl;
        cout << " $1000 and over   " << rangeI << endl; 
        
     
        }
        // End Display Salary

main.cpp

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include "salesreport.h"
using namespace std;

int main()
{
   
    int i = 0;
     int sales[i];
    cout << "Enter a sales amount  (negative to end): ";
    cin >> sales[i];
    while( sales[i] >= 0 )
    {
          cout << endl;
          i++;
          cout << "Enter a sales amount  (negative to end): ";
          cin >> sales[i];
     } 

SalesReport mySalesReport( sales );


system("PAUSE");
return(0);
}

Recommended Answers

All 7 Replies

You need to rethink these comparisons. Read them out loud and see if they sound correct:

if (200 <= salary && salary <= 299)  
           else if (300 <= salary && salary <= 399)  
            else if (400 <= salary && salary <= 499)  
            else if (500 <= salary && salary <= 599)  
            else if (600 <= salary && salary <= 699)  
            else if (700 <= salary && salary <= 799)  
            else if (800 <= salary && salary <= 899)  
            else if (900 <= salary && salary <= 999)  
}

im still not seeing the problem
could someone just tell me what it is

Sorry, my fault. I'm not used to seeing the IF in that style. I've always used if ((val >= 300) && (val < 400)) to check for 300-399.

However i keep getting incorrect results
no matter what i enter i keep getting 11 for the range 200-299 and 40 for the range over 1000

With the information you provided, 11 is probably the correct value for RangeA and 40 is correct for RangeI. You'd have to show us why these values are wrong, not just tell us they are.

And what about the range from 0 to 199? Those values will be added to RangeI the way your IF is set up.

well the program is supposed to take in a set of numbers convert them with the function salary and then count the number of entries that fall in that range
the numbers counted in the ranges should change based on what is entered so what is making rangeA always 11 and rangeI 40
ive been trying to find out what is casing it
i got a feeling its gonna seem so obvious when someone points it out to me

Interesing syntax, I thought array initialization has to be a constant???

Is'nt that going to go out of range and cause all kinds of interesing problems?

since i is incremented further down...

int i = 0;
int sales;

well the program is supposed to take in a set of numbers convert them with the function salary and then count the number of entries that fall in that range
the numbers counted in the ranges should change based on what is entered so what is making rangeA always 11 and rangeI 40
ive been trying to find out what is casing it
i got a feeling its gonna seem so obvious when someone points it out to me

OK, here's why we can't answer you:
When I add the number of letters in my full name I get 12. But that's wrong. What am I doing wrong?

Have I given you enough information to fix my problem? What else do you need to know to tell me what's wrong?

In other words -- think about what you you need answered and give all the information you can to show us why your answers are wrong. The key is think!

It may be an entirly different issue but this part of the code has a bug.

The array of zero size is created

Then values are read into the array indexing with i

This index is incremented.

C arrays do not grow dynamically.

Out of bounds access is undefined and cause weird behavior.

#include <iostream>
#include <string>

int main()
{
    int i = 0;
    int sales[i];

    std::cout <<  "sizeof array = " << sizeof(sales) << std::endl;

    std::cout << "Enter a sales amount  (negative to end): ";
    std::cin >> sales[i];
    while( sales[i] >= 0 )
    {
          i++;
          std::cout << "Enter a sales amount  (negative to end): ";
          std::cin >> sales[i];
     }

    return(0);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.