Thread: Program displaying wrong results

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Program displaying wrong results

    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

    salesreport.h
    Code:
    // 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
    salesreport.cpp

    Code:
     // 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[]) 
    {
        //Salary();
    	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 (salary >= 200 && salary <= 299)  
                rangeA += 1;
               
               
               else if (salary >= 300 && salary <= 399)  
                rangeB += 1;
         
                
                else if (salary >= 400 && salary <= 499)  
                 rangeC += 1;
            
                
                else if (salary >= 500 && salary <= 599)  
                 rangeD += 1;
       
                
                else if (salary >= 600 && salary <= 699)  
                rangeE += 1;
          
                
                else if (salary >= 700 && salary <= 799)  
                rangeF += 1;
         
                
                else if (salary >= 800 && salary <= 899)  
                rangeG += 1;
          
                
                else if (salary >= 900 && 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

    Code:
    #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);
    }

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    I haven't looked much into the code, but one thing that I noticed:
    Code:
    for (int i =0; i <= arraysize; i++)
    should be:
    Code:
    for (int i =0; i < arraysize; i++)

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There are huge errors you need to fix before proceeding.
    Change

    int sales[i];

    to

    std::vector<int> sales;

    Then this

    cin >> sales[i];

    should be changed into

    int sale;
    cin >> sale;
    sales.push_back(sale);

    This

    int arraysize = sizeof(sales)/sizeof(int);

    should be changed to

    std::size_t arraysize = sales.size();

    and

    sales[i]

    to

    sales.at(i)

    Of course, the class needs to modified to take and work with a vector, too. Begin with those changes to remove the broken code first.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Code:
    #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);
    }

    Edit your main first...
    First thing, how may you even write like this int i=0; int sales[i];??
    you should write like const int i=(some value);

    And as you are using static array so initialize the variable i with a value that should be the size of your array....
    You are okay with getting values in array like cin>>sales[i] but i'll recommend you to change the loop variable from i to some what else as i is being used as constant for declaring the array size.


    I hope it will then work fine....


    Regards...

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A static array should not be used at all in this case.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Whatever... If he/she wants to, one should not be discouraged but let him/her explore the difference between static and dynamic concepts :-)

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indeed? But this is not such an exercise.
    This is an exercise meant to be handled with a dynamic array (ie vector).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    If i was there to handle this exercise, definitely i would go for dynamic concepts rather than these static ones.... Hope you understand..

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    i guess ill try converting to a dynamic array
    i was hesitant to do so because we havent learned how to do them in my c++ class yet

    thanks for responding
    ill post an update on how these changes work out
    Last edited by bkoper16; 03-03-2011 at 10:17 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with this program, and how to fix it?
    By pantera in forum C++ Programming
    Replies: 5
    Last Post: 12-19-2009, 11:06 PM
  2. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  3. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  4. Whats wrong with this program?!?!?
    By johnh444 in forum C++ Programming
    Replies: 5
    Last Post: 06-30-2004, 11:20 AM
  5. Program Has Stuff Wrong Wit It
    By oobootsy1 in forum C++ Programming
    Replies: 5
    Last Post: 08-12-2003, 08:38 PM