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