I'm having problems with my code

Sorry guys, i'm an absolute newbie and need help with this code.
I'm trying to get the program to shutdown the computer once a numeral password has been entered. Can you all help to improve my code?

#include <iostream>
#include <cmath>
#include <windows.h>

using namespace std;
int main () {

int a;
cout<<"what is the password?" <<endl;
cin>>a;

if (a==3);
cout<<"good job"<<endl;
system ("pause");
system ("shutdown/p");

if a<3;
cout<<"Wrong Password try again"<<endl;
cin>>a;

if a>3;
cout<<"Wrong Password try again"<<endl;
cin>>a;
}
return 0;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>
#include <windows.h>

using namespace std;
int main () 
{

    int a;
    cout<<"what is the password?" <<endl;
    cin>>a;

    if (a==3) {
        cout<<"good job"<<endl;
        system ("pause");
        system ("shutdown/p");
    } else {
        if a<3;
        cout<<"Wrong Password try again"<<endl;
        cin>>a;
    }

return 0;
The program needs a loop, for example a while loop. Otherwise, only the very first cin>>a; input is useful.

As for the if-else structure, if the first test (a==3) is not true, the else clause can handle the other possibilities without the need for any further if conditions.
Last edited on
Try using a loop for the wrong password check.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>
#include <windows.h>

using namespace std;
int main () 
{
	int a;
	cout<<"what is the password?" <<endl;
	cin>>a;

	while (a != 3)
	{
		cout<<"Wrong Password try again"<<endl;
		cin >> a;
	}

	cout<<"good job"<<endl;
	system ("pause");
	system ("shutdown /p");

	return 0;
}


Dang, Ninja'd Had to answer a phone before I hit submit.
Last edited on
Thanks guys(Gals). The code works like a charm! I've learnt how to handle this already! Thanks Stewbond!
@MiiNiPaa:
Your code has an error at Return 0... I have no idea how to solve it. You mind dropping me an email to help me with that?
Topic archived. No new replies allowed.