Infinite Loop

The code I have so far causes an infinite loop. I have both my code and the input from the file. Ignore the Quit Function. It's not done yet. Can anyone see where the loop goes wrong here?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>


using namespace std;

struct domList
{
string domName;
string ipAddress;
int counter;
};

void insertOne(domList list[], ifstream& inFile, int len);
void change(domList list[], string dName, string IpNum, int len);
void Delete(domList list[], string dName, int& lengthList);  
void Find(domList list[], string dName, int len);  
void Print (domList list[], int len);

int main (){
    
    char command;
    
    domList list[NULL];
    int listLength = 0;
    
    string ipNum, DomName;
    int counter = 0;
    bool x;
    ifstream dataFile;
    
    //stores the name of the dataFile
    string dataFile1;
    
    cout << "Please enter the data file you wish to use: ";
    cin >> dataFile1;
      
    dataFile.open (dataFile1.c_str());
    if (!dataFile) 
    {
       cout << "bummer file\n\n";
       system ("pause");
       return 1;
    }
    
    while(dataFile){
       dataFile >> command;
       switch(command){
                       case 'A':
                                insertOne(list, dataFile, listLength);
                                listLength++;
                       break;
                       case 'M':
                                change(list, DomName, ipNum, listLength);
                       break;
                       case 'D':
                                Delete(list, DomName, listLength);
                                listLength--;
                       break;
                       case 'F':
                                Find(list, DomName, listLength);
                       break;
                       case 'P':
                                Print(list, listLength);
                       break;
                       case 'Q':
                                
                                system("pause");
                                return 0;
                       default: 
                                cout << "Invalid Command." << endl;
    }
}
}



void insertOne(domList list[], ifstream& inFile, int len)
{
     domList one;
     inFile >> one.domName >> one.ipAddress;
     one.counter = 0;
     list[len] = one;
}

//change Function

void change(domList list[], string dName, string IpNum, int len)
{
     for(int i = 0; i < len; i++)
     {
          if(list[i].domName == dName)
          {
                list[i].ipAddress = IpNum;
                list[i].counter += 1;
          }
     }
}

//Delete Function

void Delete(domList list[], string dName, int& lengthList)
{
     for(int i = 0; i < lengthList; i++)
     {
          if(list[i].domName == dName)
          {
               for(int l = i; l < lengthList; l++)
               {
                     list[l] = list[l+1];
               }
          }
     }
}

//Find Function

void Find(domList list[], string dName, int len)
{
     for (int i = 0; i < len; i++)
     {
         if (list[i].domName == dName)
         {
              cout << list[i].ipAddress << endl;
              list[i].counter += 1;
         }    
     }
}

//Print Function

void Print (domList list[], int len)
{
     for (int i = 0; i < len; i++)
     /{
         cout << setw(20) << list[i].domName << setw(15)
              << list[i].ipAddress << setw(6)
              << list[i].counter << endl;
     }
}


//Quit Function

void Quit (domList list[], int len)
{
     for (int i = 0; i < len; i++)
     {
         for (int l = 0; l < len; l++)
         {
             if(list[i].domName[5] <= list[l].domName[5])
             {
                  
             }
                
         }
     }
}



This is the Input for the program. File name "two.txt"

A 	www.barkingmad.org 	010.023.111.202
A 	www.toadstools.org 	111.162.001.122
A 	www.stools.com 		012.240.117.219
A 	www.stooges.biz 	113.021.097.138
A 	www.relish.edu 		014.126.222.047
A 	www.canuk.ca 		115.293.113.196
A 	www.cherokee.org 	016.015.131.215
A 	www.lancashire.com 	117.127.228.134
A 	www.froggy.biz 		018.233.106.263
A 	www.ewarrior.net	119.191.021.112
P
F	www.ewarrior.net
F	www.barkingmad.org
F	www.stooges.com
F	www.relish.edu
D	www.canuk.ca
D	www.cherokee.org 
A	www.odu.edu            192.012.174.002
F	www.lancashire.com
F	www.yorkshire.com
F	www.cherokee.org
F	www.canuk.ca
F	www.froggy.biz 
F	www.ewarrior.com
M	www.ewarrior.com 	192.168.113.112
D	www.barkingmad.org 
F	www.ewarrior.com
F	www.lancashire.com
F	www.odu.edu
Q
Last edited on
line 49.

Try changing
while(dataFile){
to
while(!dataFile.eof())
Tried it. Doesn't work. I think it's because I haven't finished the Quit function yet. I'll see what that does. It's supposed to do this:

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.
Stewbond is 100% correct but i wanted to explain why. I want to point out your code and use it as an example

1
2
3
4
5
6
7
8
9
    dataFile.open (dataFile1.c_str());
    if (!dataFile) 
    {
       cout << "bummer file\n\n";
       system ("pause");
       return 1;
    }
    
    while(dataFile){


The first line in the code I copied is your attempt to open the selected file. When in the next line, your if statement tests to see if it opened properly. If it didn't, say the file didn't exist, then it would of returned with 0 saying it wasn't set up.

Now you get to your while statement, while the file is setup, keep trying to run the loop. You are testing once again that the variable is setup properly to a file which even if you come to the end of the file will always be true. The function dataFile.eof() checks for the end of the file ( eof = end of file).

Sorry Stewbond, I don't mean to jump on your response but I wanted people who saw this to understand why your answer was accurate because I like people to understand code rather then use it blindly and get more confused later when things get more complicated.
Topic archived. No new replies allowed.