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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
#include <iostream>
#include <cctype>
#include <cstring>
#include <fstream>
using namespace std;
// These are my non Tech functions.
void welcome();
int how_many_in_file();
struct tech_list
{
char *tech_name;
char *tech_desc;
float cost;
char *pro;
char *con;
float rating;
};
class Tech
{
public:
Tech(int howmany, int howmany_filein); // This is my constructor.
~Tech(); // This is my destructor.
Tech file_in(); // This reads in from the file.
void read_out(int howmany); // This reads out to the file.
void input(); // This is my function for user input.
void edit_name(int x); // This is an extension of my search function, for editting the name.
void edit_desc(int x);
void edit_cost(int x);
void edit_pro(int x);
void edit_con(int x);
void edit_rating(int x);
void display(int j); // This is my display all function.
void search(); // This searches for the item name that the user wants to edit.
void display_pro(); // This displays the pro.
void display_con();// This displays the con.
private:
tech_list *object; // A pointer of the type tech_list.
int size; // This is for holding my maximum number of items.
int num_items; // This is a counter for making sure I am inputting only after the read in items.
};
*************************************************************************
#include "class.h"
int main()
{
welcome();
int selection;
int howmany;
char again;
cin >> howmany; cin.ignore();
int howmany_filein = how_many_in_file();
howmany = howmany + howmany_filein;
Tech object(howmany, howmany_filein);
object.file_in();
for(int i = howmany_filein; i < howmany; i++)
{
cout << "We are working with item #: "
<< i + 1 << endl;
object.input();
}
do{
cout << "Please enter 1 to display all, 2 to edit an item, 3 to display the benefit of an item, and 4 to display the con of an item." << endl;
cin >> selection; cin.ignore();
if(selection == 1)
{
for(int j = 0; j < howmany; j++)
{
object.display(j);
}
}
if(selection == 2)
{
object.search();
}
if(selection == 3)
{
object.display_pro();
}
if(selection == 4)
{
object.display_con();
}
cout << "to continue enter y, to end the program enter n." << endl;
cin >> again; cin.ignore();
}while(again == 'y');
object.read_out(howmany);
return 0;
}
**************************************************************************
#include "class.h"
// This is my constructor.
Tech::Tech(int howmany, int howmany_filein)
{
object = new tech_list[howmany]; // This initializes my object pointer of type tech_list (my struct) to point to a new array of type tech_list of size howmany.
// Which is the sum of the number of items read in from our data.txt, and however many items the user indicated they wanted to add.
size = howmany; // I am setting the maximum size, to be used so that I can stop my functions from inputting past my allocated memory.
num_items = howmany_filein; // num_items is a counter, that takes in the number of items read in so it doesn't write over any information we wanted to be saved in our data.txt
}
// This is my deconstructor, this deletes the new objects when we are done with them and have already saved them via external data files.
Tech::~Tech()
{
delete object;
}
// This function reads in the first part of the external data file, an integer that represents the number of items that are saved in our external data file.
int how_many_in_file()
{
ifstream filein;
filein.open("data.txt");
if(filein)
{
int size_temp;
filein >> size_temp;
filein.ignore(100, '\n');
return size_temp;
}
filein.close(); // Closing the file so we can work with it later.
}
// This function is for reading in the data from our external data file.
Tech Tech::file_in()
{
ifstream filein;
filein.open("data.txt");
if(filein) // If we're in the file it executes.
{
int size_temp;
filein >> size_temp;
filein.ignore(100, '\n'); // This is to move past the number at the start, because we have already used that in a seperate function. See function "how_many_in_file"
int i= 0;
char temp[200];
while(filein && !filein.eof())
{
// This reads in from the file into temp, and dynamically allocates our pointers within the struct tech_list and copies the temp into them.
filein.get(temp, 200, '|'); filein.ignore(200, '|');
object[i].tech_name = new char[strlen(temp)+1];
strcpy(object[i].tech_name, temp);
filein.get(temp, 200, '|'); filein.ignore(200, '|');
object[i].tech_desc = new char[strlen(temp)+1];
strcpy(object[i].tech_desc, temp);
filein >> object[i].cost; filein.ignore(100, '|'); // Like our input function, cost and rating are not pointers and do not need to be dynamically allocated.
filein.get(temp, 200, '|'); filein.ignore(200, '|');
object[i].pro = new char[strlen(temp)+1];
strcpy(object[i].pro, temp);
filein.get(temp, 200, '|'); filein.ignore(200, '|');
object[i].con = new char[strlen(temp)+1];
strcpy(object[i].con, temp);
filein >> object[i].rating; filein.ignore(100, '\n');
++i; // As we read from the next part of the file, we increment i to save the next line of information to the next item.
filein.peek(); // This peeks to the next line and makes sure we aren't hitting the end of the file.
}
filein.close(); // Closing the file so we can work with it later.
}
}
// This function writes the items into an external data file "data.txt".
void Tech::read_out(int howmany)
{
ofstream fileout("data.txt", ios::in);
if(!fileout)
{
return; // If we're not accessing the file, return.
}
fileout << howmany << endl;
for(int j = 0; j < howmany; ++j)
{
fileout << object[j].tech_name << "|"; // Write the information of object tech_name at index "j".
fileout << object[j].tech_desc << "|";
fileout << object[j].cost << "|";
fileout << object[j].pro << "|";
fileout << object[j].con << "|";
fileout << object[j].rating << endl; // Ends the line, indicating a new item.
}
fileout.close(); // Closes the file.
}
|