What am i doing wrong?

What am i doing wrong
im very new to C++ and im just trying some random things.
im trying to put a simpel password on the program.
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
#include "stdafx.h"
#include <iostream>
#include <Windows.h> 


char password;
using namespace std;
float key = 363;

int main()
{

	cout << "Hello welcome to the program" << endl;
	Sleep(1000);
	cout << "howmany kilos of Apples would you like?" << endl;
	Sleep(3000);
	cin >> password;

	if (password = key)
	{
		cout << "Welcome to the program" << endl;
		Sleep(10000);

	}
	else	cout << "Wrong Password" << endl;


    return 0;
}

password = key
I suspect you mean password == key

= is for assigning values.
== is for comparing values

Other problems include the following:

password is a single char. So it can hold one character. So if the user enters more than one character, you've got a problem.

key is a float, password is a char. How can you compare a number to a char?
Last edited on
..... i feel like an idiot, this shows how new i am.
Thanks allot.
closed account (E0p9LyTq)
Look very carefully at line 19. You are assigning (=) a float to a char.

Comparing (==) a float to a char would be just as much a problem, for different reasons.
Okay i made it a litlle bit more simpel but now it says Wrong password even when i fill in the right password..
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
#include "stdafx.h"
#include <iostream>
#include <Windows.h> 


char password;
using namespace std;


int main()
{

	cout << "Hello welcome to the program" << endl;
	Sleep(1000);
	cout << "howmany kilos of Apples would you like?" << endl;
	Sleep(3000);
	cin >> password;

	if (password == 313)
	{
		cout << "Welcome to the program" << endl;
		Sleep(10000);

	}
	else
	{
		cout << "Wrong Password" << endl;
		Sleep(5000);
	}


	return 0;
}
Last edited on
Yes... kilos... of """apples""" ;)

TheLent, char is a data type meant for characters (or single bytes). If you want to enter a number, change password to be of type int.

Also, avoid global variables. Declare password inside main.

1
2
3
4
5
6
int main()
{
    int password;
    std::cin >> password;
    // ...
}
Last edited on
XD omg ive heard people like apples........
Thanks i supose i need to read more about data types
Last edited on
Topic archived. No new replies allowed.