Hi, can anyone help me fix my code. This program is linked to a project list (with classes). This is the main program. It has to open up a file of strings and output then in order of how many there are. But its not working though. Can someone help me fix it up.

#include <iostream>
#include <fstream>
#include <string>
#include "IPlist.h"
using namespace std;

int main()
{
    IPlist p;
    string fileName;
    ifstream infile;

    cout << "Enter name of file : " << flush;
    cin >> fileName;
    
    infile.open(fileName.c_str());
    
    if(infile >> IPaddress >> count)
    {
         if(!p.empty())
         {
                 p.display();
         }
    }
    
    if(!infile)
    {
               infile.clear();
               cout << "Invalid file name" << endl;
               
    }
    
   system ("pause");
   return 0;
}

Do I have to use my other class functions first? Like I have:
bool isPresent(string address)
updateCount(string address)
void display()
void insertInOrder(string address)

lines 26-31 are in the wrong order. Move them between lines 16 and 18. You have to validate the file is opened before you can read it.

If that still doesn't fix the problem then I suspect the file is not where you think it is. On line 14 use getline() and enter the full or relative path to the file.

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.