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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
# include <iostream>
# include <iomanip>
# include <fstream>
# include <conio.h>
# include <ctime>
# include <string.h>
# include <string>
# include <windows.h>
using namespace std;
struct Alarm
{
int alarmCounter; // max 3 alarms
int HourAlarm;
int MinuteAlarm;
int DayAlarm;
int MonthAlarm;
int YearAlarm;
char password[20];
int emptyCounter;
};
const char FILE_PATH[] = "F:\\alarm.txt";
const char FILE_PATH2[] = "F:\\password.txt"; //have password in main file, can move to another file when working
fstream MyFile( FILE_PATH, ios :: binary | ios :: in | ios :: out );
fstream MyFile2( FILE_PATH2, ios :: binary | ios :: in | ios :: out );
void createFile();
void displayMenu();
void switchFunction();
void addAlarm();
void changeAlarm();
void displayAlarms();
int checkAlarmCounter();
int checkPassword( char[] );
void createEmptyPassword();
void startClock();
void pause( int );
int main()
{
char password[20] = {"abc"}; //if month is this then password is diff.
Alarm temp;
system("COLOR 0A");
system("CLS");
//PlaySound("D:\\Wide_Awake.wav",NULL,SND_FILENAME|SND_LOOP|SND_ASYNC);
createFile();
createEmptyPassword();
MyFile.open( FILE_PATH, ios :: binary | ios :: in | ios :: out );
MyFile.clear();
MyFile.seekg( 0, ios :: beg );
MyFile.read( ( char* ) &temp, sizeof( Alarm ) );
int checkedPass = 0;
checkedPass = checkPassword( temp.password );
if( checkedPass == 1 )
{
while( strcmp( temp.password, password ) == 0 )
{
switchFunction();
}
}
if( checkedPass == 4 )
{
switchFunction();
}
if( checkedPass == 2 )
{
cout << "enter password to continue: ";
cin >> temp.password;
while( strcmp ( temp.password, password ) != 0 )
{
cout << "error: wrong password\n"
<< "enter new password: ";
cin >> temp.password;
}
}
else
if( checkedPass == 3 )
{
cout << "\n\aError: with password. \n\a";
}
system("CLS");
cout << "GoodBye!";
getch();
MyFile2.close();
MyFile.close();
return 0;
}
//-------------------------------------------------------------------------
void createFile()
{
if ( ! MyFile )
{
fstream MyFile( FILE_PATH, ios :: binary | ios :: out );
}
MyFile.close();
if ( ! MyFile2 )
{
fstream MyFile2( FILE_PATH2, ios :: binary | ios :: out );
}
MyFile2.close();
}
//--------------------------------------------------------
int checkPassword( char inputPassword[] )
{
char password[20] = {"abc"}; // if month is this then password is diff.
Alarm temp;
MyFile.open( FILE_PATH, ios :: binary | ios :: in );
MyFile.clear();
MyFile.seekg( 0, ios :: beg );
MyFile.read( ( char* ) &temp, sizeof( Alarm ) );
int returnCorrect = 1;
int returnWrong = 2;
int error = 3;
int continueWithProgram = 4;
while( !MyFile.eof() )
{
if( strcmp( temp.password, "EMPTY" )== 0 )
{
cout << "Enter new password to save to file ";
cin >> temp.password;
while( strcmp( temp.password, password ) != 0 )
{
cout << "error: incorrect password attempt to save to file.\n"
<< "please try again. ";
cin >> temp.password;
}
MyFile.close();
temp.emptyCounter = 1;
MyFile.open( FILE_PATH, ios :: binary | ios :: in | ios :: out );
MyFile.clear();
MyFile.write( ( const char* ) &temp, sizeof( Alarm ) );
MyFile.close();
return continueWithProgram;
}
if( strcmp( temp.password, password )== 0 )
{
cout << "\n\ncorrect password in file.\n\n" ;
return returnCorrect;
}
else
if( strcmp( temp.password, password )!= 0 )
{
cout << "\n\nwrong password in file\n\n";
return returnWrong;
}
MyFile.read( ( char* ) &temp, sizeof( Alarm ) );
}
cout << "Error: courrupt files, delete them. "; getch();
return error;
}
//--------------------------------------------------------------------
void displayMenu()
{
cout << "do you want to add a new alarm time or change one?" << endl
<< "\tpress 'd' to display alarms " << endl
<< "\tpress 'a' to add " << endl
<< "\tpress 'c' to change " << endl
<< "\tpress 's' to start clock" << endl << endl;
}
//-------------------------------------------------------------
void switchFunction()
{
char choice;
displayMenu();
choice = getch();
while( choice != 27 )
{
switch ( choice )
{
case 'a':
case 'A':
addAlarm();
break;
case 'c':
case 'C':
changeAlarm();
break;
case 'd':
case 'D':
displayAlarms();
break;
case 's':
case 'S':
startClock();
break;
default:
cout << "error:";
}
displayMenu();
choice = getch();
}
}
//----------------------------------------------------------
void addAlarm()
{
Alarm temp;
MyFile.open( FILE_PATH, ios :: binary | ios :: in | ios :: out );
MyFile.clear();
MyFile.seekg( 0, ios :: beg );
MyFile.read( ( char* ) &temp, sizeof( Alarm ) );
int alarmCount = 0;
alarmCount = checkAlarmCounter();
cout << "\n\n" << alarmCount << "\n\n";
if( alarmCount == 2 )
{
system("CLS");
cout << "\aError: with alarm count\n";
}
while( !MyFile.eof() )
{
if( temp.alarmCounter < 4 )
{
if( temp.alarmCounter == 0 )
{
MyFile.close();
cout << "enter first alarm hour: ";
cin >> temp.HourAlarm;
while( temp.HourAlarm > 24 || temp.HourAlarm < 0 )
{
cout << "Error: enter hours within range 0-24 \n\n"
<< "enter first alarm hour: ";
cin >> temp.HourAlarm;
}
cout << "enter first alarm minute: ";
cin >> temp.MinuteAlarm;
while( temp.MinuteAlarm > 59 || temp.MinuteAlarm < 0 )
{
cout << "Error: enter minutes within range 0-59 "
<< "enter first alarm minute: ";
cin >> temp.MinuteAlarm;
}
temp.alarmCounter++;
cout << "\nAlarm Count is: " << temp.alarmCounter << "\n" ;
MyFile.open( FILE_PATH, ios :: binary | ios :: in | ios :: out );
MyFile.seekg( ( 1 - 1 ) * sizeof( Alarm ) );
MyFile.clear();
MyFile.write( ( const char* ) &temp, sizeof( Alarm ) );
MyFile.close();
}
if( temp.alarmCounter <= 2 || temp.alarmCounter != 1 || temp.alarmCounter != 3)
{
MyFile.close();
cout << "enter second alarm hour: ";
cin >> temp.HourAlarm;
while( temp.HourAlarm > 24 || temp.HourAlarm < 0 )
{
cout << "Error: enter hours within range 0-24 \n\n"
<< "enter second alarm hour: ";
cin >> temp.HourAlarm;
}
cout << "enter second alarm minute: ";
cin >> temp.MinuteAlarm;
while( temp.MinuteAlarm > 59 || temp.MinuteAlarm < 0 )
{
cout << "Error: enter minutes within range 0-59 "
<< "enter second alarm minute: ";
cin >> temp.MinuteAlarm;
}
temp.alarmCounter++;
cout << "\nAlarm Count is: " << temp.alarmCounter << "\n" ;
MyFile.open( FILE_PATH, ios :: binary | ios :: app );
MyFile.clear();
MyFile.write( ( const char* ) &temp, sizeof( Alarm ) );
MyFile.close();
}
if( temp.alarmCounter <= 3 || temp.alarmCounter != 1 || temp.alarmCounter != 2 )
{
MyFile.close();
cout << "enter third alarm hour: ";
cin >> temp.HourAlarm;
while( temp.HourAlarm > 24 || temp.HourAlarm < 0 )
{
cout << "Error: enter hours within range 0-24 \n\n"
<< "enter third alarm hour: ";
cin >> temp.HourAlarm;
}
cout << "enter third alarm minute: ";
cin >> temp.MinuteAlarm;
while( temp.MinuteAlarm > 59 || temp.MinuteAlarm < 0 )
{
cout << "Error: enter minutes within range 0-59 "
<< "enter third alarm minute: ";
cin >> temp.MinuteAlarm;
}
temp.alarmCounter++;
cout << "\nAlarm Count is: " << temp.alarmCounter << "\n" ;
MyFile.open( FILE_PATH, ios :: binary | ios :: app );
MyFile.clear();
MyFile.write( ( const char* ) &temp, sizeof( Alarm ) );
MyFile.close();
}
}
MyFile.read( ( char* ) &temp, sizeof( Alarm ) );
}
MyFile.close();
}
//---------------------
//next slide.
|