Jan 14, 2015 at 1:41pm UTC
A user will input entries. But when the user wants to clear out all the info, it displays no entry but can be updated if inputted again. I don't know how to update it
here's my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
#include<iostream>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<iomanip>
#include <fstream>
#include<cstring>
using namespace std;
struct Student
{
char sn[15];
char name[100];
char course[50];
int GPA;
};
void AddEntry(Student p);//adds entry
void DisplayEntry();//displays entry
void InitRecord();//remove all the data entry
void line(char , int );
void newLine();
Student person[20];
Student p;
int index = 0;
char choice;
int main()
{
int option;
while (1) {
system("cls" );
system("COLOR D" );
cout << "-------------------XYZ's Computer College" ;
line('-' , 19);
cout << "\n What would you like to do?\n" ;
line('-' , 50);
cout << " [1] Initialized Record \n" ;
cout << " [2] Add Record \n" ;
cout << " [3] Show Record \n" ;
cout << " [4] Exit \n" ;
line('-' , 50);
cout << "Please enter your choice: " ;
cin >> option;
newLine();
char buff[2];
switch (option)
{
case 1:
InitRecord();
break ;
case 2:
system("cls" );
system("COLOR B" );
cout << "Enter Student Number: " ;
cin.getline(p.sn, 15);
cout << "Enter Name: " ;
cin.getline(p.name, 100);
cout << "Enter Course: " ;
cin.getline(p.course, 50);
cout << "Enter GPA: " ;
cin >> p.GPA;
newLine();
AddEntry(p);
break ;
case 3:
DisplayEntry();
break ;
case 4:
exit(1);
default :
cout << "Invalid choice \n\n" ;
system("pause>0" );
}
}
return 0;
}
void InitRecord()
{
system("cls" );
system("COLOR E" );
cout << "WARNING! All records will be deleted. [Y]/[N]" ;
cin >> choice;
if (choice == 'y' || choice == 'Y' )
{
ofstream fout;
fout.open("c:\\activity\\student.txt" , ios_base::trunc);
cout << "\nRecord has been deleted!" ;
fout.close();
}
else if (choice == 'n' || choice == 'N' )
{
cout << "No changes made." ;
}
system("pause>0" );
}
void AddEntry(Student p)
{
person[index] = p;
index++;
}
void DisplayEntry()
{
ofstream fout;
if (ios_base::trunc)
{
system("cls" );
system("COLOR E" );
char ltr;
cout << setw(11) << " Student No." << setw(10)
<< " Name" << setw(10)
<< " Course" << setw(10)
<< " GPA" << setw(10);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
printf("\nRecords Total:0\n" );
system("pause>0" );
fout.close();
}
else if (ios_base::out)
{
fout.open("c:\\activity\\student.txt" );
system("cls" );
system("COLOR E" );
char ltr;
cout << setw(11) << " Student No." << setw(10)
<< " Name" << setw(10)
<< " Course" << setw(10)
<< " GPA" << setw(10);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
int i = 0;
for (; i < index; i++){
cout << endl;
fout << endl;
cout << person[i].sn << setw(20);
fout << person[i].sn << setw(2);
cout << person[i].name << setw(15);
fout << person[i].name << setw(2);
cout << person[i].course << setw(10);
fout << person[i].course << setw(2);;
cout << person[i].GPA << setw(10);
fout << person[i].GPA << setw(2);
}
printf("\nRecords Total:%d\n" , i);
system("pause>0" );
fout.close();
}
}
void line(char ch, int ctr)
{
for (int i = 0; i < ctr; i++) {
cout << ch;
}
cout << endl;
}
void newLine()
{
char s; do {
cin.get(s);
} while (s != '\n' );
}
Last edited on Jan 14, 2015 at 1:42pm UTC
Jan 14, 2015 at 3:15pm UTC
Open the file in binary mode. To read or write the Nth record, seek to position (N-1)*sizeof(Student) first. To append a record, seek to the end and write the new record.
I don't see you reading or writing records to the file at all.
Lines 108 and 128 are wrong. ios_base::trunc and ios_base::out are non-zero constants so the if's will always be true. It looks like you're trying to say "if there are no records then print it, else print the records." Don't do these as different code paths. Just print the records. If there are zero then you'll achieve the same thing.