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
|
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <windows.h>
#include <cstring>
using namespace std;
int main(int argc, char *argv[])
{
//Declare integers for configuration value storing, and random number
int a,b,cfg_1,cfg_2, cfg_3, cfg_4, cfg_5;
/*
cfg_1=max_num (val)
cfg_2=min_num (val)
*/
//Read the config.txt, and get data from it
char data[80];
//Declare strings for raw data from text file
string cfg1, cfg2, cfg3, cfg4, cfg5;
//Open the file
ifstream infile;
infile.open("config.txt");
//Get data line by line, and then erase some letters from string
infile >> data;
cfg1 = data;
cfg1.erase (0,8);
infile >> data;
cfg2 = data;
cfg2.erase (0,8);
infile >> data;
cfg3 = data;
cfg3.erase (0,9);
infile >> data;
cfg4 = data;
cfg4.erase (0,15);
infile >> data;
cfg5 = data;
cfg5.erase (0,13);
infile.close();
//Convert strings to integers
istringstream myStream(cfg1);
myStream>>cfg_1;
istringstream myStream1(cfg2);
myStream1>>cfg_2;
//We dont need this really, instead:
//strcpy(name, cfg3.c_str());
char * name;
name = new char[cfg3.length() + 1];
strcpy(name, cfg3.c_str());
//istringstream myStream2(cfg3);
//myStream2>>cfg_3;
istringstream myStream3(cfg4);
myStream3>>cfg_4;
istringstream myStream4(cfg5);
myStream4>>cfg_5;
//
//Set the name of console
SetConsoleTitle(name);
//Generate true random number, but within range that is declared in config.txt
a:
srand((unsigned)time(0));
int range=(cfg_1-cfg_2)+1;
for(int index=0; index<20; index++){
a = cfg_2+int(range*rand()/(RAND_MAX + 1.0));
//Forced number from configuration (if there is supposed to be one)
if (cfg_4==1)
a=cfg_5;
cout << "Try to guess the number (Number from "<< cfg_2 <<" to " << cfg_1 << "): " << endl;
cin >> b;
if (b==a)
cout << "Number is correct!" << endl;
else
cout << "Sorry, you entered wrong number, random number was: " << a << endl;
//Ask if user wants to guess numbers again
int que;
cout << "Try to guess again? (Type 1 to confirm)" << endl;
cin >> que;
if (que==1){
cout << endl;
goto a;
}
else return(0);
}
}
|