I have seemed to get the first function tagReader to work, but can't get the others to work. The other functions are bolded.

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

    //----------------------------------------
    // 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;
    
     inFile >> cylinder;
         while (!inFile.eof()) 
         {
         inFile>> serial;
         inFile >> num1;
         inFile >> num2;
         cout << fixed << showpoint << setprecision(2);
         cout <<"\nCylinder: " << cylinder << " " << serial << " " << num1 << " " << num2 << endl << endl;   
         count = 0;
         count++;
         while (!inFile.eof())
         {     inFile >> cylinder ;
               inFile >> serial;
               inFile >> num1;
               inFile >> num2;
               cout << "\nCylinder: " << cylinder << " " << serial << " " << num1 << " " << num2 << endl << endl; 
               count++; 
         } 
        }
}
//**********************************************************************
//********************************************************************** 
void getEmployee (string& who, double& rate, int &hours)
{
    string lastName;

     cout << "\nInput the employee's last name: ";
     cin >> lastName;
     cout << "\nInput the number of hours worked: ";
     cin >> hours;
     cout << "\nInput the payrate: $";
     cin >> rate; 
     cout << "\nHello " << lastName << "! You have worked " << hours << " hour(s) at a payrate of $" << rate << " an hour.\n" << endl;
}
//**********************************************************************
//**********************************************************************
double paycheck (int hours, double rate)
{
     double paycheck;
     double overtime;
     double overtime_check;
     cout << "The employee has worked " << hours << " hours." << endl;
    if(hours <=40)
    {
     paycheck = hours * rate;
     cout << "\nThe employee has earned $" << paycheck << " dollar(s)." << endl;  
     }
    else if(hours > 40)
    {
     overtime = (hours - 40) * rate * 1.5;
     overtime_check = paycheck + overtime;
     cout << "\nThe employee has earned $" << paycheck << " dollar(s) of overtime pay." << endl;
     cout << "\nTherefore, the employee has earned $" << paycheck << " dollar(s)." << endl;
     }    
}
//**********************************************************************
//**********************************************************************
void repeater ( string word, int num)
{
    cout << "\nEnter a word: ";
     cin >> word;
     cout << "\nHow many times would you like this word processed? ";
     cin >> num;
     
     cout << "" << endl;
     
     for(int i=0;i<num;i++)
     {
     cout << word << endl;
      }
      cout << "\nThe word " << word << " appeared " << num << " times." << endl << endl;
}
//**********************************************************************
//**********************************************************************
bool valid (int val)
{
     cout << "Enter a value between 75 and 100: ";
     cin >> val;
     
     if(75.0 <= val && 100 >= val)
     {
       cout << "\nTrue" << endl << endl;
      }
      else
     {
       cout << "\nFalse" << endl << endl;
      }
}
//**********************************************************************
//**********************************************************************
void header ()
{
    cout << endl;     
    cout << "Driver Name: Peter Langlands - first 3; Tyler Bazan - second 3" << endl;
    cout << "Navigator Name: Tyler Bazan - first 3; Peter Langlands - second 3" << endl;
    cout << "Date: March 3, 2011" << endl;
    cout << "Lab CRN 26682\n" << endl;       
}  
//-----------------------------------------------------}

Recommended Answers

All 3 Replies

You may want to actively step by step debug this with your IDE. First comment out all functions so none are being called or compiled. Take the first function and debug it until it works properly, then un-comment the next function and debug it until it works properly. Do so until all functions work properly.

Debugging is half the fun of actual writing programs; not staring off into spacing hoping it fixes itself.

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 <<"\nCylinder: " << cylinder << " " << serial << " " << num1 << " " << num2 << endl << endl;   
         count = 0;
         count++;
         while (!inFile.eof())
         {     inFile >> cylinder ;
               inFile >> serial;
               inFile >> num1;
               inFile >> num2;
               cout << "\nCylinder: " << cylinder << " " << serial << " " << num1 << " " << num2 << endl << endl; 
               count++; 
         } 
        }
}

Your function here, doesn't seem right. You are calling a while loop inside of a while loop that appears to be doing the same thing. You may only need the first while loop to do the job you are looking for.

You also have the two statements

count = 0;
         count++;

Every time you loop through, you are resetting your count(er) to zero, then immediately adding.

Overall, you may want to recheck the structure of this function. As mentioned above, once this function is working properly proceed through the rest of your program.

Alright, I took out the other loop, and now it keeps on looping through and does not stop.

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

    //----------------------------------------
    // 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;
    
     inFile >> cylinder;
      
         while (!inFile.eof()) 
         {
         inFile>> serial;
         inFile >> num1;
         inFile >> num2;
         cout << fixed << showpoint << setprecision(2);
         cout <<"\nCylinder: " << cylinder << " " << serial << " " << num1 << " " << num2 << endl << endl;   
         count = 0;
         count++;
         }
         }
        
/*

You may want to actively step by step debug this with your IDE. First comment out all functions so none are being called or compiled. Take the first function and debug it until it works properly, then un-comment the next function and debug it until it works properly. Do so until all functions work properly.

Debugging is half the fun of actual writing programs; not staring off into spacing hoping it fixes itself.

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 <<"\nCylinder: " << cylinder << " " << serial << " " << num1 << " " << num2 << endl << endl;   
         count = 0;
         count++;
         while (!inFile.eof())
         {     inFile >> cylinder ;
               inFile >> serial;
               inFile >> num1;
               inFile >> num2;
               cout << "\nCylinder: " << cylinder << " " << serial << " " << num1 << " " << num2 << endl << endl; 
               count++; 
         } 
        }
}

Your function here, doesn't seem right. You are calling a while loop inside of a while loop that appears to be doing the same thing. You may only need the first while loop to do the job you are looking for.

You also have the two statements

count = 0;
         count++;

Every time you loop through, you are resetting your count(er) to zero, then immediately adding.

Overall, you may want to recheck the structure of this function. As mentioned above, once this function is working properly proceed through the rest of your program.

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