1

Random characters or values get inserted into the file while finding the total in the void report::getdata() function.

Data is read into character arrays as they can be inserted into the buffer.

I tried converting m1,m2,m3,m4,m5 into integers and and converting them back to characters so they can be copied into the buffer and inserted into the file. Can someone please tell me what I am doing wrong.

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdlib.h>
#include<stdio.h>
#define SIZE 55
char buffer[SIZE + 1];
class report
{
public:
    int a, b, c, d, e, s, avg;
    char rollno[100];
    char name[100];
    char m1[10];
    char m2[10];
    char m3[10];
    char m4[10];
    char m5[10];
    char grade[10];
    char average;
    char total[100];
    void pack();
    void unpack();
    void getdata();
    void putdata();
    void insert();
    void create();
    void sort();
    void search();
    void update();
    void display();
    void failures();
    void modify(char *key);
    void search(char *key);

};

void report::getdata()
{

    cout << "ENTER THE ROLL NO OF THE STUDENT:\t";
    cin >> rollno;
    cout << "ENTER THE NAME OF THE STUDENT:\t";
    cin >> name;
    cout << "ENTER THE MARKS OF THE FOLLOWING SUBJECTS OUT OF 100\n";
    cout << "OPERATION RESEARCH:\t";
    cin >> m1[0];
    cout << "CRYPTOGRAPHY:\t";
    cin >> m2[0];
    cout << "OPERATING SYSTEMS:\t";
    cin >> m3[0];
    cout << "FILE STRUCTURE:\t";
    cin >> m4[0];
    cout << "SOFTWARE TESTING:\t";
    cin >> m5[0];
    a = m1[0] - '0';
    b = m2[0] - '0';
    c = m3[0] - '0';
    d = m4[0] - '0';
    e = m5[0] - '0';
    s = a + b + c + d + e;
    total[0] = s + '0';

}

void report::putdata()
{
    cout << rollno << "\t" << name << "\t" << m1 << "\t" << m2 << "\t" << m3
            << "\t" << m4 << "\t" << m5 << "\t" << total << endl;

}
void report::pack()
{
    strcpy(buffer, rollno);
    strcat(buffer, "|");
    strcat(buffer, name);
    strcat(buffer, "|");
    strcat(buffer, m1);
    strcat(buffer, "|");
    strcat(buffer, m2);
    strcat(buffer, "|");
    strcat(buffer, m3);
    strcat(buffer, "|");
    strcat(buffer, m4);
    strcat(buffer, "|");
    strcat(buffer, m5);
    strcat(buffer, "|");
    strcat(buffer, total);
    strcat(buffer, "|");
    strcat(buffer, "\n");
}
void report::unpack()
{
    char *p;
    p = strtok(buffer, "|");
    strcpy(rollno, p);
    p = strtok(NULL, "|");
    strcpy(name, p);
    p = strtok(NULL, "|");
    strcpy(m1, p);
    p = strtok(NULL, "|");
    strcpy(m2, p);
    p = strtok(NULL, "| ");
    strcpy(m3, p);
    p = strtok(NULL, "|");
    strcpy(m4, p);
    p = strtok(NULL, "|");
    strcpy(m5, p);
    p = strtok(NULL, "|");
    strcpy(total, p);

}

void report::insert()
{
    getdata();
    pack();
    ofstream fout("test.txt", ios::app);
    fout << buffer;
    fout.close();
}
void report::display()
{
    ifstream fin("test.txt");
    while (!fin.eof())
    {
        fin.getline(buffer, SIZE + 1, '\n');
        if (fin.fail())
            break;
        unpack();
        putdata();
    }
    fin.close();
}
void report::search(char *key)
{
    ifstream fin("test.txt");
    int count = 0;
    while (!fin.eof())
    {
        fin.getline(buffer, SIZE + 1, '\n');
        if (fin.fail())
            break;
        unpack();
        if (strcmp(rollno, key) == 0)
        {
            putdata();
            count++;
        }
    }
    cout << "TOTAL RECORDS FOUND:" << count << endl;
    fin.close();
}
void report::modify(char *key)
{
    ifstream fin("test.txt");
    ofstream fout("temp.txt");
    int count = 0;
    while (!fin.eof())
    {
        fin.getline(buffer, SIZE + 1, '\n');
        if (fin.fail())
            break;
        unpack();
        if (strcmp(rollno, key) == 0)
        {
            getdata();
            count++;
        }
        pack();
        fout << buffer;
    }
    if (count == 0)
        cout << "ROLL NO NOT FOUND!!" << endl;
    else
        cout << "MODIFIED!!" << endl;
    fin.close();
    fout.close();
    remove("test.txt");
    rename("temp.txt", "test.txt");
}

int main()
{
    int choice;
    int n;
    report r;
    char key[20];
    clrscr();

    while (1)
    {

        cout << "--------------------\n" << endl;
        cout << "1.MAKE REPORT CARD\n" << "2.UPDATE DETAILS\n"
                << "3.SORT NAMES\n" << "4.SEARCH A NAME\n" << "5.UPDATE GRADE\n"
                << "6.VIEW REPORT CARD\n" << "7.LIST OF FAILURES\n" << "8.EXIT"
                << endl;
        cout << "\n--------------------\n\n" << endl;
        cout << "ENTER YOUR CHOICE:\t" << endl;
        cin >> choice;

        switch (choice)
        {
            case 1:
                r.insert();

                cout << "VALUES INSERTED\n" << endl;
                break;

            case 2:
                cout << "ENTER THE ROLL NO TO MODIFY:\t" << endl;
                cin >> key;
                r.modify(key);
                break;

            case 3:
                cout << "example 3" << endl;
                break;

            case 4:
                cout << "ENTER THE ROLL NO:\t" << endl;
                cin >> key;
                r.search(key);

                break;

            case 5:
                cout << "example 5" << endl;
                break;

            case 6:
                cout << "REPORT CARD DETAILS\n" << endl;
                r.display();
                break;

            case 7:
                cout << "example 7" << endl;
                break;

            case 8:
                exit(0);
                break;
            default:
                return 0;
        }
    }
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • You really should get into the habit of always initializing all your variables. – Jesper Juhl May 30 '20 at 15:36
  • can you please tell me where did i go wrong – Rathan D Rao May 30 '20 at 15:37
  • If you mean to convert `m1` to `m5` from strings with numerical digits to integer values then just subtracting '0' from the first array element will not work. You'll need to use `std::stoi` (C++11), or the old `atoi` although it is not a safe function. It is not clear what you intend with `cin >> m1[0]` for example. – DNT May 30 '20 at 15:49
  • the thing is that i use TURBO C++ Compiler – Rathan D Rao May 30 '20 at 16:34
  • 1
    That is unfortunate. Using a 30-year-old tool chain makes it very hard to get good help. C++ and Turbo C++ are not the same thing, so a lot of the advice you will get flat-out doesn't apply and few people can assist you fully due to unfamiliarity with the dialect. Your first duty to yourself is to pass whatever course demands you use Turbo C++, but I strongly recommend teaching yourself modern C++ so that you have a smoother transition into industry at the end. – user4581301 May 30 '20 at 17:17
  • Unrelated: `while (!fin.eof())` will not work. Explanation of why and how to fix it here: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 May 30 '20 at 17:23
  • Suggestion: back up your program and eliminate everything that is not needed to reproduce the problem you are having like user input and the menu system. The less room you give a bug to hide in the easier it is to find. Plus if you construct a program that does nothing but cause the bug and requires a minimum of user input, you can easily run this test over and over until you find the bug. Use [mcve] for inspiration. – user4581301 May 30 '20 at 17:30
  • 3
    Suggestion: Turbo C++ came with Turbo Debugger, one of the best debuggers available in its day and it still holds up well 30 years later. Use Turbo Debugger to place breakpoints near where things appear to go wrong and then step through the code to watch what happened. When you see something you didn't expect happen, odds are good you just found a bug. Turbo Debugger can also allow you to step backwards top see how you got somewhere after it happens. The debugger is one of the best programmer productivity tools out there, and using it will put you far ahead of the rest of your class. – user4581301 May 30 '20 at 17:34
  • Consider switching to a modern C++ compiler such as [GCC](http://gcc.gnu.org/) or [Clang](http://clang.llvm.org/) and use the [GDB](https://www.gnu.org/software/gdb/) debugger with it. Then refer to [this C++ reference](https://en.cppreference.com/w/cpp). You have all that on many Linux distributions (e.g. [Debian](http://deblan.org/) or [Ubuntu](http://ubuntu.com/)...). You could be interested by [JSON](http://json.org/) or [Qt](http://qt.io/) – Basile Starynkevitch Jun 24 '20 at 09:27

1 Answers1

1

The best method to solve your problem would be to use binary files to store your data.

First, remove the pack() and unpack() methods. We won't need those.

void report::insert()
{
    r.getdata();
    ofstream fout("test.dat", ios::app|ios::binary);
    fout.write((char*)&r,sizeof(r));
    fout.close();
}

void report::search(char *key)
{
    ifstream fin("test.dat",ios::binary);
    int count = 0;
    while (!fin.eof())
    {
        fin.read((char*)&r,sizeof(r));        
        if (fin.fail())
            break;
        if (strcmp(r.rollno, key) == 0)
        {
            r.putdata();
            count++;
        }
    }
    cout << "TOTAL RECORDS FOUND:" << count << endl;
    fin.close();
}

void report::display()
{
    ifstream fin("test.dat",ios::binary);
    while (!fin.eof())
    {
        fin.read((char*)&r,sizeof(r));
        if (fin.fail())
            break;
        r.putdata();
    }
    fin.close();
}
Kitswas
  • 1,134
  • 1
  • 13
  • 30