cout << "Enter Student ID (999 to end): ";  cin >> searchId;
        inFile.open(file + ".txt");
        inFile.ignore(80, '\n');
        while (searchId != 999)
        {
            for (int i = 0; i <= count; i++)
            {
                inFile >> ID >> lastName >> firstName >> q1 >> q2 >> q3 >> q4 >> final;
                if (searchId == ID)
                {
                    avgGrd(q1, q2, q3, q4, final, average, grade);
                    cout << "  " << left << setw(12) << lastName << "  " << setw(12) << firstName << ' ' 
                        << setw(3) << average << ' ' << setw(2) << grade << endl;
                    cout << "Enter Student ID (999 to end): ";  cin >> searchId;
                }
            }
        }cout << "Enter any character: ";  cin >> end;

This is what I have so far. And this works up to about 3 iterations and then stops. I'm not sure why. Also, everytime I try to make it so it will output "Record not found!" for an invalid input it does that for every input thereafter. Please Help!

Recommended Answers

line 2: how is variable file declared? If it is std::string, you can't do it that way.

file += ".txt" 
infile.open(file.c_str())

if file is declared as a character array do this:

strcat(file,".txt");
infile.open(file);

Your program doesn't need that for next loop. Just …

Jump to Post

The entire thing works.

Not possible. Line 27 is wrong, as I explained in my last post. open() doesn't take a std::string as the first argument, it takes only char*.

and line 28 should be if( !infile.is_open()) instead of if( infile.fail())

Jump to Post

I gave you the code the read the file, you just chose to ignore it.

Jump to Post

All 11 Replies

line 2: how is variable file declared? If it is std::string, you can't do it that way.

file += ".txt" 
infile.open(file.c_str())

if file is declared as a character array do this:

strcat(file,".txt");
infile.open(file);

Your program doesn't need that for next loop. Just a while loop will do ok

while(true)
{
    cout << "Enter Student ID (999 to end): ";  
        cin >> searchId;
    if( searchId == 999 )
       break;
    while( inFile >> ID >> lastName >> firstName >> q1 >> q2 >> q3 >> q4 >> final )
    {
       // do something with the data
      if (searchId == ID)
      {
          avgGrd(q1, q2, q3, q4, final, average, grade);
          cout << "  " << left << setw(12) << lastName << "  " << setw(12) << firstName << ' ' 
             << setw(3) << average << ' ' << setw(2) << grade << endl;
          break;
      }
  }
 }
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;
// Function prototypes
void avgGrd(int, int, int, int, int, double&, string&);
int findLow(int, int, int, int);
int findHigh(int, int, int, int);
int qfAvg(double, int, double&);

void main()
{
    // Variable declarations
    ifstream inFile;
    string lastName, firstName, grade, file;
    char end;
    int ID, q1, q2, q3, q4, final, count = 0, searchId;
    double sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0, sumFinal = 0, sumAvg = 0;
    int q1Count = 0, q2Count = 0, q3Count = 0, q4Count = 0, finalCount = 0, avgCount = 0;
    double avg;
    double average;


    cout << "Enter file name: "; cin >> file;
    inFile.open(file + ".txt");
    if (!inFile.fail())
    {
        cout << fixed << setprecision(1);
        cout << endl << setw(53) << "Fall 2012 Student Grade Report:" << endl << endl;
        cout << "No.    ID   Last Name    First Name  Quiz1 Quiz2 Quiz3 Quiz4 Final  Avg   Grd" << endl;
        cout << "------------------------------------------------------------------------------" << endl;
        inFile.ignore(80, '\n');
        inFile >> ID;
        while (!inFile.eof())
        {

            count++;
            inFile >> lastName >> firstName >> q1 >> q2 >> q3 >> q4 >> final;
            sum1 += q1; sum2 += q2; sum3 += q3; sum4 += q4; sumFinal += final; 
            avgGrd(q1, q2, q3, q4, final, average, grade);
            sumAvg += average;
            cout << right << setw(3) << count << " " << setw(6) << ID << "  " << left 
                << setw(12) << lastName << " " << left << setw(12) << firstName << " ";
                if (q1 > 0)
                {
                    cout <<right << setw(3) << q1;
                    q1Count++;
                }
                else
                    cout << "   ";
                if (q2 > 0)
                {
                    cout << right << setw(6) << q2;
                    q2Count++;
                }
                else
                    cout << "      ";
                if (q3 > 0)
                {
                    cout << right << setw(6) << q3;
                    q3Count++;
                }
                else
                    cout << "      ";
                if (q4 > 0)
                {
                    cout << right << setw(6) << q4;
                    q4Count++;
                }
                else
                    cout << "      ";
            cout << right << setw(5) << final << right << setw(8) << average << "   " << left << setw(7) << grade << endl;
            inFile >> ID;
        }
        inFile.close();
        cout << "                                      ----------------------------------------" << endl;
        cout << "                                       ";
        qfAvg(sum1, q1Count, avg);
        cout << setw(6) << avg;
        qfAvg(sum2, q2Count, avg);
        cout << setw(6) << avg; 
        qfAvg(sum3, q3Count, avg);
        cout << setw(6) << avg;
        qfAvg(sum4, q4Count, avg);
        cout << setw(6) << avg;
        qfAvg(sumFinal, count, avg);
        cout << setw(5) << avg;
        qfAvg(sumAvg, count, avg);
        cout << setw(5) << avg << endl;

        cout << "Enter Student ID (999 to end): ";  cin >> searchId;
        inFile.open(file + ".txt");
        inFile.ignore(80, '\n');
        while (searchId != 999)
        {
            for (int i = 0; i <= count; i++)
            {
                inFile >> ID >> lastName >> firstName >> q1 >> q2 >> q3 >> q4 >> final;
                if (searchId == ID)
                {
                    avgGrd(q1, q2, q3, q4, final, average, grade);
                    cout << "  " << left << setw(12) << lastName << "  " << setw(12) << firstName << ' ' 
                        << setw(3) << average << ' ' << setw(2) << grade << endl;   
                    cout << "Enter Student ID (999 to end): ";  cin >> searchId;      
                }
            }cout << "Record not found!" << endl;
        }cout << "Enter any character: ";  cin >> end;
    }
    else
        cout << endl << "File Open Failed!" << endl;
}

// Function definitions
void avgGrd(int q1, int q2, int q3, int q4, int final, double& average, string& grade)
{   
    int low = findLow(q1, q2, q3, q4);
    int high = findHigh(q1, q2, q3, q4);
    average = ((q1 + q2 + q3 + q4 + final + final) - low + high) / 6.;
    if (average >= 96.66)
        grade = "A+";
    else if (average >= 93.33)
        grade = 'A';
    else if (average >= 90)
        grade = "A-";
    else if (average >= 86.66)
        grade = "B+";
    else if (average >= 83.33)
        grade = 'B';
    else if (average >= 80)
        grade = "B-";
    else if (average >= 76.66)
        grade = "C+";
    else if (average >= 73.33)
        grade = 'C';
    else if (average >= 70)
        grade = "C-";
    else if (average >= 66.66)
        grade = "D+";
    else if (average >= 63.33)
        grade = 'D';
    else if (average >= 60)
        grade = "D-";
    else
        grade = 'F';

}
int findLow(int q1, int q2, int q3, int q4)
{
    int low = q1;
    if (q2 < low)
        low = q2;
    if (q3 < low)
        low = q3;
    if (q4 < low)
        low = q4;
    return low;
}
int findHigh(int q1, int q2, int q3, int q4)
{
    int high = q1;
    if (q2 > high)
        high = q2;
    if (q3 > high)
        high = q3;
    if (q4 > high)
        high = q4;
    return high;
}
int qfAvg(double sum, int Count, double& avg)
{
    avg = sum / Count;
    return avg;
}

This is the whole code. The entire thing works. Even inputting 999. The only problem I am having is I have to have it cout << "Record not found!" << endl; when the input is not valid. How I have it right here it does it but then I am stuck in an infinite loop of "Record not found!". I don't know how to fix this.

The entire thing works.

Not possible. Line 27 is wrong, as I explained in my last post. open() doesn't take a std::string as the first argument, it takes only char*.

and line 28 should be if( !infile.is_open()) instead of if( infile.fail())

I'm not sure what you are saying is not possible. The program runs. Perfectly actually. I just can't make the last part output properly.

I gave you the code the read the file, you just chose to ignore it.

What I'm saying is I don't understand what you mean. The file reads. Reading the file is not the issue

Not possible. Line 27 is wrong, as I explained in my last post. open() doesn't take a std::string as the first argument, it takes only char*.

Perhaps he's using some old or otherwise nonstandard compiler that's somehow automatically typecasting std::string to const char*?

I'm not sure but I am using Microsoft Visual Studios 2010 and it compiles and reads the file without a problem. I just can't get the second part of the program to work properly.

Not possible. Line 27 is wrong, as I explained in my last post. open() doesn't take a std::string as the first argument, it takes only char*.

This was discussed in this thread. and is possible, though I can't vouch for every compiler accepting a std::string as the file name.

This entire section of code is quite a mess.

cout << "Enter Student ID (999 to end): "; 
cin >> searchId;
inFile.open(file + ".txt");
inFile.ignore(80, '\n');
while (searchId != 999)
{
    for (int i = 0; i <= count; i++)
    {
        inFile >> ID >> lastName >> firstName >> q1 >> q2 >> q3 >> q4 >> final;
        if (searchId == ID)
        {
            avgGrd(q1, q2, q3, q4, final, average, grade);
            cout << " " << left << setw(12) << lastName << " " << setw(12) << firstName << ' '
            << setw(3) << average << ' ' << setw(2) << grade << endl;
            cout << "Enter Student ID (999 to end): "; cin >> searchId;
        }
    }cout << "Record not found!" << endl;
}cout << "Enter any character: "; 

cin >> end;

What constitutes a valid Student ID?
cin >> searchId;
Validate that searchId falls within the acceptable range.

If (searchId == 999) 
    return 0;

Also change void main() to int main()

inFile.open(file + ".txt");
check that you actually opened the file and handle if unsuccessful

Get rid of the following loops.
while (searchId != 999) and for (int i = 0; i <= count; i++)

Use something like:

bool bFound = false;
while (inFile >> ID >> lastName >> firstName >> q1 >> q2 >> q3 >> q4 >> final)
{
    if (searchId == ID)
    {
       bFound = true;
       // do other stuff
    }
}

if (!bFound)
{
    // error ID not found
    // handle error
}

A valid search ID is one that is found within the file. SO you would have to search the entire file to find out. I can actually open the file and handle it successfully. There is a first half to this outputs perfectly fine. The biggest prblem I'm having is that I have to be able to search for the ID's and I can't because it will only allow me to go through the file once and I don't know how to change that. SO I can search for the ID's as long as they are in sequential order and if I skip ahead I can't go back and I don't know how to fix that. I need to be able to search the entire file every time.

after going through the file you have to reset the file stream back to the beginning of the file.

infile.feekp(0);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.