I do not know why when I compile it, on why does the last cylinder file print out multiple times int tags1 and tags2. I bolded them so you would see what I am talking about. Could someone please help me out?

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

using namespace std;
// function prototypes
void  tagReader ( ifstream& inFile, int&count);
void getEmployee ( string& who, double& rate, int &hours );
double paycheck (int hours, double rate);
void repeater ( string word, int num);
bool valid (int val);
void header ();


//=========================================================
int main() 
{    
    int count = 0; 
    ifstream inFile;
    string cylinder, serial;
    double num1, num2;
    double counter;
   
    //----------------------------------------
    // Call function tagReader with tags1.txt
    inFile.open("tags1.txt");
    
    if (!inFile) 
    {
        cout << "\n\nError! - Invalid file name!  (1)\n\n";
        system ("pause");
        return 1;
    }
         inFile >> cylinder;
         while (inFile) 
         {
    
         inFile>> serial;
         inFile >> num1;
         inFile >> num2;
         cout << fixed << showpoint << setprecision(2);
         cout <<"Cylinder: " << cylinder << " " << serial << " " << num1 << " " << num2 << endl << endl;   
         count = 1;
         counter++;
         while (count < num1)
         {     inFile >> cylinder ;
               inFile >> serial;
               inFile >> num1;
               inFile >> num2;
               cout << "\nCylinder: " << cylinder << " " << serial << " " << num1 << " " << num2 << endl << endl; 
               count++;        
         }
       }

    cout <<"Results from tags1.txt :\n";
    tagReader ( inFile, count);    
    cout << "File tags1.txt had " << count << " data items. \n\n";
    inFile.close ();
    
    //----------------------------------------
    // Call function tagReader with tags2.txt
    inFile.clear (); 
    inFile.open("tags2.txt");
    if (!inFile) 
         {
          cout << "\n\nERROR!  Invalid file name! (2)\n\n";
          system ("pause");
          return 1;
         }   
         
         inFile >> cylinder;
         while (inFile) 
         {
    
         inFile>> serial;
         inFile >> num1;
         inFile >> num2;
         cout << fixed << showpoint << setprecision(2);
         cout <<"Cylinder: " << cylinder << " " << serial << " " << num1 << " " << num2 << endl << endl;   
         count = 1;
         counter++;
         while (count < num1)
         {     inFile >> cylinder ;
               inFile >> serial;
               inFile >> num1;
               inFile >> num2;
               cout << "\nCylinder: " << cylinder << " " << serial << " " << num1 << " " << num2 << endl << endl; 
               count++;        
         }
       }

    cout <<"Results from tags2.txt :\n";
    tagReader ( inFile, count);    
    cout << "File tags2.txt had " << count << " data items. \n\n";
    inFile.close ();
    
   system("pause");
   return 0;
}
//**********************************************************************
//Function Name: GetLength
// Prompts the user to input the lenth of a rectangular room
// Returns the int value input by the user
//**********************************************************************
void tagReader (ifstream& inFile, int&count)
{    

}

Output:

Cylinder: bsharkdata 1782 12.00 82.90


Cylinder: ftmyersfeb 7150 11.00 32.89


Cylinder: sandiego99 8800 15.20 52.76


Cylinder: turtles666 2180 11.00 14.09


Cylinder: turtles765 4223 11.90 17.10


Cylinder: FredsFish 4145 11.60 42.22


Cylinder: GreatBaReef 8673 10.00 11.40


Cylinder: VaBch77June 1111 13.50 44.00


Cylinder: VaBch77June 1111 13.50 44.00


Cylinder: VaBch77June 1111 13.50 44.00


Cylinder: VaBch77June 1111 13.50 44.00


Cylinder: VaBch77June 1111 13.50 44.00


Cylinder: VaBch77June 1111 13.50 44.00


Cylinder: VaBch77June 1111 13.50 44.00

Results from tags1.txt :
File tags1.txt had 14 data items.

Cylinder: turtles686 2110 14.88 65.19


Cylinder: turtles995 1123 61.60 77.17


Cylinder: UKFishData 4145 99.04 22.98


Cylinder: Gr8BaReef2 8673 19.71 76.12


Cylinder: VaBch78876 1111 69.11 51.10


Cylinder: shark9987 1782 83.40 89.11


Cylinder: florida331 3130 14.01 52.89


Cylinder: florida876 6600 15.20 52.76


Cylinder: flKeys331 4410 64.66 56.22


Cylinder: flKeys877 6630 16.27 52.77


Cylinder: flKeys877 6630 16.27 52.77


Cylinder: flKeys877 6630 16.27 52.77


Cylinder: flKeys877 6630 16.27 52.77


Cylinder: flKeys877 6630 16.27 52.77


Cylinder: flKeys877 6630 16.27 52.77


Cylinder: flKeys877 6630 16.27 52.77


Cylinder: flKeys877 6630 16.27 52.77

Results from tags2.txt :
File tags2.txt had 17 data items.

Press any key to continue . . .

Because !infile is TRUE until you hit the End Of File. When you read the last line, you still haven't hit EOF so you fo through the loop once more. The first input hits EOF and continues through the loop since you are beyond the check already. The last line read is still in the variables.

How do you make !inFile false so that it will not loop through/

I got my code working

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

using namespace std;
// function prototypes
void  tagReader ( ifstream& inFile, int&count);
void getEmployee ( string& who, double& rate, int &hours );
double paycheck (int hours, double rate);
void repeater ( string word, int num);
bool valid (int val);
void header ();


//=========================================================
int main() 
{    
    int count = 0; 
    ifstream inFile;
    string cylinder, serial;
    double num1, num2;
    double counter;
   
    //----------------------------------------
    // Call function tagReader with tags1.txt
    inFile.open("tags1.txt");
    
    if (!inFile) 
    {
        cout << "\n\nError! - Invalid file name!  (1)\n\n";
        system ("pause");
        return 1;
    }
         inFile >> cylinder;
         while (!inFile.eof()) 
         {
    
         inFile>> serial;
         inFile >> num1;
         inFile >> num2;
         cout << fixed << showpoint << setprecision(2);
         cout <<"Cylinder: " << cylinder << " " << serial << " " << num1 << " " << num2 << endl << endl;   
         count = 1;
         counter++;
         while (!inFile.eof())
         {     inFile >> cylinder ;
               inFile >> serial;
               inFile >> num1;
               inFile >> num2;
               cout << "\nCylinder: " << cylinder << " " << serial << " " << num1 << " " << num2 << endl << endl; 
               count++;        
         }
       }

    cout <<"Results from tags1.txt :\n";
    tagReader ( inFile, count);    
    cout << "File tags1.txt had " << count << " data items. \n\n";
    inFile.close ();
    
    //----------------------------------------
    // Call function tagReader with tags2.txt
    inFile.clear (); 
    inFile.open("tags2.txt");
    if (!inFile) 
         {
          cout << "\n\nERROR!  Invalid file name! (2)\n\n";
          system ("pause");
          return 1;
         }   
         
         inFile >> cylinder;
         while (!inFile.eof()) 
         {
    
         inFile>> serial;
         inFile >> num1;
         inFile >> num2;
         cout << fixed << showpoint << setprecision(2);
         cout <<"Cylinder: " << cylinder << " " << serial << " " << num1 << " " << num2 << endl << endl;   
         count = 1;
         counter++;
         while (!inFile.eof())
         {     inFile >> cylinder ;
               inFile >> serial;
               inFile >> num1;
               inFile >> num2;
               cout << "\nCylinder: " << cylinder << " " << serial << " " << num1 << " " << num2 << endl << endl; 
               count++;        
         }
       }

    cout <<"Results from tags2.txt :\n";
    tagReader ( inFile, count);    
    cout << "File tags2.txt had " << count << " data items. \n\n";
    inFile.close ();
    
   system("pause");
   return 0;
}
//**********************************************************************
//Function Name: GetLength
// Prompts the user to input the lenth of a rectangular room
// Returns the int value input by the user
//**********************************************************************
void tagReader (ifstream& inFile, int&count)
{    

}

Good try -- it's the right idea. Go back to your first code and:

Read the first line as you just did outside the loop.
Inside the loop process all the data read.
Read the next line as the last thing in the loop.

I got the code to work, but it gives me the wrong number of data sets in the output. How would I fix this?

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

using namespace std;
// function prototypes
void  tagReader ( ifstream& inFile, int&count);
void getEmployee ( string& who, double& rate, int &hours );
double paycheck (int hours, double rate);
void repeater ( string word, int num);
bool valid (int val);
void header ();


//=========================================================
int main() 
{    
    int count = 0; 
    ifstream inFile;
    string cylinder, serial;
    double num1, num2;
    double counter;
   
    //----------------------------------------
    // Call function tagReader with tags1.txt
    inFile.open("tags1.txt");
    
    if (!inFile) 
    {
        cout << "\n\nError! - Invalid file name!  (1)\n\n";
        system ("pause");
        return 1;
    }
      
    cout <<"Results from tags1.txt :\n";
    tagReader ( inFile, count);    
    cout << "File tags1.txt had " << count << " data items. \n\n";
    inFile.close ();
    
    //----------------------------------------
    // Call function tagReader with tags2.txt
    inFile.clear (); 
    inFile.open("tags2.txt");
    if (!inFile) 
         {
          cout << "\n\nERROR!  Invalid file name! (2)\n\n";
          system ("pause");
          return 1;
         }   

    cout <<"Results from tags2.txt :\n";
    tagReader ( inFile, count);    
    cout << "File tags2.txt had " << count << " data items. \n\n";
    inFile.close ();
    
   system("pause");
   return 0;
}
//**********************************************************************
//Function Name: GetLength
// Prompts the user to input the lenth of a rectangular room
// Returns the int value input by the user
//**********************************************************************
void tagReader (ifstream& inFile, int&count)
{    
     string cylinder, serial;
     double num1, num2;
     inFile >> cylinder;
         while (!inFile.eof()) 
         {
    
         inFile>> serial;
         inFile >> num1;
         inFile >> num2;
         cout << fixed << showpoint << setprecision(2);
         cout <<"Cylinder: " << cylinder << " " << serial << " " << num1 << " " << num2 << endl << endl;   
         count = 1;
         count++;
         while (!inFile.eof())
         {     inFile >> cylinder ;
               inFile >> serial;
               inFile >> num1;
               inFile >> num2;
               cout << "\nCylinder: " << cylinder << " " << serial << " " << num1 << " " << num2 << endl << endl; 
               count++; 
                      
         }
     } 
}

**Output:

Results from tags1.txt :
Cylinder: bsharkdata 1782 12.00 82.90


Cylinder: ftmyersfeb 7150 11.00 32.89


Cylinder: sandiego99 8800 15.20 52.76


Cylinder: turtles666 2180 11.00 14.09


Cylinder: turtles765 4223 11.90 17.10


Cylinder: FredsFish 4145 11.60 42.22


Cylinder: GreatBaReef 8673 10.00 11.40


Cylinder: VaBch77June 1111 13.50 44.00

File tags1.txt had 9 data items. There is supposed to be 8

Results from tags2.txt :
Cylinder: turtles686 2110 14.88 65.19


Cylinder: turtles995 1123 61.60 77.17


Cylinder: UKFishData 4145 99.04 22.98


Cylinder: Gr8BaReef2 8673 19.71 76.12


Cylinder: VaBch78876 1111 69.11 51.10


Cylinder: shark9987 1782 83.40 89.11


Cylinder: florida331 3130 14.01 52.89


Cylinder: florida876 6600 15.20 52.76


Cylinder: flKeys331 4410 64.66 56.22


Cylinder: flKeys877 6630 16.27 52.77

File tags2.txt had 11 data items. There is supposed to be 10.

Press any key to continue . . .

Now your code doesn't make sense. With your two loops you just complicated the issue.

Get rid of the second loop which is redundant.
Read all the values before the loop.
Read all the values at the end of the loop.

And by adding .eof() you now have the problem that the loop doesn't exit if a different error occurs.

I need the second loop in their to stop the first one.

Really? That makes no sense.

yup, I complied it in Dev C++. Its weird, if I take out the second loop it will run forever, but the second loop will stop the first one.

yup, I complied it in Dev C++. Its weird, if I take out the second loop it will run forever, but the second loop will stop the first one.

Yup... That's because you completely ignore the last help I gave you. I even told you the loop would never end. But did you believe me? Noooooooooooooooooo :icon_wink:

yea, i ignored your help, because you did not help me with my code at all, and I did not know what I needed to take out. All I read was advice, NOT HELP. SO I did believe you, you just did not help me. This is a program for my homework assignment and I have been working on it for nearly 8 hours. Could you please just show me in my code what I need to take out. PLEASE.

yea, i ignored your help, because you did not help me with my code at all, and I did not know what I needed to take out. All I read was advice, NOT HELP. SO I did believe you, you just did not help me.

He is trying to help you out. By making you think. You are doing a good job by attempting the code and working around his advice. Keep at it.

This is a program for my homework assignment and I have been working on it for nearly 8 hours. Could you please just show me in my code what I need to take out. PLEASE.

Showing the answer is just doing your homework. Try to take the 'advice' (which is help) that WaltP is giving you and work with it. Just "finally giving you the answer" will not "help" you or your grade in the long run.

Funny too, I just replied in your newest post prior to reading this one and I did the same attempt WaltP did. Please read our suggestions as they are a direct answer to your question in non-syntax form.

Alright, I got it to work with only one loop, but can you tell me why in my output tags2 is reading 18 data sets when it is only supposed read 10. How do I fix this?

Output:

Results from tags1.txt :

Cylinder: bsharkdata 1782 12 82.9


Cylinder: ftmyersfeb 7150 11 32.89


Cylinder: sandiego99 8800 15.2 52.76


Cylinder: turtles666 2180 11 14.09


Cylinder: turtles765 4223 11.9 17.1


Cylinder: FredsFish 4145 11.6 42.22


Cylinder: GreatBaReef 8673 10 11.4


Cylinder: VaBch77June 1111 13.5 44

File tags1.txt had 8 data items.

Results from tags2.txt :

Cylinder: turtles686 2110 14.88 65.19


Cylinder: turtles995 1123 61.6 77.17


Cylinder: UKFishData 4145 99.04 22.98


Cylinder: Gr8BaReef2 8673 19.71 76.12


Cylinder: VaBch78876 1111 69.11 51.1


Cylinder: shark9987 1782 83.4 89.11


Cylinder: florida331 3130 14.01 52.89


Cylinder: florida876 6600 15.2 52.76


Cylinder: flKeys331 4410 64.66 56.22


Cylinder: flKeys877 6630 16.27 52.77

File tags2.txt had 18 data items.

Press any key to continue . . .

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

using namespace std;
// function prototypes
void  tagReader ( ifstream& inFile, int&count);
void getEmployee ( string& who, double& rate, int &hours );
double paycheck (int hours, double rate);
void repeater ( string word, int num);
bool valid (int val);
void header ();
//=========================================================
int main() 
{    
    int count = 0; 
    ifstream inFile;
    string cylinder, serial;
    double num1, num2;
    //----------------------------------------
    // Call function tagReader with tags1.txt
    
    inFile.open("tags1.txt");
    
    if (!inFile) 
    {
        cout << "\n\nError! - Invalid file name!  (1)\n\n";
        system ("pause");
        return 1;
    }
    cout <<"Results from tags1.txt :\n";
    tagReader ( inFile, count);    
    cout << "File tags1.txt had " << count << " data items. \n\n";
    inFile.close ();

    //----------------------------------------
    // Call function tagReader with tags2.txt
    inFile.clear (); 
    inFile.open("tags2.txt");
    if (!inFile) 
         {
          cout << "\n\nERROR!  Invalid file name! (2)\n\n";
          system ("pause");
          return 1;
         }   
    cout <<"Results from tags2.txt :\n";
    tagReader ( inFile, count);    
    cout << "File tags2.txt had " << count << " data items. \n\n";
    inFile.close ();
    
    system("pause");
    return 0;
}    

//**********************************************************************
//**********************************************************************
void tagReader (ifstream& inFile, int&count)
{    
     string cylinder, serial;
     double num1, num2;
     while (!inFile.eof())
         {     inFile >> cylinder ;
               inFile >> serial;
               inFile >> num1;
               inFile >> num2;
               cout << "\nCylinder: " << cylinder << " " << serial << " " << num1 << " " << num2 << endl << endl; 
               count++; 
         }         
}

yea, i ignored your help, because you did not help me with my code at all, and I did not know what I needed to take out. All I read was advice, NOT HELP. SO I did believe you, you just did not help me. This is a program for my homework assignment and I have been working on it for nearly 8 hours. Could you please just show me in my code what I need to take out. PLEASE.

Now I understand....

I gave you the answer. I explained exactly what you need to do. All you want is for us to do it for you. Sorry, I graduated in the 70's. I don't do homework today.

Bye...

Now I understand....

I gave you the answer. I explained exactly what you need to do. All you want is for us to do it for you. Sorry, I graduated in the 70's. I don't do homework today.

Bye...

I did it. Alright, I got it to work with only one loop, but can you tell me why in my output tags2 is reading 18 data sets when it is only supposed read 10. How do I fix this?

Output:

Results from tags1.txt :

Cylinder: bsharkdata 1782 12 82.9


Cylinder: ftmyersfeb 7150 11 32.89


Cylinder: sandiego99 8800 15.2 52.76


Cylinder: turtles666 2180 11 14.09


Cylinder: turtles765 4223 11.9 17.1


Cylinder: FredsFish 4145 11.6 42.22


Cylinder: GreatBaReef 8673 10 11.4


Cylinder: VaBch77June 1111 13.5 44

File tags1.txt had 8 data items.

Results from tags2.txt :

Cylinder: turtles686 2110 14.88 65.19


Cylinder: turtles995 1123 61.6 77.17


Cylinder: UKFishData 4145 99.04 22.98


Cylinder: Gr8BaReef2 8673 19.71 76.12


Cylinder: VaBch78876 1111 69.11 51.1


Cylinder: shark9987 1782 83.4 89.11


Cylinder: florida331 3130 14.01 52.89


Cylinder: florida876 6600 15.2 52.76


Cylinder: flKeys331 4410 64.66 56.22


Cylinder: flKeys877 6630 16.27 52.77

File tags2.txt had 18 data items.

Press any key to continue . . .

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

using namespace std;
// function prototypes
void  tagReader ( ifstream& inFile, int&count);
void getEmployee ( string& who, double& rate, int &hours );
double paycheck (int hours, double rate);
void repeater ( string word, int num);
bool valid (int val);
void header ();
//=========================================================
int main() 
{    
    int count = 0; 
    ifstream inFile;
    string cylinder, serial;
    double num1, num2;
    //----------------------------------------
    // Call function tagReader with tags1.txt
    
    inFile.open("tags1.txt");
    
    if (!inFile) 
    {
        cout << "\n\nError! - Invalid file name!  (1)\n\n";
        system ("pause");
        return 1;
    }
    cout <<"Results from tags1.txt :\n";
    tagReader ( inFile, count);    
    cout << "File tags1.txt had " << count << " data items. \n\n";
    inFile.close ();

    //----------------------------------------
    // Call function tagReader with tags2.txt
    inFile.clear (); 
    inFile.open("tags2.txt");
    if (!inFile) 
         {
          cout << "\n\nERROR!  Invalid file name! (2)\n\n";
          system ("pause");
          return 1;
         }   
    cout <<"Results from tags2.txt :\n";
    tagReader ( inFile, count);    
    cout << "File tags2.txt had " << count << " data items. \n\n";
    inFile.close ();
    
    system("pause");
    return 0;
}    

//**********************************************************************
//**********************************************************************
void tagReader (ifstream& inFile, int&count)
{    
     string cylinder, serial;
     double num1, num2;
     while (!inFile.eof())
         {     inFile >> cylinder ;
               inFile >> serial;
               inFile >> num1;
               inFile >> num2;
               cout << "\nCylinder: " << cylinder << " " << serial << " " << num1 << " " << num2 << endl << endl; 
               count++; 
         }         
}

Now I understand....

I gave you the answer. I explained exactly what you need to do. All you want is for us to do it for you. Sorry, I graduated in the 70's. I don't do homework today.

Bye...

And I am sorry for bugging you. I know that I am supposed to do the work on my own. I am a college student taking my first programming class. I have been frustrated with this program all day. I understood your advice clearly though. I appreciate your help and I am sorry if I aggravated you.

I did it. Alright, I got it to work with only one loop, but can you tell me why in my output tags2 is reading 18 data sets when it is only supposed read 10.

Hint: 8 + 10 = 18

Reset the counter between file reads.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
   int counter = 0;
void tagReader(ifstream& inFile, int counter);
string file = "tags1.txt";

int main(){
   ifstream inFile;

   inFile.open(file, ios::in);
   tagReader(inFile, counter);
    file = "tags2.txt";
   inFile.open(file, ios::in); 
   tagReader(inFile, counter);
       system("pause");
       return 0;
}

void tagReader(ifstream& inFile, int counter)
{

    string cylinder;
    string serial;
    int num1 = 0;
    double num2 = 0.0;
    double num3 = 0.0;

    if (inFile.is_open()) {
        while (!inFile.eof()) {
            inFile >> cylinder;
            inFile >> serial;
            inFile >> num1;
            inFile >> num2;
            inFile >> num3;
            cout << cylinder << " " << serial << " " << num1 << " " << num2 << " " << num3 << std::endl;
            counter++;
        }
        inFile.close();
    }
    std::cout << "" << std::endl;
    std::cout << "File had " << file << " " << counter << " data items." << std::endl;
    std::cout << "" << std::endl;
    counter = 0; //reset
}
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.