The void and Output file will not work for my code and I don't know why.

#include <iostream>
#include <string>
#include <istream>
#include <fstream>
#include <cstdlib>
#include<iomanip>

using namespace std;

#define MAX_ENTRIES 50



struct internet
{
string internetDomain;
string ipNumber;
int counter;
};

// declare the array of structs - Need to determine if this is dynamically allocated or not.
struct internet *list[MAX_ENTRIES];


void selectionSort();
void insertEntry(string, string);
void removeEntry(string);
void findAndIncrementEntry(string);
void printEntries();
void finishAndExit();
void modifyIP(string, string);
int arrayIndex(string);
void printEntriesNotVisited();	
void printEntriesVisitedMost();
void OutputFile(std:: ofstream& outVariable, internet list[], int listSize);



int main()
{

ifstream inFile;
ifstream OutputFile;
string fileName;
string domainName;
string ipAddress;
int ARRAY_SIZE=50;

//array name
internet location[ARRAY_SIZE];



// Prompt the user to enter the input filename 
cout << "Enter the filename:";
cin >> fileName;

// Open the file name entered. 
inFile.open(fileName.c_str());

// Loop through the file and do the actions that it does until you get to the X instruction or end of file.
if( inFile.is_open()) 
{
while(! inFile.eof() ) 
{
char action;

// Get the line
inFile >> action;

// Now perform the action specified by the file.
switch(action) 
{
case 'X':
finishAndExit();
break;
case 'M':
inFile >> domainName;
inFile >> ipAddress;
modifyIP(domainName,ipAddress);
break;
case 'I':
inFile >> domainName;
inFile >> ipAddress;
insertEntry(domainName,ipAddress);
break;
case 'R':
inFile >> domainName;
removeEntry(domainName);
break;
case 'F':
inFile >> domainName;
findAndIncrementEntry(domainName);
break;
case 'P':
printEntries();
break;
}
}
} else {
cout << "File " << fileName << " not found";

}
}
int ARRAY_SIZE=50;

OutputFile (internet, int ARRAY_SIZE);

//Close input and output files
inFile.close();
OutputFile.close();
}

void finishAndExit() {
string test;
// Sort the intems in the array using the selectionSort
selectionSort();
// Print the sorted entries
printEntries();

// Print the entries with a zero counter
printEntriesNotVisited();	

// Print the entry with the largest 
printEntriesVisitedMost();

// Write output to file.
OutputFile();

exit(0);
}

void modifyIP(string domainName, string ipAddress) {

// Look for the item that is being changed and then update it accordingly
int entryIndex = arrayIndex(domainName);
if(entryIndex != -1)	{
list[entryIndex]->ipNumber = ipAddress;
}
return;
}

void insertEntry(string domainName, string ipAddress) {

cout << "Adding " << domainName << endl;
// Need to create the space for the entry and update the structures data elements.	
for(int i=0;i < MAX_ENTRIES; i++) {
if(list[i] == NULL ) {
list[i] = new internet();
list[i]->internetDomain = domainName;
list[i]->ipNumber = ipAddress;
list[i]->counter = 0;
return;
}
}
}


void removeEntry(string domainName) {

cout << "Removing " << domainName << endl;

int entryIndex = arrayIndex(domainName);
if(entryIndex != -1)	{
delete list[entryIndex];
list[entryIndex]=NULL;
}

}

void findAndIncrementEntry(string domainName) {

cout << "Finding " << domainName << endl;

int entryIndex = arrayIndex(domainName);
if(entryIndex != -1)	{
list[entryIndex]->counter++;
}

}

void printEntries() {

cout << "Printing Entries: " << endl;
sizeof(list);
for(int i=0; i < MAX_ENTRIES ; i++) {
if(list[i] != NULL && list[i]->internetDomain != "") {
cout << "Domain:" << list[i]->internetDomain << " IP:" << list[i]->ipNumber << " Count: " << list[i]->counter << endl;
}
}

}

void printEntriesNotVisited() {

cout << "Entries not visited: " << endl;
sizeof(list);
for(int i=0; i < MAX_ENTRIES ; i++) {
if(list[i] != NULL && list[i]->internetDomain != "") {
if(list[i]->counter == 0) {
cout << "Domain:" << list[i]->internetDomain << " IP:" << list[i]->ipNumber << " Count: " << list[i]->counter << endl;
}
}
}

}

void printEntriesVisitedMost() {

int maxCount = 0;
int maxCountIndex;


for(int i=0; i < MAX_ENTRIES ; i++) {
if(list[i] != NULL && list[i]->internetDomain != "") {
if(maxCount < list[i]->counter) {
maxCount = list[i]->counter;
}
}
}

cout << "Most popular Sites: " << endl;
for(int i=0; i < MAX_ENTRIES ; i++) {
if(list[i] != NULL && list[i]->internetDomain != "") {
if(maxCount == list[i]->counter) {
cout << "Domain:" << list[i]->internetDomain << " IP:" << list[i]->ipNumber << " Count: " << list[i]->counter << endl;
}
}
}

}


int arrayIndex(string domainName) {

for(int i=0; i < MAX_ENTRIES ; i++) {
if(list[i] != NULL && list[i]->internetDomain == domainName) {
return i;
}
}

// Can't find the specific domain.
return -1;

}


// This will need to be modified to accept the internet structs array
void selectionSort()
{
int index;
int smallestIndex;
int minIndex;
struct internet *temp;
for (index = 0; index < MAX_ENTRIES - 1; index++)
{
if(list[index] == NULL)
continue;
//Step a
smallestIndex = index; 
for (minIndex = index + 1; minIndex < MAX_ENTRIES; minIndex++) {
if(list[minIndex] == NULL) {
continue;
}
if (list[minIndex]->internetDomain.compare(list[smallestIndex]->internetDomain) < 0)
smallestIndex = minIndex; 
//Step b
}
temp = list[smallestIndex];
list[smallestIndex] = list[index];
list[index] = temp;
}
}

void OutputFile(ofstream& outVariable, internet list[], int listSize)
{

//Outputs header of list
outVariable << setw(30) << left << "Internet domains"
<< "Number observed\n\n";

//Loops through array and outputs list of 2 columns: species and count
for (int counter = 0; counter< listSize; counter++)
{
if (list[counter].internetDomain != "")//If sturct is not null
{
outVariable << setw(30) << left << list[counter].internetDomain << " "
<< setw(3) << right << list[counter].counter << "\n";
}
}
}//End outputData

This is what I am supposed to do. write a C++ program that will manage a list of domain names and IP numbers stored in an array of structs. This program will give you practice inputting data from files, writing and calling functions, using arrays and outputting data to a file. You should only use one array of structs.

The infotype in this problem will consist of an Internet domain name (string), an IP number (string) and a counter (int). New entries are added to the end of the array. Whenever an item is accessed by the F(ind) or M(odify) command, its counter is incremented by one.
Prompt the user to input the name of the data file. (3 pts) The input contains lines of the following format:
<command> < data for processing the command>
commands are: A, M, D, F, P, Q
//----------------------------------------------------------------------------------------------------------------
A (insert) will be followed by a domain name and IP number
A: (10 pts) Add an entry with the domain name, IP number, and zero counter to the end of the array
//----------------------------------------------------------------------------------------------------------------
M (modify) will be followed by a domain name and a new IP number
M: ( 10 pts) Changes the IP number for the indicated domain name. Increment the counter.
//----------------------------------------------------------------------------------------------------------------
D (delete ) will be followed by the name to delete from the array
D: (8 pts) deletes the entry with the indicated domain name from the array
//----------------------------------------------------------------------------------------------------------------
F (find) will be followed by a domain name to find in the list
F: (8 pts) print out the IP number for the indicated domain name. Increment the counter.
//----------------------------------------------------------------------------------------------------------------
P (print) will have no additional data
P: (5 pts) Print the contents of the array; print domain name, IP number, counter for each entry
//----------------------------------------------------------------------------------------------------------------
Q (quit) will have no additional data.
Q: (16 pts) stop processing; do the following:
Use Selection sort to sort the array in order by domain name.
Print the sorted list to the screen.
Prompt the user to input a top level domain ( for example edu ) Print a list of all list entries from that domain to the screen. (Allow multiple entries)
Print the list entry with the largest counter to the screen. (this was the most popular web site)
Print the sorted list to an output file. Make sure to include your name and lab CRN in the output file.
Name the output file with your first and last name
Notes:
1. Remember to review the grading algorithm.
2. Save a copy of the file you hand in. Make sure to leave the file unchanged after your submission until you receive a grade on the project assignment.
3. Sample data one.txt, two.txt, and three.txt ,try.txt
4. Print a message if the domain name being searched for is not in the array.(modify, find, or delete)
5. Do not use global variables; pass parameters.
6. You should use functions to accomplish each task. You must have at least five functions.
7. Make sure that your output has a heading and is labeled.
8.Use function prototypes; function definitions should be after the main.
9. Name your source code file using your last name and first initial as follows: lastname_firstInitial_Prj4.cpp
For example Stewie Griffin would save his source code in a file name Griffin_S_Prj4.cpp
10. Echo print the data input from each line.
11. This program will have many lines of output. You should count the lines of output and use an if similar to the following to allow the user to see all of the output.

if ( lines % 20 == 0)
{
cout << "enter a character to continue ";
cin >> it;
}

I have code working but when i type in the file name it says file not found and closes. I checked code and am confused because the file is their.

#include <iostream>
#include <string>
#include <istream>
#include <fstream>
#include <cstdlib>
#include<iomanip>

using namespace std;

#define MAX_ENTRIES 50



struct internet
{
string internetDomain;
string ipNumber;
int counter;
};

// declare the array of structs - Need to determine if this is dynamically allocated or not.
struct internet *list[MAX_ENTRIES];


void selectionSort();
void insertEntry(string, string);
void removeEntry(string);
void findAndIncrementEntry(string);
void printEntries();
void finishAndExit();
void modifyIP(string, string);
int arrayIndex(string);
void printEntriesNotVisited();	
void printEntriesVisitedMost();
void OutputFile(std:: ofstream& outVariable, internet list[], int listSize);



int main()
{

ifstream inFile;
ifstream OutputFile;
string fileName;
string domainName;
string ipAddress;
int ARRAY_SIZE=50;

//array name
internet location[ARRAY_SIZE];



// Prompt the user to enter the input filename 
cout << "Enter the filename:";
cin >> fileName;
cout << endl;

// Open the file name entered. 
inFile.open(fileName.c_str());

// Loop through the file and do the actions that it does until you get to the X instruction or end of file.
if( inFile.is_open()) 
{
while(! inFile.eof() ) 
{
char command;

// Get the line
inFile >> command;

// Now perform the action specified by the file.
while (command !='Q') 
{
      cout<<command<<endl;
      
}
if(command=='A')
{
inFile >> domainName;
inFile >> ipAddress;
modifyIP(domainName,ipAddress);
}
else if (command=='M')
{
inFile >> domainName;
inFile >> ipAddress;
insertEntry(domainName,ipAddress);
}
else if (command=='D')
{
inFile >> domainName;
removeEntry(domainName);
}
else if (command=='F')
{
inFile >> domainName;
findAndIncrementEntry(domainName);
}
else if (command=='P')
{
printEntries();
}
else if (command=='X')
      {
finishAndExit();
}
}
} else {
cout << "File " << fileName << " not found";




int ARRAY_SIZE=50;


void OutputFile();


//Close input and output files
inFile.close();
//OutputFile.close();
}
}
void finishAndExit() {
string test;
// Sort the intems in the array using the selectionSort
selectionSort();
// Print the sorted entries
printEntries();

// Print the entries with a zero counter
printEntriesNotVisited();	

// Print the entry with the largest 
printEntriesVisitedMost();

// Write output to file.
void OutputFile();

exit(0);
}

void modifyIP(string domainName, string ipAddress) {

// Look for the item that is being changed and then update it accordingly
int entryIndex = arrayIndex(domainName);
if(entryIndex != -1)	{
list[entryIndex]->ipNumber = ipAddress;
}
return;
}

void insertEntry(string domainName, string ipAddress) {

cout << "Adding " << domainName << endl;
// Need to create the space for the entry and update the structures data elements.	
for(int i=0;i < MAX_ENTRIES; i++) {
if(list[i] == NULL ) {
list[i] = new internet();
list[i]->internetDomain = domainName;
list[i]->ipNumber = ipAddress;
list[i]->counter = 0;
return;
}
}
}


void removeEntry(string domainName) {

cout << "Removing " << domainName << endl;

int entryIndex = arrayIndex(domainName);
if(entryIndex != -1)	{
delete list[entryIndex];
list[entryIndex]=NULL;
}

}

void findAndIncrementEntry(string domainName) {

cout << "Finding " << domainName << endl;

int entryIndex = arrayIndex(domainName);
if(entryIndex != -1)	{
list[entryIndex]->counter++;
}

}

void printEntries() {

cout << "Printing Entries: " << endl;
sizeof(list);
for(int i=0; i < MAX_ENTRIES ; i++) {
if(list[i] != NULL && list[i]->internetDomain != "") {
cout << "Domain:" << list[i]->internetDomain << " IP:" << list[i]->ipNumber << " Count: " << list[i]->counter << endl;
}
}

}

void printEntriesNotVisited() {

cout << "Entries not visited: " << endl;
sizeof(list);
for(int i=0; i < MAX_ENTRIES ; i++) {
if(list[i] != NULL && list[i]->internetDomain != "") {
if(list[i]->counter == 0) {
cout << "Domain:" << list[i]->internetDomain << " IP:" << list[i]->ipNumber << " Count: " << list[i]->counter << endl;
}
}
}

}

void printEntriesVisitedMost() {

int maxCount = 0;
int maxCountIndex;


for(int i=0; i < MAX_ENTRIES ; i++) {
if(list[i] != NULL && list[i]->internetDomain != "") {
if(maxCount < list[i]->counter) {
maxCount = list[i]->counter;
}
}
}

cout << "Most popular Sites: " << endl;
for(int i=0; i < MAX_ENTRIES ; i++) {
if(list[i] != NULL && list[i]->internetDomain != "") {
if(maxCount == list[i]->counter) {
cout << "Domain:" << list[i]->internetDomain << " IP:" << list[i]->ipNumber << " Count: " << list[i]->counter << endl;
}
}
}

}


int arrayIndex(string domainName) {

for(int i=0; i < MAX_ENTRIES ; i++) {
if(list[i] != NULL && list[i]->internetDomain == domainName) {
return i;
}
}

// Can't find the specific domain.
return -1;

}


// This will need to be modified to accept the internet structs array
void selectionSort()
{
int index;
int smallestIndex;
int minIndex;
struct internet *temp;
for (index = 0; index < MAX_ENTRIES - 1; index++)
{
if(list[index] == NULL)
continue;
//Step a
smallestIndex = index; 
for (minIndex = index + 1; minIndex < MAX_ENTRIES; minIndex++) {
if(list[minIndex] == NULL) {
continue;
}
if (list[minIndex]->internetDomain.compare(list[smallestIndex]->internetDomain) < 0)
smallestIndex = minIndex; 
//Step b
}
temp = list[smallestIndex];
list[smallestIndex] = list[index];
list[index] = temp;
}
}

void OutputFile(ofstream& outVariable, internet list[], int listSize)
{

//Outputs header of list
outVariable << setw(30) << left << "Internet domains"
<< "Number observed\n\n";

//Loops through array and outputs list of 2 columns: species and count
for (int counter = 0; counter< listSize; counter++)
{
if (list[counter].internetDomain != "")//If sturct is not null
{
outVariable << setw(30) << left << list[counter].internetDomain << " "
<< setw(3) << right << list[counter].counter << "\n";
}
}
}//End outputData

For your own sanity (and that of others reading it), please indent your code, e.g.:

void functionName (type1 arg1, ...)
{
    some statements;
    while (condition) {
        more statements;
    }
}

Also, when your problem is simply that your code isn't opening the specified file, don't post 200 extra lines of irrelevant functions. Trim the source down to what demonstrates the problem, and post just that.

That said, I don't see anything obvious wrong with your code. Is the filename printed out correctly and completely in your else-block? In particular, if there is any white-space in your path, cin >> ... is going to break at that point, instead use getline(cin, fileName); .

Also, just because the file didn't open correctly doesn't mean the file wasn't found. What are you typing in for your filename? What is the error message printing back out?

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.