else, else if and if

Hey im having a bit of trouble here im pretty sure you guys can see what im trying to do and this is the firt bit im stuck on.

So far i have covered:
Lesson 0: Installing Visual Studio Express 2012
Lesson 1: Creating a Console Application (2013)
Lesson 1: Creating a Console Application (2012)
Lesson 1: Creating a Console Application (2010)
Lesson 1: Creating a Console Application (2008)
Lesson 1: Creating a Console Application (2003)
Lesson 2: Basic Input and Output
Lesson 3: Variables and Constants
Lesson 4: Basic Data Types
Lesson 5: Logical Operators
Lesson 6: Relational Operators

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main() {
	int pass;
	cout << "Hello sean, what is your password?" << endl;
	cin >> pass;

	if (pass == 123);
	cout << "Welcome sean" << endl;

	else if;
	cout << "Access denied" << endl;

	return 0;
}
line 13. you dont need a semi-colon, but what you do need is a condition for your second if statement. BUT i don't think you actually want a second if there.

follow this pattern:
1
2
3
4
5
6
7
8
if (pass == 123)  // bad to have a semi-colon on this line as well
{ // put enclosing braces in, even if if you one line under your if and else
    cout << "Welcome sean" << endl;
}
else
{
    cout << "Access denied" << endl;
}

Last edited on
you nearly got it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
int main() {
	int pass;
	cout << "Hello sean, what is your password?" << endl;
	cin >> pass;

	if (pass == 123)
	cout << "Welcome sean" << endl;

	else
	cout << "Access denied" << endl;

	return 0;
}


there is no ; after if loops

you don't need the 2nd if, unless you wanted to create another "else"
when doing "if else" loops, the last one should just be "else"
What if i wanted the password to be sean123 How would i create it so a password can be typed with text?

p.s i see where i went wrong now that makes sense thank you
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;
int main() {
	string pass;
	cout << "Hello sean, what is your password?" << endl;
	cin >> pass;

	if (pass == "sean123")
	cout << "Welcome sean" << endl;

	else
	cout << "Access denied" << endl;

	return 0;
}

"string" is for letters, special signs and numbers
change "int" which is for numbers only to "string"
put "" what you want as well
Hey, thank you i understand that.

Finally,
do you have any idea how this wouldn't work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
using namespace std;

int main() {
	int pass;
	cout << "Hello sean, what is your password?" << endl;
	cin >> pass;

if (pass == 123)  // bad to have a semi-colon on this line as well
{ // put enclosing braces in, even if if you one line under your if and else
    cout << "Access granted welcome sean" << endl;
	int iNumber;
	cout << "how old are you sean? " << endl;
	cin >> iNumber;
	cout << "Sean you're " << iNumber " Years old" << endl;
}
else
{
    cout << "Access denied! Password is incorrect." << endl;
}

	return 0;
}


PS i kept ur notes in there so i can remember
Last edited on
nearly.
line 16 should be:
cout << "Sean you're " << iNumber << "Years old" << endl;
Ow yes thank you i need to stop forgetting these little things like ; or inputs and outputs. all of the help is well appriciated thank you guys
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
using namespace std;

int main() {
	int pass;
	cout << "Hello sean, what is your password?" << endl;
	cin >> pass;

if (pass != 123)  // bad to have a semi-colon on this line as well
{ // put enclosing braces in, even if if you one line under your if and else
    cout << "Access denied! Password is incorrect." << endl;
}
else
{
    cout << "Access granted welcome sean" << endl;
	int iNumber;
	cout << "how old are you sean? " << endl;
	cin >> iNumber;
	cout << "Sean you're " << iNumber " Years old" << endl;
}

	return 0;
}

"!=" means does not equal
note for the future. Should write error first. so you can get it out of the way
Easier to read. just helps a lot in the long run
Furthermore, would there be any way to create a delay between messages? for example

1
2
3
4
5
6
7
8
9
10
11
#include <cstdlib>

cout << "Quiting in 5 seconds << endl;
delay 1 (IN SECONDS) 

cout << "4" << endl;
delay 1 

so on so on then it quits the application?

exit( exit_code ); 


I know this wasnt an actuall code but it was an example
Last edited on
@DyavolskiMost
Ow yeh now i remember learning that i must recap over my lessons.
its like:
< means smaller
> means greater
>= means greater or equal to
<= means smaller or equal to

Right?


ow yeh this was my attempt at a delayed message

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

int main() {
	int pass;
	cout << "Hello sean, what is your password?" << endl;
	cin >> pass;

if (pass == 123)  // bad to have a semi-colon on this line as well
{ // put enclosing braces in, even if if you one line under your if and else
    cout << "Access granted welcome sean" << endl;
	cout << "how old are you sean? " << endl;
	int iNumber;
	cin >> iNumber;
    cout << "Sean you're " << iNumber << "Years old" << endl;
}
else
{
    cout << "Access denied! Password is incorrect." << endl;
	cout.flush(); 
	sleep(1);
	cout << "Quiting in 5 seconds" << endl;
	cout.flush();
	sleep(1);
	cout << "4" << endl;
}

	return 0;
}
NVM i found the fix it just requires #include windows.h

and also sleep has an upercase S
1
2
	cout << "Quiting in 5 seconds" << endl;
	cout.flush();

You don't need the second line. outputting endl will automatically flush the stream. In fact, that's the purpose of endl vs. simply outputting '\n'.
Topic archived. No new replies allowed.