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
|
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class myClass{
public:
myClass (string fname) //constructor
{
filename = fname;
selection = 0;
input = 0;
number = 0.0;
anothernumber = 0.0;
replacement = 0.0;
newfilename = "";
choice = "";
answer = "";
}
string returnNewfile()
{
return newfilename;
}
string returnAnswer()
{
return answer;
}
int returnSelection()
{
return selection;
}
int returnInput()
{
return input;
}
double returnOtherNumber()
{
return anothernumber;
}
double returnNumber()
{
return number;
}
double returnReplace()
{
return replacement;
}
string returnChoice() //choice
{
return choice;
}
void Select()
{
cout << "Please enter 0 for read or 1 for write mode" << endl;
while (!(cin >> selection) || selection < 0)
{
cout << "Value is invalid, try again." << endl;
cin.clear(); //Clear error flag
cin.ignore();
} // end while
if (selection == 0)
{
ifstream filename ("example.txt");
if (filename.is_open())
{
while(getline(filename, line)) //Goes through the file and gets each line individually
{ //and prints out whats in it line by line.
cout << '\n' <<"This is what is in the file: " << line << endl;
}
filename.close();
}
}
else if (selection == 1)
{
fstream filename("example.txt");
if (filename.is_open())
{
cout << "How many numbers do you want to enter? " << endl;
while (!(cin >> input) || input < 0)
{
cout << "Value is invalid, try again." << endl;
cin.clear(); //Clear error flag
cin.ignore();
} // end while
for (int i = 1; i <= input; i++) //This will check how many numbers you want to enter
{ //then will keep asking for the numbers until it reaches what you wanted.
cout << '\n' << "This is what is in the file: " << line << endl;
cout << '\n' << "Hit a to accept value, r to replace, d to delete it, or type x to accept rest of the values. " << endl;
while (!(cin >> choice))
{
cout << "Value is invalid, try again." << endl;
cin.clear(); //Clear error flag
cin.ignore();
} // end while
if (choice == "a" || choice == "A")
{
filename << line << endl;
}
if (choice == "r" || choice == "R")
{
cout << "What number do you want to replace it with? " << endl;
while (!(cin >> number) || number < 0)
{
cout << "Value is invalid, try again." << endl;
cin.clear(); //Clear error flag
cin.ignore();
} // end while
filename << number << " " << line << endl;
cout << "The number has been replaced." << endl;
}
if (choice == "d" || choice == "D")
{
number--;
cout << "Number deleted." << endl;
}
if (choice == "x" || choice == "X")
{
terminate();
}
cout <<'\n' << "Would you like to enter a new number? y or n" << endl;
cin >> answer;
if (answer == "y" || answer == "Y")
{
cout << '\n' << "Please enter the number." << endl;
while (!(cin >> anothernumber) || anothernumber < 0)
{
cout << "Value is invalid, try again." << endl;
cin.clear(); //Clear error flag
cin.ignore();
} // end while
filename << line << " ";
filename << anothernumber << endl;
}
}// end of for loop
cout << "Would you like to save the changes of the file to a new one?" << endl;
cin >> newfilename;
while (newfilename != "yes" || newfilename != "Yes" || newfilename != "YES" )
{
cout << "Value is invalid, try again." << endl;
cin.clear(); //Clear error flag
cin.ignore();
} // end while
if(newfilename == "yes" || newfilename == "Yes" || newfilename == "YES")
{
string thisfilenow;
cout << "Please enter new file name." << endl;
cin >> thisfilenow; //this will save the new data into another file leaving the old file intact.
filename << thisfilenow;
}
// while loop
}// end of filename.open if statement
filename.close();
}// end of else if selectio == 1
}//End of void select
private:
string filename;
string line;
string answer;
string newfilename;
int selection;
double myarray[10][10];
int input;
double number, replacement,anothernumber;
string choice;
};
int main()
{
string fname = "example.txt";
myClass co(fname);
co.Select();
}
|