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;
}

Dani AI

Generated

A few concise, practical points that build on remarks from and and address some bugs that often hide behind “it won’t run”:

First, the silent exit is usually an input-file / working-directory problem, but even with the file present the current parsing logic hides errors. Avoid testing eof() to control the read loop and avoid reading text then using atoi — a failed read or a non‑numeric line commonly produces an extra zero in the vector. A safer pattern is to read integers with extraction so the loop only succeeds on valid integers:

int value;
while (in >> value) {
    salaries.push_back(value);
}

If the input can contain garbage or blank lines, read lines and parse with std::stoi (with error handling) instead.

Second, protect the stats code against empty data. The current approach leaves minimumSalary and maximumSalary at sentinel numeric_limits values if the vector is empty; write stats only after verifying salaries.empty() is false. For a concise one‑pass min/max use std::minmax_element and check the result before writing. Also verify out.is_open() before attempting to write stats.txt.

Third, Visual Studio notes that help reduce confusion: either run with Ctrl+F5 or set a breakpoint so the console doesn’t disappear, set the project Debugging → Working Directory if needed, or mark newdata.txt in Solution Explorer with “Copy to Output Directory = Copy if newer” so it lands next to the .exe. Prefer printing a clear diagnostic and returning a nonzero exit code rather than immediately terminating, so failures are visible during debugging.

Checklist: ensure the file is in the debugger working directory (or use a full path), switch the read loop to extraction, guard against empty input before computing min/max, and check streams before using them.

Recommended Answers

All 2 Replies

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.