-1

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. enter image description here

#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.

Cedric22
  • 3
  • 5
  • 1
    Hint: compiler tells you *at which line* the problem occurred. For example, [my compiler](https://wandbox.org/permlink/2DGDoFRxOZWkmYgO) told me that the error is on line 35. I looked there and found the typo: `highExp = expenses;` should be `highExp = expenses[i];` – Yksisarvinen Dec 14 '20 at 10:01

1 Answers1

2

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.

0RR
  • 1,538
  • 10
  • 21
  • as of now im having trouble when comes to aligning – Cedric22 Dec 14 '20 at 10:20
  • Take it one step at a time, and look up things that you have trouble understanding. – 0RR Dec 14 '20 at 10:29
  • its in this section : cout << monthName << "\t" << sales[i] << "\t" << expenses[i] << "\t" << profit << endl; I'm struggling to fix the alignment of numbers to the profit section. like all is alright except for the numbers in the proft. – Cedric22 Dec 14 '20 at 10:32
  • I'm unsure exactly what you mean, but maybe https://en.cppreference.com/w/cpp/io/manip/setw the std::setw function can be useful, to set a fixed string width for the profit value – 0RR Dec 14 '20 at 10:40
  • i have now included a picture, the February part isn't aligned at all unlike the rest – Cedric22 Dec 14 '20 at 10:43
  • The problem is because the end of February is aligned with the tab, so adding another tab will shift the column over. Whereas with January the tab `\t` will add one space. You can have a look here https://stackoverflow.com/questions/2485963/c-alignment-when-printing-cout if this will help, it will be easier to use the `std::setw` function to align the tables, or you can simplifiy your months to always be the same number of characters e.g. use "Jan" for "January" and "Feb" for "February" so they are all 3 characters. – 0RR Dec 14 '20 at 10:49