0

Can you tell me what's wrong with it because it's not running on visual studio and I need it right now???

#include <iostream>
#include<vector>
#include<algorithm>
#include<limits>
#include<fstream>
#include<string>

using namespace std;

class PayRoll
{
    private:
      vector<int> salaries;
    public:
      PayRoll();
      bool searchSalary(int salary);
      void generateStats();
      void printSalaries();
};

PayRoll::PayRoll()
{
    ifstream in("newdata.txt");
    if(!in.good())
    {
        cout<<"Sorry Data File is not Found"<<endl;
        exit(1);
    }
    else
    {   string str;
        while(!in.eof())
        {
            in>>str;
            int temp = atoi(str.c_str());
            salaries.push_back(temp);
        }
    }
}

bool PayRoll::searchSalary(int salary)
{
    return (find(salaries.begin(),salaries.end(),salary) != salaries.end());
}

void PayRoll::generateStats()
{
    int minimumSalary = numeric_limits<int>::max();
    int maximumSalary = numeric_limits<int>::min();

    for(vector<int>::const_iterator iter = salaries.begin();iter<salaries.end();iter++)
    {
        if(*iter<minimumSalary) minimumSalary = *iter;
        if(*iter>maximumSalary) maximumSalary = *iter;
    }
    ofstream out("stats.txt");
    out<<"Minimum Salary is: "<<minimumSalary<<endl;
    out<<"Maximum Salary is: "<<maximumSalary<<endl;

}

void PayRoll::printSalaries()
{
    for(vector<int>::const_iterator iter = salaries.begin();iter<salaries.end();iter++)
    {
        cout<<(*iter)<<endl;
    }
}




int main() {

    PayRoll payroll;
    int selection;

    do
    {
    cout<<"Search Salary, Enter 1"<<endl;
    cout<<"Generates Stats file, Enter 2"<<endl;
    cout<<"Print Salaries, Enter 3"<<endl;
    cout<<"Quit, Enter 4"<<endl;
    cin>>selection;
    switch(selection)
    {
        case 1:
          int tempSalary;
          cout<<"Enter Salary to search: "<<endl;
          cin>>tempSalary;
          if(payroll.searchSalary(tempSalary)) cout<< "Salary Found "<<endl;
          else cout<<"Salary not found"<<endl;
          break;
        case 2:
          payroll.generateStats();
          break;
        case 3:
          payroll.printSalaries();
          break;
        case 4:
          break;
        default:
          cout<<"Enter a valid Selection"<<endl;
          break;
    }
    }while(selection != 4);


    return 0;
}
3
Contributors
2
Replies
32
Views
2 Years
Discussion Span
Last Post by cgeier
0

That builds and runs fine. My guess is that it finishes very quickly because it can't find the datafile and you don't even realise it ran and finished.

Edited by Moschops

0

If you want to see the message comment out line #27 exit(1);

In VC++ you can do the following to see where your program is running:

Add include statement:

#include<direct.h>

Then add the following code to "Payroll::Payroll"

char* buffer;
string curDir;

// Get the current working directory: 
if( (buffer = _getcwd( NULL, 0 )) == NULL )
{
      perror( "_getcwd error" );
}
else
{
    curDir = buffer;
    printf("Current Directory: %s \n", buffer);
    free(buffer);
}

Then change

from:

cout<<"Sorry Data File is not Found"<<endl;

to:

cout<<"Sorry Data File is not Found (" << curDir << ")" << endl;

Better yet, you can make the path of your input file fully-qualified to ensure that the file is where you expect it to be.

Resource:
_getcwd, _wgetcwd

Edited by cgeier

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.