Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 709967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:33:44+00:00 2026-05-14T04:33:44+00:00

I’m almost finished with my program, but I have one problem that I can’t

  • 0

I’m almost finished with my program, but I have one problem that I can’t seem to solve. How do I get the minimum spending?

My lecturer gave me the task:

The company you work at receives a monthly report in a text format. The report contains the
following information.

  • Department Name
  • Head of Department Name
  • Month
  • Minimum spending of the month
  • Maximum spending of the month

Your program is to obtain the name of the input file from the user. Implement a structure to represent the data:

Once the file has been read into your program, print out the following statistics for the user:

  • List which department has the minimum spending per month by month
  • List which department has the minimum spending by month by month

Write the information into a file called “MaxMin.txt”

Then do a processing so that the Department Name, Head of Department Name, Minimum
spending and Maximum spending are written to separate files based on the month, eg Jan, Feb,
March and so on.

Here is the text file I was given to work with:

Engineering Bill Jan 2000 15000
IT  Jack Jan 300 20000
HR  Jill Jan 1500 10000

Engineering  Bill Feb 5000 45000
IT  Jack Feb 4500 7000
HR  Jill Feb 5600 60000

Engineering  Bill Mar 5000 45000
IT  Jack Mar 4500 7000
HR  Jill Mar 5600 60000

Engineering  Bill Apr 5000 45000
IT  Jack Apr 4500 7000
HR  Jill Apr 5600 60000

Engineering Bill May 2000 15000
IT  Jack May 300 20000
HR  Jill May 1500 10000

Engineering Bill Jun 2000 15000
IT  Jack Jun 300 20000
HR  Jill Jun 1500    10000

and here’s the c++ code I’ve written

ue#include <iostream>
#include <fstring>
#include <string>

using namespace std;

struct Record 
{
 string depName;
 string head;
 string month;
 float max;
 float min;
 string name;
}
myRecord[19];

int main () 
{
string line;
ofstream minmax,jan,feb,mar,apr,may,jun;
char a[50];
char b[50];
int i = 0,j,k;
float temp;
//float maxjan=myRecord[0].max,maxfeb=myRecord[0].max,maxmar=myRecord[0].max,maxapr=myRecord[0].max,maxmay=myRecord[0].max,maxjune=myRecord[0].max;
 float minjan=myRecord[1].min,minfeb=myRecord[1].min,minmar=myRecord[1].min,minapr=myRecord[1].min,minmay=myRecord[1].min,minjune=myRecord[1].min;
float maxjan=0,maxfeb=0,maxmar=0,maxapr=0,maxmay=0,maxjune=0;
 //float minjan=0,minfeb=0,minmar=0,minapr=0,minmay=0,minjune=0;
string maxjanDep,maxfebDep,maxmarDep,maxaprDep,maxmayDep,maxjunDep;
string minjanDep,minfebDep,minmarDep,minaprDep,minmayDep,minjunDep;
 cout<<"Enter file name: ";
cin>>a;
ifstream myfile (a);
//minmax.open ("MaxMin.txt");

if (myfile.is_open()){
 while (! myfile.eof()){
  myfile>>myRecord[i].depName>>myRecord[i].head>>myRecord[i].month>>myRecord[i].min>>myRecord[i].max;
  cout << myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl;
i++;
}
myfile.close();
}
else{ 
 cout << "Unable to open file"<<endl;}

cout<<"Enter file name: ";
cin>>b;
ifstream myfile1 (b);
 minmax.open ("MaxMin.txt");
 jan.open ("Jan.txt");
 feb.open ("Feb.txt");
 mar.open ("March.txt");
 apr.open ("April.txt");
 may.open ("May.txt");
 jun.open ("Jun.txt");
if (myfile1.is_open()){
 while (! myfile1.eof()){
  myfile1>>myRecord[i].depName>>myRecord[i].head>>myRecord[i].month>>myRecord[i].min>>myRecord[i].max;
if (myRecord[i].month == "Jan"){
jan<< myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl;

if (maxjan< myRecord[i].max){
  maxjan=myRecord[i].max;
  maxjanDep=myRecord[i].depName;}

  for (k=1;k<=3;k++){
  for (j=0;j<2;j++){
   if (myRecord[j].min>myRecord[j+1].min){
temp=myRecord[j].min;
   myRecord[j].min=myRecord[j+1].min;
   myRecord[j+1].min=temp;
   minjanDep=myRecord[j].depName;
   }}}
}
if (myRecord[i].month == "Feb"){
 feb<< myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl;
 if (maxfeb< myRecord[i].max){
 maxfeb=myRecord[i].max;
 maxfebDep=myRecord[i].depName;}

for (k=1;k<=3;k++){
  for (j=0;j<2;j++){
   if (myRecord[j].min>myRecord[j+1].min){
temp=myRecord[j].min;
   myRecord[j].min=myRecord[j+1].min;
   myRecord[j+1].min=temp;
   minfebDep=myRecord[j+1].depName;
   }}}

}

if (myRecord[i].month == "Mar"){
 mar<< myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl;
 if (maxmar< myRecord[i].max){
maxmar=myRecord[i].max;
maxmarDep=myRecord[i].depName;}
 if (myRecord[i].min<minmar){
  minmar=myRecord[i].min;
minmarDep=myRecord[i].depName;
 }}

if (myRecord[i].month == "Apr"){
 apr<< myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl;
 if (maxapr< myRecord[i].max){
 maxapr=myRecord[i].max;
 maxaprDep=myRecord[i].depName;}

 if (minapr>myRecord[i].min){
  minapr=myRecord[i].min;
  minaprDep=myRecord[i].min;}
}

if (myRecord[i].month == "May"){
 may<<myRecord[i].depName<<myRecord[i].head<<myRecord[i].month<<myRecord[i].min<<myRecord[i].max<<endl;
 if (maxmay< myRecord[i].max){
 maxmay=myRecord[i].max;
 maxmayDep=myRecord[i].depName;}

 if (minmay>myRecord[i].min){
  minmay=myRecord[i].min;
  minmayDep=myRecord[i].depName;}    
}

if (myRecord[i].month == "Jun"){
 jun<< myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl;
 if (maxjune< myRecord[i].max){
 maxjune=myRecord[i].max;
 maxjunDep=myRecord[i].depName;}

 if (minjune>myRecord[i].min){
  minjune=myRecord[i].min;
  minjunDep=myRecord[i].depName;}
}

i++;

myfile.close();
}
minmax<<"department that has maximum spending at jan "<<maxjanDep<<endl;
minmax<<"department that has minimum spending at jan "<<minjanDep<<endl;
minmax<<"department that has maximum spending at Feb "<<maxfebDep<<endl;
minmax<<"department that has minimum spending at Feb "<<minfebDep<<endl;
minmax<<"department that has maximum spending at March "<<maxmarDep<<endl;
minmax<<"department that has minimum spending at March "<<minmarDep<<endl;
minmax<<"department that has maximum spending at April "<<maxaprDep<<endl;
minmax<<"department that has minimum spending at April "<<minaprDep<<endl;
minmax<<"department that has maximum spending at May "<<maxmayDep<<endl;
minmax<<"department that has minimum spending at May "<<minmayDep<<endl;
minmax<<"department that has maximum spending at June "<<maxjunDep<<endl;
minmax<<"department that has minimum spending at June "<<minjunDep<<endl;
}

else{
 cout << "Unable to open file"<<endl;}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:33:45+00:00Added an answer on May 14, 2026 at 4:33 am

    My first suggestion is that the prof wants a list of Record – rather than hardcoding in 19, use a vector and fill it as you go.

    vector<Record> records;
    

    My second suggestion is to use a loop to process each month.

    vector<string> months;
    months.push_back("Jan");
    months.push_back("Feb");
    
    for (int i = 0; i < months.size(); ++i) {
      string month = months[i];
      ReadInMonthBudget(&records, month); // Then use a function to read in each month file. 
    }
    

    Then you can work on collecting your stats:

    for (int i = 0; i < months.size(); ++i) {
      string month = months[i];
      cout << "Min spender in month " << month << ": " << FindMinSpenderInMonth(records, month);
    }
    

    Using functions will make your code much easier to understand (and write), because it breaks the problem down into much smaller chunks that are easier to solve.

    Start by getting it working, then improve on it. If this is a college level class, your professor may want you to come up with a better than O(n^2) solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 369k
  • Answers 369k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The Fluid Pager Widget, from their Infusion framework, handles this… May 14, 2026 at 6:30 pm
  • Editorial Team
    Editorial Team added an answer You could create a user-defined function for this: CREATE FUNCTION… May 14, 2026 at 6:30 pm
  • Editorial Team
    Editorial Team added an answer Basically, I would put a class on the p tag… May 14, 2026 at 6:30 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.