I keep getting a error that says invalid conversion from int to int
here is my code

sales report.h

#ifndef SALESREPORT_H
#define SALESREPORT_H

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

#endif // SALESREPORT_H

sales report.cpp

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

SalesReport::SalesReport(int sales[])
{
	Salary(sales);
	DisplaySalary(sales);
}

int SalesReport::Salary(int sales[])
{
    int arraysize = sizeof(sales);
    int i = 0;
    while (i <= arraysize)
    {
          sales[i] = (sales[i]* .09) + 200;
          i++;
    }
    
   return sales;
}
// End Salary

void SalesReport::DisplaySalary(int sales[])
{
     int arraysize = sizeof(sales);
     int i = 0;
     int rangeA, rangeB , rangeC, rangeD, rangeE, rangeF, rangeG, rangeH, rangeI;
     while (i <= arraysize)
     {    
           if (200 <= sales[i] <= 299)  
            rangeA += 1;
            i++;
           
           else if (300 <= sales[i] <= 399)  
            rangeB += 1;
            i++;
            
            else if (400 <= sales[i] <= 499)  
            rangeC += 1;
            i++;
            
            else if (500 <= sales[i] <= 599)  
            rangeD += 1;
            i++;
            
            else if (600 <= sales[i] <= 699)  
            rangeE += 1;
            i++;
            
            else if (700 <= sales[i] <= 799)  
            rangeF += 1;
            i++;
            
            else if (800 <= sales[i] <= 899)  
            rangeG += 1;
            i++;
            
            else if (900 <= sales[i] <= 999)  
            rangeH += 1;
            i++;
            
            else 
            rangeI += 1;
            i++;
            }
            // 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];
    do 
{
    cout << "Enter a sales amount  (negative to end): ";
    cin >> sales[i];
    cout << endl;
    i++;
} while( sales[i] >= 0);

SalesReport mySalesReport(sales);

mySalesReport.Salary(sales);
mySalesReport.DisplaySalary(sales);

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

along with the invalid conversion errors
it keeps saying endl is an undeclared variable
and is says there is a ; expected before each else in the if else statement

can someone find out the problem

Recommended Answers

All 11 Replies

line 15 of report.cpp: That line doesn't do what you want it to do. sizeof(any pointer) is always the same. Arrays are always passed to functions by pointer even if you declare it like an array. Since this is a c++ program you should use vector<int> sales, then that function can easily get the number of elements in the array.

SalesReport::SalesReport(std::vector<int>& sales)
{
	Salary(sales);
	DisplaySalary(sales);
}
int SalesReport::Salary(std::vector<int>& sales)
{
    int arraysize = sales.size();
    int i = 0;
    while (i <= arraysize)
    {
          sales[i] = (sales[i]* .09) + 200;
          i++;
    }
    
   return sales;
}

As for your specific problems, please tell us the line numbers that are causing the errors.

all on salesreport.cpp
invalid conversion int to int is on line 23
expected primary expression and expected ; before else errors starting on line 38 and continue throughout if else statement

line 73 cout undeclared and endl undeclared

add this to the top of your code:

#include <iostream>   //U already have iostream so no need to put that again.. but put the following:

using namespace std;  //Without this, every line beginning with cout would have to be replaced with std::cout.

*sigh* No offense or anything.. You are better than me cuz I dont know arrays PERFECTLY but Im not exactly sure how you learned about arrays and functions without knowing that.. when your if-else statements contain more than one calc/statements/whatever.. it needs cbrackets..

Example:
This is ok..

if(x == 1)  //this is fine because it only contains cout<<x and the else part only contains cout<<"x != 1"..
   cout<<x;
else
   cout<<"x != 1";

This is not OK..

if(x == 1)  //this is not ok because it contains cout<<x & cout<<"try again" and the else part also has two statements..
   cout<<x;       //statement 1
   cout<<"try again";   //statement 2
                                   //no brackets here!?!?! why not?? that = bad..
else
   cout<<"x != 1";    //statement 1, no opening cbrackets
   cout<<"sigh";      //statement 2, also with no closing cbrackets..

The fix for the above "not ok" code.

if(x == 1)  //this is ok because it contains cout<<x & cout<<"try again" in cbrackets and the else part also has {two statements}..
{  
 cout<<x;               //statement 1
 cout<<"try again";     //statement 2
}
else
{
   cout<<"x != 1";   //statement 1
   cout<<"sigh";     //statement 2
}

What are you trying to do with the Salary method of SalesReport? You want to modify the array passed as a parameter? Or you want to compute a total salary from the sales received? If you want to modify the array passed as a parameter, you're doing it with the code you already have...

Maybe you want to do this instead:

int SalesReport::Salary(int sales[])
{
    int arraysize = sizeof(sales);
    int i = 0;

    int total = 0;

    while (i <= arraysize)
    {
        total += (sales[i]* .09) + 200;
        i++;
    }

    return total;
}

...and the type conversion error will banish. For you information, sales is an "int array" (int[]) and you try to return an "int", so there is an incompatibility. Maybe you want to return the array (a pointer on it - int*), but I doubt that this is what you want.

For the "illegal elses", you need to put i++ at the same indent level of each line that follows "if" and "else if". But I suggest that you use a for loop and place the i increment inside, so you won't need to put i++ for each condition:

for (int i = 0; i <= arraysize; i++)
{
  //Do your conditions.
}

And for cout and endl, they belong to the std namespace, so you need to write std::cout and std::endl OR put at the beginning of your code:

using namespace std;

Hope this helps with your understanding of C++!

the salary function is supposed to change each of the elements in the array to a calculated salary
so i want each item in the array to to equal sales(which is the value in each array item) * .09 + 200

and also are you saying i should just turn the whole if else statement into a for statement

What are you trying to do with the Salary method of SalesReport? You want to modify the array passed as a parameter? Or you want to compute a total salary from the sales received? If you want to modify the array passed as a parameter, you're doing it with the code you already have...

Maybe you want to do this instead:

The code you posted is incorrect. See my previous post about why sizeof does not return the number of elements in the array.

i fixed the size of problem by changing it to
int arraysize = sizeof(sales)/sizeof(int) ;
based on info i found online
also fixed endl and cout problems
other problems still persistent
thanks for all your responses by the way

errors in if statement solved all that is left is invalid conversion

i fixed the size of problem by changing it to
int arraysize = sizeof(sales)/sizeof(int) ;
based on info i found online
also fixed endl and cout problems
other problems still persistent
thanks for all your responses by the way

Wrong -- wrong -- wrong. Didn't you read my previous post about sizeof and pointers???? sizeof(sales) will always be 4 because variable named sales is a pointer, not an array. If you want your program to be wrong then go ahead and leave it the way you have it. I'm not going to mention this to you again. Just do it however you want to because its your funeral, not mine.

alright so if i use vector<int> sales will this not only take care of my array size problem but my conversion error too

Glancing through, it didn't seem like anyone has pointed out to you yet that

if (200 <= sales[i] <= 299)

should be

if(sales[i] >=200 && sales[i] <=299)

The first version may compile as a statement, but it isn't going to resolve properly. The same for your other else ifs.

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.