Page 1 of 2

C++ Help

Posted: 22 Sep 2015, 21:50
by Razor
Hi, does anyone code throught c++ ? I have a snipped and need to add 1 loop and 1 "function".
Looking for somebody who want to help.
Cheers!

Re: C++ Help

Posted: 23 Sep 2015, 05:47
by Sektor
I'm a noob at C++ but I think even I can do that.

Re: C++ Help

Posted: 23 Sep 2015, 08:34
by Razor
[syntax=c++]#include <cstdlib>
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

int main() {

int n;
int num_attempts = 4;
//int time
string your_attempt = " ";
int y;

cout << "ROBCO INDUSTRIES (™) TERMLINK PROTOCOL" << endl;
cout << "ENTER PASSWORD NOW" << endl;


cout << endl;
cout << num_attempts << " ATTEMPT(S) LEFT: ";
for (n = 0; n<num_attempts; n++) {
cout << "■";
} cout << endl;
cout << endl;
cout << "0xF4F0 VIRAL 0xF5BC MINES" << endl;
cout << "0cF4FC DARED 0xF5C8 LIKES" << endl;
cout << "0xF508 WIRES 0xF5D4 PARTS" << endl;
cout << "0xF514 LINES 0xF5E0 HAREM" << endl;
cout << "0xF520 LIVES 0xF5EC LINED" << endl;
cout << "0xF52C SORTS 0xF5F8 WARNS" << endl;
cout << "0xF538 AGREE 0xF604 THREW" << endl;
cout << "0xF544 FARMS 0xF610 SIDES" << endl;

do {
if (num_attempts > 0) {
cout << " > ";
cin >> your_attempt;
if (your_attempt != "AGREE") {
if (your_attempt == "VIRAL" || your_attempt == "LINES" || your_attempt == "LIVES" || your_attempt == "SORTS" || your_attempt == "FARMS" || your_attempt == "MINES" || your_attempt == "LIKES" || your_attempt == "PARTS" || your_attempt == "LINED" || your_attempt == "WARNS" || your_attempt == "SIDES") {
cout << "Incorrect! " << "1/5 Characters Right" << endl;
}
else { cout << "Incorrect! " << "2/5 Characters Right" << endl; }
}
}
num_attempts = num_attempts - 1;
cout << num_attempts << " ATTEMPT(S) LEFT: ";
for (n = 0; n<num_attempts; n++) {
cout << "■";
} cout << endl;
} while (your_attempt != "AGREE" && num_attempts != 0);

if (your_attempt == "AGREE") {
cout << "Exact Match! +75 XP" << endl;
}
else {
cout << "You have been temporarily locked out! Contact the Administrator!" << endl;
}
system("PAUSE");
return 0;
}[/syntax]

OK so i need to do a loop that if you get answer wrong four times you will be blocked for 3 minutes and when you are correct you get that text plus your CD-ROM drive will open. I dont want to make this program quit - if you wont get an answer - time loop and back to the beggining if you got it the result (text + opening of cd rom) just freeze - do not shut down this program.

That will be fine!

One other thing - if somebody want to - you can re-doo script that counts letters (here i just have words that has 2 same letters that the right one) i't would be great to add other words and make program count them and show other results like comparing "key word" 'pedestrian' to 'lesbian' >output> wrong password 5/10 Characters Right. But its optional :)

Anyway i dont care that this is hardcoded.

Re: C++ Help

Posted: 23 Sep 2015, 08:56
by Sektor
You could surround it with an infinite do while loop.

[cpp]
int main() {

do {

.......

} while (1);

system("PAUSE");
return 0;
}
[/cpp]

That doesn't use a function but it's easy enough to add one.

Re: C++ Help

Posted: 23 Sep 2015, 09:03
by Razor
OK WORKED - but still it loops itself and i want to get an result and thats it - no repetition (otherwise 3min of break and then return).

Re: C++ Help

Posted: 23 Sep 2015, 09:15
by Sektor
This will exit if you type AGREE or loop after 3 minutes if you get it wrong.

[cpp]#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>

using namespace std;

int main() {

string your_attempt = " ";

while (your_attempt != "AGREE") {
int n;
int num_attempts = 4;
long lockout_time;
int y;

cout << "ROBCO INDUSTRIES (™) TERMLINK PROTOCOL" << endl;
cout << "ENTER PASSWORD NOW" << endl;

cout << endl;
cout << num_attempts << " ATTEMPT(S) LEFT: ";
for (n = 0; n<num_attempts; n++) {
cout << "?";
} cout << endl;
cout << endl;
cout << "0xF4F0 VIRAL 0xF5BC MINES" << endl;
cout << "0cF4FC DARED 0xF5C8 LIKES" << endl;
cout << "0xF508 WIRES 0xF5D4 PARTS" << endl;
cout << "0xF514 LINES 0xF5E0 HAREM" << endl;
cout << "0xF520 LIVES 0xF5EC LINED" << endl;
cout << "0xF52C SORTS 0xF5F8 WARNS" << endl;
cout << "0xF538 AGREE 0xF604 THREW" << endl;
cout << "0xF544 FARMS 0xF610 SIDES" << endl;

do {
if (num_attempts > 0) {
cout << " > ";
cin >> your_attempt;
if (your_attempt != "AGREE") {
if (your_attempt == "VIRAL" || your_attempt == "LINES" || your_attempt == "LIVES" || your_attempt == "SORTS" || your_attempt == "FARMS" || your_attempt == "MINES" || your_attempt == "LIKES" || your_attempt == "PARTS" || your_attempt == "LINED" || your_attempt == "WARNS" || your_attempt == "SIDES") {
cout << "Incorrect! " << "1/5 Characters Right" << endl;
}
else { cout << "Incorrect! " << "2/5 Characters Right" << endl; }
}
}
num_attempts = num_attempts - 1;
cout << num_attempts << " ATTEMPT(S) LEFT: ";
for (n = 0; n<num_attempts; n++) {
cout << "?";
} cout << endl;
} while (your_attempt != "AGREE" && num_attempts != 0);

if (your_attempt == "AGREE") {
cout << "Exact Match! +75 XP" << endl;
}
else {
cout << "You have been temporarily locked out! Contact the Administrator!" << endl;

time_t now = time(NULL);
lockout_time=now+180;
while (now<lockout_time) {
now = std::time(NULL);
cout << lockout_time-now << " until lockout ends" << endl;
}
}
}

system("PAUSE");
return 0;
}
[/cpp]

Re: C++ Help

Posted: 23 Sep 2015, 09:40
by Sektor
Updated with timer.

Re: C++ Help

Posted: 23 Sep 2015, 10:01
by Razor
Nicely done - but how to prevent it from closing? And i mean "forever" not loop just print msg and then freez program.

Re: C++ Help

Posted: 23 Sep 2015, 10:05
by Sektor
You can add an infinite while loop. I also made it only update the time remaining each second. It was showing each millisecond before. I hope you learn this and don't just copy it for homework. I haven't played Fallout 3 but I saw this hacking game in a video of it.

[cpp]#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>

using namespace std;

int main() {

string your_attempt = " ";

while (your_attempt != "AGREE") {
int n;
int num_attempts = 4;
int lockout_time;
int y;
time_t now;
int old;

cout << "ROBCO INDUSTRIES (™) TERMLINK PROTOCOL" << endl;
cout << "ENTER PASSWORD NOW" << endl;
cout << endl;
cout << num_attempts << " ATTEMPT(S) LEFT: ";
for (n = 0; n<num_attempts; n++) {
cout << "?";
} cout << endl;
cout << endl;
cout << "0xF4F0 VIRAL 0xF5BC MINES" << endl;
cout << "0cF4FC DARED 0xF5C8 LIKES" << endl;
cout << "0xF508 WIRES 0xF5D4 PARTS" << endl;
cout << "0xF514 LINES 0xF5E0 HAREM" << endl;
cout << "0xF520 LIVES 0xF5EC LINED" << endl;
cout << "0xF52C SORTS 0xF5F8 WARNS" << endl;
cout << "0xF538 AGREE 0xF604 THREW" << endl;
cout << "0xF544 FARMS 0xF610 SIDES" << endl;

do {
if (num_attempts > 0) {
cout << " > ";
cin >> your_attempt;
if (your_attempt != "AGREE") {
if (your_attempt == "VIRAL" || your_attempt == "LINES" || your_attempt == "LIVES" || your_attempt == "SORTS" || your_attempt == "FARMS" || your_attempt == "MINES" || your_attempt == "LIKES" || your_attempt == "PARTS" || your_attempt == "LINED" || your_attempt == "WARNS" || your_attempt == "SIDES") {
cout << "Incorrect! " << "1/5 Characters Right" << endl;
}
else { cout << "Incorrect! " << "2/5 Characters Right" << endl; }
}
}
num_attempts = num_attempts - 1;
cout << num_attempts << " ATTEMPT(S) LEFT: ";
for (n = 0; n<num_attempts; n++) {
cout << "?";
} cout << endl;
} while (your_attempt != "AGREE" && num_attempts != 0);

if (your_attempt == "AGREE") {
cout << "Exact Match! +75 XP" << endl;
}
else {
cout << "You have been temporarily locked out! Contact the Administrator!" << endl;

now = time(NULL);
lockout_time=now+180;

while (now<lockout_time) {
now = time(NULL);
if (now && now!=old) {
cout << lockout_time-now << " until lockout ends" << endl;
old = now;
}
}
}
}

//system("PAUSE");
while(1){}
return 0;
}[/cpp]

Re: C++ Help

Posted: 23 Sep 2015, 10:37
by Razor
Well something went wrong : P

About copying - I want to start to learn this language same way i have learned gta2 scripting. But now i'm having problems with time and you know that time is what you need when you learn language.

Re: C++ Help

Posted: 23 Sep 2015, 10:54
by Sektor
Does it do that every time? Might need to initalise some variables or change NULL to 0.

I'm installing Visual Studio now. It worked fine in mingw.

I initialised "now" variable and changed NULL to 0.

[cpp]
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>

using namespace std;

int main() {

string your_attempt = " ";

while (your_attempt != "AGREE") {
int n;
int num_attempts = 4;
int lockout_time;
int y;
time_t now = time(0);
int old;

cout << "ROBCO INDUSTRIES (™) TERMLINK PROTOCOL" << endl;
cout << "ENTER PASSWORD NOW" << endl;
cout << endl;
cout << num_attempts << " ATTEMPT(S) LEFT: ";
for (n = 0; n<num_attempts; n++) {
cout << "?";
} cout << endl;
cout << endl;
cout << "0xF4F0 VIRAL 0xF5BC MINES" << endl;
cout << "0cF4FC DARED 0xF5C8 LIKES" << endl;
cout << "0xF508 WIRES 0xF5D4 PARTS" << endl;
cout << "0xF514 LINES 0xF5E0 HAREM" << endl;
cout << "0xF520 LIVES 0xF5EC LINED" << endl;
cout << "0xF52C SORTS 0xF5F8 WARNS" << endl;
cout << "0xF538 AGREE 0xF604 THREW" << endl;
cout << "0xF544 FARMS 0xF610 SIDES" << endl;

do {
if (num_attempts > 0) {
cout << " > ";
cin >> your_attempt;
if (your_attempt != "AGREE") {
if (your_attempt == "VIRAL" || your_attempt == "LINES" || your_attempt == "LIVES" || your_attempt == "SORTS" || your_attempt == "FARMS" || your_attempt == "MINES" || your_attempt == "LIKES" || your_attempt == "PARTS" || your_attempt == "LINED" || your_attempt == "WARNS" || your_attempt == "SIDES") {
cout << "Incorrect! " << "1/5 Characters Right" << endl;
}
else { cout << "Incorrect! " << "2/5 Characters Right" << endl; }
}
}
num_attempts = num_attempts - 1;
cout << num_attempts << " ATTEMPT(S) LEFT: ";
for (n = 0; n<num_attempts; n++) {
cout << "?";
} cout << endl;
} while (your_attempt != "AGREE" && num_attempts != 0);

if (your_attempt == "AGREE") {
cout << "Exact Match! +75 XP" << endl;
}
else {
cout << "You have been temporarily locked out! Contact the Administrator!" << endl;

now = time(NULL);
lockout_time=now+180;

while (now<lockout_time) {
now = time(0);
if (now && now!=old) {
cout << lockout_time-now << " until lockout ends" << endl;
old = now;
}
}
}
}

//system("PAUSE");
while(1){}
return 0;
}
[/cpp]

Re: C++ Help

Posted: 23 Sep 2015, 11:09
by Razor
Errors, shouldn't we give those ints some value?

Re: C++ Help

Posted: 23 Sep 2015, 11:35
by Sektor
Yes change int old; to int old=0;

Re: C++ Help

Posted: 23 Sep 2015, 11:57
by Razor
Okey, so far so good - its working as i wanted. Last one thing - I discovered that you can type whatever you want for example: 'poop' and it still return 2/5 letter info - The result i wanted should be only for words from code, otherwise it should return other text like: WRONG.

Re: C++ Help

Posted: 23 Sep 2015, 12:23
by Sektor
You could use http://www.cplusplus.com/reference/string/string/find/ to find letters in the strings.

Since the position of the letters is fixed, you don't need to use find, you could use:
http://www.cplusplus.com/reference/stri ... ng/substr/
http://www.cplusplus.com/reference/stri ... g/compare/

If you aren't expected to use those then you could make another word list with just words that share 2 letters.

Re: C++ Help

Posted: 23 Sep 2015, 12:58
by Razor
im going to make list that just share 2 letters. And hardcode them. So if you put something else than they you will recive other communicate.

Re: C++ Help

Posted: 23 Sep 2015, 13:34
by Sektor
I ended up not using find, substr or compare. This version can show any number of correct letters and doesn't use word lists.

Image

[cpp]
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>

using namespace std;

int main() {

string your_attempt = " ";
string password = "AGREE";

while (your_attempt != password) {

int num_attempts = 4;
int n = 0;
int correct_letters = 0;
time_t lockout_time = 0;
time_t old = 0;
time_t now = 0;

cout << "ROBCO INDUSTRIES (™) TERMLINK PROTOCOL" << endl;
cout << "ENTER PASSWORD NOW" << endl;
cout << endl;
cout << num_attempts << " ATTEMPT(S) LEFT: ";
for (n = 0; n<num_attempts; n++) {
cout << "?";
} cout << endl;
cout << endl;
cout << "0xF4F0 VIRAL 0xF5BC MINES" << endl;
cout << "0cF4FC DARED 0xF5C8 LIKES" << endl;
cout << "0xF508 WIRES 0xF5D4 PARTS" << endl;
cout << "0xF514 LINES 0xF5E0 HAREM" << endl;
cout << "0xF520 LIVES 0xF5EC LINED" << endl;
cout << "0xF52C SORTS 0xF5F8 WARNS" << endl;
cout << "0xF538 AGREE 0xF604 THREW" << endl;
cout << "0xF544 FARMS 0xF610 SIDES" << endl;

do {
if (num_attempts > 0) {
cout << " > ";
cin >> your_attempt;
your_attempt.resize(5);

if (your_attempt != password) {
correct_letters = 0;
for (int i = 0; i < password.size(); i++) {
if (your_attempt == password) {
correct_letters++;
}
}

cout << "Incorrect! " << correct_letters << "/5 Characters Right" << endl;
}
}
if (your_attempt != password) {
num_attempts = num_attempts - 1;
cout << num_attempts << " ATTEMPT(S) LEFT: ";
for (n = 0; n < num_attempts; n++) {
cout << "?";
} cout << endl;
}
} while (your_attempt != password && num_attempts != 0);

if (your_attempt == password) {
cout << "Exact Match! +75 XP" << endl;
}
else {
cout << "You have been temporarily locked out! Contact the Administrator!" << endl;

now = time(0);
lockout_time = now + 180;

while (now<lockout_time) {
now = time(0);
if (now && now != old) {
cout << lockout_time - now << " seconds until lockout ends" << endl;
old = now;
}
}
}
}

//system("PAUSE");
while (1){}
return 0;
}
[/cpp]

Re: C++ Help

Posted: 23 Sep 2015, 17:36
by Razor
THANKS~!
Ok, So now i can change password and hints to whatever i want but it has to have 5 letters, yes? Or at least the same ammount of letters so i can change just outcome for "/xletters"

Am i right?

LESBIAN WOMBAD <3 *_*

Re: C++ Help

Posted: 23 Sep 2015, 21:02
by Razor
Tried to update that code and got error:

[cpp]#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>

using namespace std;

int main() {

string your_attempt = " ";
string password = "SKAZANI";

while (your_attempt != password) {

int num_attempts = 4;
int n = 0;
int correct_letters = 0;
time_t lockout_time = 0;
time_t old = 0;
time_t now = 0;

cout << "DR. WADDLES (™) PMS (PROTECTING MEMORY SYSTEM) " << endl;
cout << "WPISZ HASLO:" << endl;
cout << endl;
cout << num_attempts << " ZOSTALO PROB: ";
for (n = 0; n<num_attempts; n++) {
cout << "▀";
} cout << endl;
cout << endl;


cout << " _____ _____ _____ _____ _ _ _ _____ _____ _____ _____ "<< endl;
cout << "| | | _ | | | | | | | _ | | | | __|" << endl;
cout << "| | | -| | | | | | | | | |- -| __|" << endl;
cout << "|__|__|__|__|__|__|_____|_____|__|__|_|___|_____|_____|" << endl;
cout << "" << endl;
cout << " _____ _____ _____ _____ _____ _____ _____ __ _____ " << endl;
cout << "|_ _| __| __ | | | | | _ | | | _ |" << endl;
cout << " | | | __| -| | | |- -| | | | | |__| |" << endl;
cout << " |_| |_____|__|__|_|_|_|_____|_|___|__|__|_____|__|__|" << endl;
cout << "" << endl;

cout << "- BRAK POLSKICH ZNAKOW + WIELKIE LITERY" << endl;
cout << "__________________________________________" << endl;
cout << "0xF68C ..;$##$%^FRA 0xF68D $SARKAZM$$$$" << endl;
cout << "0xF698 SZKA%#$%{}!@ 0xF699 {}^%#@@#!SKA" << endl;
cout << "0xF6A4 <:%#%@#{}@!# 0xF6B5 ZANI>>,@{#:@" << endl;
cout << "0xF6S7 ..^ZASUWKA#@ 0xF6S6 %#@!@$@*#$%@" << endl;
cout << "0xF5H9 ###!!#~~~SZM 0xF4H8 }|}|}|}@@#!!" << endl;
cout << "0xF7S9 ATKA%:??>>]Z 0xF987 ####!@^&KARW" << endl;
cout << "0xF789 ASYPKA./[ZAK 0xF123 ASZ##~!@#%KA" << endl;
cout << "0xF1Z3 WASY^^^[][]@ 0xF444 SIARZ_.~*.*~" << endl;
cout << "0xF1Z4 [_%%!!<>>>>| 0xF001 #####;[.,:{$" << endl;
cout << "0xF2B3 ~!!@!@**^^%$ 0xF003 <][[;;<>^$%$" << endl;
cout << "0xF3B3 <,?EKSTAZA*% 0xF006 *APASZKI$%#@" << endl;
cout << "0xF3C4 &#$%##^&{ {[ 0xF009 (&*(&^%SZKAR" << endl;
cout << "0xF4A1 /*-*SZAKALE| 0xF112 AD#^$KATUSZA" << endl;
cout << "__________________________________________" << endl;

do {
if (num_attempts > 0) {
cout << " > ";
cin >> your_attempt;
your_attempt.resize(5);

if (your_attempt != password) {
correct_letters = 0;
for (int i = 0; i < password.size(); i++) {
if (your_attempt == password) {
correct_letters++;
}
}

cout << "NIEPRAWIDLOWE HASLO! " << correct_letters << "/7 POPRAWNYCH ZNAKOW" << endl;
}
}
if (your_attempt != password) {
num_attempts = num_attempts - 1;
cout << num_attempts << " ZOSTALO PROB: ";
for (n = 0; n < num_attempts; n++) {
cout << "▀";
} cout << endl;
}
} while (your_attempt != password && num_attempts != 0);

if (your_attempt == password) {
cout << "WYGRALES ZYCIE" << endl;
}
else {
cout << "DOSTEP DO KONTA ZOSTAL ZABLOKOWANY NA 3 MINUTY!" << endl;

now = time(0);
lockout_time = now + 180;

while (now<lockout_time) {
now = time(0);
if (now && now != old) {
cout << lockout_time - now << " SEKUND POZOSTALO" << endl;
old = now;
}
}
}
}

//system("PAUSE");
while (1) {}
return 0;
}[/cpp]

WHY?

Re: C++ Help

Posted: 23 Sep 2015, 21:47
by Sektor
your_attempt.resize(5);

Change 5 to password length + 1 or password.size()