Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a complete program which asks the user for a monthly income for 12 months.

ID: 3673101 • Letter: W

Question

Write a complete program which asks the user for a monthly income for 12 months. This information must be stored in a double array. Validate that the user does not enter a negative number for an income, if they do enter a negative number force them to correct it and then proceed with the rest of the program, do not terminate early. Then you should open a file named "output.txt". The first line you should print into the file is "The highest monthly income was: X", followed by a newline, where X is the largest monthly income number. Then you should print out the incomes for each month, separated by newlines, into the file. For example, for incomes 1 2 3 4 5 6.4 7 8.2 9 40 11 12 the first line of the output file would be The highest monthly income was: 40 Followed by the monthly incomes each on their own line. Make sure the file is closed by the end of the program.

Explanation / Answer

#include<iostream>
#include<fstream>
using namespace std;

int main(){
         ofstream myfile;
         myfile.open ("output.txt");
        //myfile << "Writing this to a file. "
        double income[12],num,max = 0;
        for(int i=0;i<12;i++){
                while(1){
                        cout<<" Enter income of month " << i+1 << " : ";
                        cin>>num;
                        if(num>=0)
                                break;
                        cout<<" Negative income.Invalid Entry";
                        }
                income[i] = num;
                if(max<num)
                        max = num;
        }
        myfile<<"The highest monthly income was: "<<max;
        for(int i=0;i<12;i++)
                myfile<<" The income of month "<<i+1<< " : "<<income[i];
        return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote