New to C++, what is the mistake in my code?

Hey guys, new C++ learner here. I am having an issue with some code I wrote for a simple username + password program. The correct of both username and password are hard-coded in but I'll deal with that when I have a better understanding of the language.

(I have cut out one or two chunks so it's easier to read)

What is happening is as I try return the value of cinValid to main() it seems to always have the value of 0. There is probably an easier way for me to do the loginVerify() but I just can't think of one. Am I making a simple mistake and if so can you point it out to me?

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
  #include "stdafx.h"
#include <iostream>
#include <string>
#include "Windows.h"

using namespace std;

std::string cinUsername;
std::string cinPassword;

int counter;
int cinValid;

int loginVerify()
{

	if (cinUsername == "123" && cinPassword == "123")
		{   
		int cinValid = 1;
		}
	else
		{
		int cinValid = 0;
		}
	
	return cinValid;
}

int main()
{

	for (int counter = 0; counter < 4; counter++)
	{
		loginQuestions(); // runs the questions where you input 
                                  // username/password (123 & 123)
		loginVerify();

		if (cinValid == 1)
		{
			cout << "Correct!\n";
			Sleep(500);
			cout << "You're in.\n";
			break;
			return 0;
		}
		else
		{
			cout << "Incorrect. \n";
			Sleep(500);
			cout << "Try again\n";
			Sleep(500);
			cout << "You have " << 3 - counter << " tries remaining. \n";
		}
	}

	cin.get();
}
Last edited on
The variables you create and set on line 19 and 23 cease to exist after those lines.

You are *shadowing*; creating new variables with the same name. Look up shadowing, then don't do it.
Last edited on
Topic archived. No new replies allowed.