Probably overlooking something (edited)

I'm working on a program that has all my passwords stored on it and if you answer the security questions it will display my passwords if I forget them. I'v gone over this program over and over and can't figure out what's wrong. It skips to the end 'if' statement.

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
#include <iostream>
using namespace std;
#include <string>
#include <thread>


string answer, answer2, answer3, answer4, answer5, answer6, answer7;

int main()

{
	cout << "Make sure you capitalise only what needs to be capitalised."<<endl;
	cin.get();    //is a better way to pause console
	system("CLS");

	cout << "I'm writing 'bla bla bla' where my security questions would be?" <<endl;
	cin >> answer;

	system("CLS");

	cout << "bla bla bla?"<<endl;
	cin >> answer2;

	system("CLS");

	cout << "bla bla bla?"<<endl;
	cin >> answer3;

	system("CLS");

	cout << "bla bla bla"<<endl;
	cin >> answer4;

	system("CLS");

	cout << "bla bla bla"<<endl;
	cin >> answer5;

	system("CLS");

	cout << "bla bla bla"<<endl;
	cin >> answer6;

	system("CLS");




	if ((answer == "bla bla bla") && (answer2 == "bla bla bla") && (answer3 == "bla bla bla") && (answer4 == "bla bla") && (answer5 == "bla bla bla") && (answer6 == "bla bla bla"))

	{


		cout << "Make sure no one else is looking. You will be looking at sensitive information. Would you like to continue?"<<endl;
		cin >> answer7 ;

		if ((answer7 != "yes") || (answer7 != "Yes"))
		{
		   return 0;
		}

		system ("CLS");

		std::this_thread::sleep_for(std::chrono::milliseconds(1000)) ;
		  cout << "5"<<endl; 
		std::this_thread::sleep_for(std::chrono::milliseconds(1000)) ;
		    system ("CLS");
		  cout << "4"<<endl;
		std::this_thread::sleep_for(std::chrono::milliseconds(1000)) ;
		    system ("CLS");
		  cout << "3"<<endl;
		std::this_thread::sleep_for(std::chrono::milliseconds(1000)) ;
		    system ("CLS");
		  cout << "2"<<endl;
		std::this_thread::sleep_for(std::chrono::milliseconds(1000)) ;
		    system ("CLS");
		  cout << "1"<<endl;
		std::this_thread::sleep_for(std::chrono::milliseconds(1000)) ;
		    system ("CLS");
		  cout << "0"<<endl;

	
		system ("CLS");

		cout << "This is where my passwords would be"<<endl;
		

	cin.get();
	system("PAUSE");

		
	}


	
	if ((answer != "Bla bla bla") || (answer2 != "bla bla") || (answer3 != "bla") || (answer4 != "bla") || (answer5 != "bla") || (answer6 != "bla"))

	{
		cout << "You have entered a wrong answer and are not allowed"<<endl;
	    cout << "to pry into the information in this program. Any more wrong attempts to gain access to the info enclosed will be regarded"<<endl;
		cout << "as an attack and the maker of this program will be notified. The said maker of this program is now warning you that any other"<<endl;
		cout << "attempt to gain access is an invasion of privacy and therefor permits him to gain access to your computer. The said person hereby"<<endl;
		cout << "offers this disclaimer to show that you have been warned and if you continue to meddle he has the right to launch the malware"<<endl;
		cout << "embedded into this program. This program has malware securely stored and if there are three or more failed attempts the malware"<<endl;
		cout << "shall be inputted and embedded into your software. You have been warned!"<<endl;
		
		cin.get();
		system("PAUSE");

	}

	return 0;
}


Please help me figure out what's wrong. Also, I know that if someone with really good programming skills could get this file and figure out my passwords since they are embedded in it, but I am a beginner and do not know how to encrypt my program so that you HAVE to answer the security questions and that is the only way.
Last edited on
use getline instead of cin >> answer; because if you use cin >> answer; and you enter bla bla bla you will only extract the first bla from the input stream because cin uses a space for a delimiter.
http://www.cplusplus.com/reference/string/string/getline/

also is (answer4 == "bla bla") supposed to be (answer4 == "bla bla bla")?
Last edited on
Firstly, std::cin only extracts characters until it hits a white-space character, so anytime your answers contain spaces, new-lines, etc, only the characters up to the white-space character will be extracted.

Typically, this is tackled by using std::getline() or std::cin.getline().

std::getline() has two implementations, the first which reads characters into a string until it hits a specified delimiter character - the other reads characters into a string until it hits a new-line.

std::cin.getline() reads characters into a char* string until a specified number of characters has been read, or until it hits a specified delimiter character.

Now we know our options. We can now make a solution using one of these methods. Here's one I slapped together using std::getline():

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
#include <iostream>
#include <string>

int main(int argc, char* argv[]) {

	const unsigned short num_questions = 3;
	std::string questions[num_questions] = {
		"What is your name?",
		"What is your birthdate?",
		"What is your favorite color?"
	};
	/*
	Note that if you're super worried about security, which I doubt, you would have to make things a little more sophisticated.
	It would make sense to have the answers in a file, encrypted with an algorithm of your choosing/design. There's no guarantee that someone won't
	reverse-engineer your program to see how the algorithm works.
	For now, the correct answers are hard-coded into the program.
	*/

	std::string answers[num_questions] = {
		"john doe",
		"whatever",
		"blue and turquoise and yellow"
	};

	bool flag = true;
	for(unsigned short i=0; i<num_questions && flag; ++i) {
		std::string answer;
		system("cls");//Place holder. Not the safest thing either.
		std::cout << questions[i] << std::endl;
		std::getline(std::cin, answer);
		for(unsigned short j=0; j<answer.size(); ++j) {//ensure that any capitalized letters are turned into their lower-case counter parts
			if(answer[j]>='A' && answer[j]<='Z') {
				answer[j]+=32;
			}
		}
		if(answer != answers[i]) {
			flag = false;
		}
	}

	if(flag) {
		std::cout << "Correct!" << std::endl;
		//things to do if the answers were correct
	}else {
		std::cout << "Wrong!" << std::endl;
		//things to do if the answers were incorrect
	}

	std::cin.get();

	return 0;
}
Last edited on
closed account (Dy7SLyTq)
as to encrypting, two easy methods could be a) a ceaser cipher or b) a hash algorithim
dont use using namespace std;
why are you including thread? why did you make 7 string variables global (never a good idea) (also i would look at this sites tutorial for arrays and containers. it will save you a lot of time)
dont use system. cin.get() is a horrible way to pause the console imo, second only to that of system("pause");
> if ((answer7 != "yes") || (answer7 != "Yes"))
|| means or

assume that the answer was `yes'
1
2
3
if( "yes" != "yes" or "yes" != "Yes")
if( false or true )
if( true )


assume that the answer was `Yes'
1
2
3
if( "Yes" != "yes" or "Yes" != "Yes")
if( true or false )
if( true )


another answer would give
1
2
if( true or true )
if( true )
so you've got a tautology.
Last edited on
cin.get() is a horrible way to pause the console imo

Not really. It SORT of is, but when you consider the other options, you really only have system("PAUSE"). What would you suggest otherwise? Also, rather than just saying don't use system, maybe it would help giving an alternative...

Anyway, here are my alternatives:
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
// Pausing
std::cin.sync();
std::cin.get();

// Clear Screen - the easy way!
std::cout << std::string('\n', 50);

// Clear Screen - the hard (non portable, assuming windows) way:
#include <windows.h>
void cls(void) {
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hStdOut == (void*)ERROR_INVALID_HANDLE)
        return;

    CONSOLE_SCREEN_BUFFER_INFO csbi;
    if (!GetConsoleScreenBufferInfo(hStdOut, &csbi)
        return;
    unsigned long cells = csbi.dwSize.X * csbi.dwSize.Y;

    if (!FillConsoleOutputCharacter(hStdOut, ' ', cells, {0, 0}, nullptr))
        return;

    if (!FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, cells, {0, 0}, nullptr))
        return;

    SetConsoleCursorPosition(hStdOut, {0, 0});
}


closed account (Dy7SLyTq)
im sorry i should have been more specific. cin.get() by itself is bad, like using vector without a bounds check. heres why:
1
2
3
4
char c;
cout<<"$";
cin>> c;
cin.get();

if i input
$ab
then it is going to *skip cin.get(). so you want to clear the buffer first. for what i do is i just turn the buffer off then force the program to wait for input

edit: *well not skip it but essentially the same thing in this case
Last edited on
Thank you all very much for your suggestions. I made all the changes that I understood. However...it still skips to the warning. Here is my revised version, and again, thanks for all the suggestions. I learned so much!

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
#include <iostream>
#include <string>


std::string answer, answer2, answer3, answer4, answer5, answer6, answer7;


int main()

{
	std::cout << "Make sure you capitalise only what needs to be capitalised."<< "!\n";
	   std::cin.sync();
       std::cin.get(); //is a better way to pause console
	system("CLS");

	std::cout << "What is the best spy of all time in movies?" << "!\n";
	std::getline (std::cin,answer);

	system("CLS");

	std::cout << "What is your name?"<< "!\n";
	std::getline (std::cin,answer2);

	system("CLS");

	std::cout << "What is your favorite color?"<< "!\n";
	std::getline (std::cin,answer3);

	system("CLS");

	std::cout << "Who built the house where you lived in for the majority of your life? Type his last name."<< "!\n";
	std::getline (std::cin,answer4);

	system("CLS");

	std::cout << "What girl in 8th grade was stupid when you were in 7th grade? First name only."<< "!\n";
	std::getline (std::cin,answer5);

	system("CLS");

	std::cout << "What was the name of the first camp (CYO) that you went to? Type 'Camp' and then the name of the camp."<< "!\n";
	std::getline (std::cin,answer6);

	system("CLS");




	if ((answer == "I'm obviously not going to display these answers") && (answer2 == "censored") && (answer3 == "censored") && (answer4 == "censored") && (answer5 == "censored") && (answer6 == "censored"))

	{


		std::cout << "Make sure no one else is looking. You will be looking at sensitive information. Would you like to continue?"<< "!\n";
		std::getline (std::cin,answer7);

		if (answer7 != "yes")
		{
		   return 0;
		}

		system ("CLS");

		std::this_thread::sleep_for(std::chrono::milliseconds(1000)) ;  //'this thread' and 'chrono' give error: name followed by '::' must be a class
		  std::cout << "5"<<endl; 
		std::this_thread::sleep_for(std::chrono::milliseconds(1000)) ;
		    system ("CLS");
		  std::cout << "4"<<endl;
		std::this_thread::sleep_for(std::chrono::milliseconds(1000)) ;
		    system ("CLS");
		  std::cout << "3"<<endl;
		std::this_thread::sleep_for(std::chrono::milliseconds(1000)) ;
		    system ("CLS");
		  std::cout << "2"<<endl;
		std::this_thread::sleep_for(std::chrono::milliseconds(1000)) ;
		    system ("CLS");
		  std::cout << "1"<<endl;
		std::this_thread::sleep_for(std::chrono::milliseconds(1000)) ;
		    system ("CLS");
		  std::cout << "0"<<endl;

	
		system ("CLS");

		std::cout << "Password is 'password'"<< "!\n";
		
	std::cin.sync();
	std::cin.get();

		
	}


	
	if ((answer != "censored") || (answer2 != "censored") || (answer3 != "censored") || (answer4 != "censored") || (answer5 != "censored") || (answer6 != "censored"))

	{
		std::cout << "You have entered a wrong answer and are not allowed"<<endl;
	    std::cout << "to pry into the information in this program. Any more wrong attempts to gain access to the info enclosed will be regarded"<<endl;
		std::cout << "as an attack and the maker of this program will be notified. The said maker of this program is now warning you that any other"<<endl;
		std::cout << "attempt to gain access is an invasion of privacy and therefor permits him to gain access to your computer. The said person hereby"<<endl;
		std::cout << "offers this disclaimer to show that you have been warned and if you continue to meddle he has the right to launch the malware"<<endl;
		std::cout << "embedded into this program. This program has malware securely stored and if there are three or more failed attempts the malware"<<endl;
		std::cout << "shall be inputted and embedded into your software. You have been warned!"<<endl;
		

		std::cin.sync();
                std::cin.get();
		

	}

	return 0;
}



Also, I learned about the hash algorithm, but I'm not sure how I would use it. I'm pretty sure its this:
1
2
3
4
public:
static HashAlgorithm^ Create(
	String^ hashName
)


Is everything in the parentheses then encrypted? How do you use this command?
Last edited on
closed account (Dy7SLyTq)
i would use an array ie string answer[7]
dont use system
i would use a for loop to compare answers 2-6 since they all need to be equal to "censored"
of course the thread gives error. you removed the line #include <thread>. i was asking why use threads at all? there are better ways to accomplish this
you dont need to wrap the response for if they get the question wrong because you can just do else
The program still skips to the "you have entered a wrong answer" part. Also,

std::this_thread::sleep_for(std::chrono::milliseconds(1000)) //both this_thread and chrono are underlined in red. It gives the error "must be a class"

Please help! I don't want to open up a new thread....but no one is helping me...maybe my thread on the forum is on page 42 by now or something.
closed account (Dy7SLyTq)
It gets bumped to the top when there is a new post... And we are helping. You aren't listening. The thread stuff is an error because you removed the line #include <thread>. You shouldn't use thread code in a minor app like this anyways. Did you listen to the other changes I said or are you still ignoring them like you have since I've started helping? Also that looks like c++/cli not c++ for the hashing algorithm which isn't actually a hashing algorithm, just a function prototype
Topic archived. No new replies allowed.