Help with while loop!!

<code/>

// designers network
// Demo logical operators
#include<iostream>
#include<string>

using namespace std;
int main()

{
string username;
string password;
bool success;

cout << "\t Game Designer's Network\n";
{
cout << "\nUsername: ";
cin >> username;

cout << "\nPassword: ";
cin >> password;

if (username == "S.Meier" && password == "civilization")
{
cout << "\nHey, Sid.\n";
success = true;
}
else if (username == "S.Miyamoto" && password == "mariobros")
{
cout << "\nWhat's up, Shigeru?\n";
success = true;
}
else if (username == "H.Kojima" && password == "metalgear")
{
cout << "\nWhat's up, Hideo?\n";
success = true;
}
else if (username == "emma" && password == "munchakin")
{
cout << "\nHey baby <3 you!\n";
success = true;
}
else if (username == "guest" || password == "guest")
{
cout << "\nYou are logged in as a guest\n";
success = true;
}
else if (username == "dale" && password == "bob")
{
cout << "\nYo Broski!! Wha gwarn??\n";
success = true;
}

else
{
cout << "\nComputer says no!!.\n";
success = false;
// return 0;
}

}while (!success);



return 0;
}


<code/>

I would appreciate some help with the above program I am currently reading several C++ books and learning just for fun!
and have hit a bit of a wall and don't want to move on before understanding thoroughly the process near the end of the file it should repeat the program if you input an incorrect login once again I would appreciate any help for this n00b problem....
You either need:

1
2
3
4
do
{
   // your code...
} while( !success );


or

1
2
3
4
while( !success )
{
   // your code...
} 


Also review this: http://www.cplusplus.com/doc/tutorial/control/
Hi there. I am pretty new to C++ myself.

It looks like you need to add the "do" part of the do-while statement before the first if statement.

Like this:

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
58
59
60
61
62
63
64
65
66
67
68
69
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	// designers network
// Demo logical operators

string username;
string password;
bool success;

do{
cout << "\t Game Designer's Network\n";
{
cout << "\nUsername: ";
cin >> username;

cout << "\nPassword: ";
cin >> password;

if (username == "S.Meier" && password == "civilization")
{
cout << "\nHey, Sid.\n";
success = true;
}
else if (username == "S.Miyamoto" && password == "mariobros")
{
cout << "\nWhat's up, Shigeru?\n";
success = true;
}
else if (username == "H.Kojima" && password == "metalgear")
{
cout << "\nWhat's up, Hideo?\n";
success = true;
}
else if (username == "emma" && password == "munchakin")
{
cout << "\nHey baby <3 you!\n";
success = true;
}
else if (username == "guest" || password == "guest")
{
cout << "\nYou are logged in as a guest\n";
success = true;
}
else if (username == "dale" && password == "bob")
{
cout << "\nYo Broski!! Wha gwarn??\n";
success = true;
}

else
{
cout << "\nComputer says no!!.\n";
success = false;
// return 0;
}

}
}while (!success);



return 0;
}
Excellent I used the do-while it must be atypo in the example in the book I'm using thank you very much greatly appreciated!!
You also might want to add a clear screen command to your else statement to clear the last user name and password.

1
2
3
4
5
6
7
else
{
cout << "\nComputer says no!!.\n";
system("CLS");
success = false;
// return 0;
}


Just a suggestion, though. It doesn't affect the functionality, only cleans it up a bit.
Thanks that does make it a whole lot better - nice and clean I'll definitely bear that in mind in the future!

Cheers.
I believe system("cls"); is not cross-platform API call. May work in Windows but not in Unix/Linux. Of cuz if you are targeting only Windows it is fine.
Well I am Linux based and do love all that is open source, but not necessarily targeting any OS as such asI'm just just learning code to for fun and to get a greater understanding of what happens under the hood so to speak. And there is of course the added bonus of being able to code an app that will do some function I may need at the time. (although I see this being quite some time off!)

Cheers again for the help guys.
Well I am Linux based


Does that mean if I figure out your brain's IP address, telnet into it, and type "shutdown -h now" you'll instantly drop to the ground like a brick? :) Sorry, just had to...
Topic archived. No new replies allowed.