简体   繁体   中英

How can I fix this [Error] cannot convert 'float [6]' to float in assignment

This is my code below, I'm having an error. ([Error] cannot convert 'float [6]' to float in assignment.) I don't know what to do or what to put in profit... My goal is to put the highest expenses, highest profit, and lowest profit below. but I'm having this kind of error. 在此处输入图像描述

#include <iostream>
#include <string>
using namespace std;
int main(){
int month[] = {1,2,3,4,5,6};
float profit;
string monthName;
float sales[6] = {1000, 1500, 2500, 5000, 4000, 1800};
float expenses[6] = {500, 250, 100, 1000, 200, 800};
float totalex=0,totalsale=0,totalprofit;


float highSales=0, highExp=0, highProf=0, lowProf=0;
string highMonth="";

cout << "Company Sales and Expenses For 2020" << endl;  

for (int i=0; i <=6; i++ ) {
    profit = sales[i]-expenses[i];
    totalprofit+=profit;
    totalsale+=sales[i];
    totalex+=expenses[i];
    switch(month[i]) {
        case 1:  monthName="January"; break;
        case 2:  monthName="February"; break;
        case 3:  monthName="March"; break;
        case 4:  monthName="April"; break;
        case 5:  monthName="May"; break;
        case 6:  monthName="June"; break;
        
    }
    if(sales[i]>highSales) {
          highSales = sales[i];
          highMonth = monthName;
          highExp = expenses;
          highProf = profit;
          lowProf = profit;
    }
    
    cout <<"\n\n" << monthName<<"\t\t"<<sales[i]<<"\t\t"<<expenses[i]<<"\t\t\t"<<profit;
   }


cout << "\nSummary : " << endl;
   cout << "\tTotal Sales : " << totalsale << endl;
   cout << "\tTotal Expenses : " << totalex << endl;
   cout << "\tTotal Profit : " << totalprofit << endl;

   cout << "\tHighest Sales : " << highSales << "\tMonth of " << highMonth << endl;
   cout << "\tHighest Expenses : " << highExp << "\tMonth of " << highMonth << endl;
   cout << "\tHighest Profit : " << highProf << "\tMonth of " << highMonth << endl;
   cout << "\tLowest Profit : " << lowProf   << "\tMonth of "  << highMonth << endl;

   cout << "\nProgrammed by: ";
          
    return 0;
}

This is my code above. The problem is that I cant print the highest expenses, highest profit, and lowest profit. I cant compile and run as this error shows [Error] cannot convert 'float [6]' to float in assignment.

highExp = expenses; in this line highExp is a float, and expenses is a float[6] , hence you are trying to assign a float array to a float which is invalid.

Additionally for (int i=0; i <=6; i++ ) { will cause errors as sales[6] is not within sales, as the start of sales is sales[0] and ends at sales[5] , same with expenses .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM