+ 2
Help
Iām having trouble getting my code to work and I was wondering if anyone can help. Hereās the code int main() { int a; a =1111; cin >> number; number = b int b; int myPassword = a; if(b = myPassword){ cout<< "Access Granted"; } else{ "Access Denied"; } return 0; }
3 Answers
+ 4
Best place to start is looking at the error message you receive, it most often tells you where to look and what to do.
::::: ERROR MESSAGE :::::
..\Playground\: In function 'int main()':
..\Playground\:8:8: error: 'number' was not declared in this scope
cin >> number;
^
..\Playground\:9:10: error: 'b' was not declared in this scope
number = b
^
..\Playground\:10:1: error: expected ';' before 'int'
int b;
As you can see by the error message, you didn't declare the variables 'numbers' or 'b'. As well, you forgot to put a semi-colon at the end of one of your statements.
In regards to variable 'b', you're using the variable by assigning it to 'numbers' and then you declare 'b' afterwards. You'll always want to declare your variables prior to using them.
Also, in your IF-ELSE statement, in the ELSE portion, you just have a string but you aren't doing anything with it or assigning it. You forgot to put "cout <<" in front of it.
In the IF condition, you have "b = myPassword" which isn't a comparison operator, it's an assignment operator. You'll want to do "b == myPassword" instead.
One last note, you have some redundancy with your variables. You're not really doing anything with variable 'a' other than assign it to the variable myPassword, which means 'a' was intended as the myPassword variable and should be set as such. Just remove variable 'a' and use myPassword, and change variable 'b' to something meaningful like "inputPassword" Also, the variable 'number' isn't even being used, so scrap it and use inputPassword in its place to store the input.
:::: CORRECTED CODE ::::
https://code.sololearn.com/cktDRwovPUVE/#cpp
#include <iostream>
using namespace std;
int main()
{
int myPassword = 1111;
int inputPassword;
cin >> inputPassword;
if(inputPassword == myPassword){
cout << "Access Granted";
}
else{
cout << "Access Denied";
}
return 0;
}
+ 1
A couple of things:
- b should be declared before being used
- if condition should use the == operator not = which is an assignment
+ 1
change your function name. it is not to be "main".
move "number = b" and "int b;".
change "b = myPassword" to "b == myPassword".