Looping Problems

So I am trying to write a program that asks the user for a name. If the user enter the right name the program ends but if the user were to enter the wrong name the program would enter a loop prompting the user to re-enter the name.

However I am not able to get the program to work! any help would be greatly appreciated

Last edited on
You don't need line 10.

You don't use passName at all. Did you mean passName to hold a value of "JustinWu" and then compare acconName to passName in line 17?

If no, then you'd need double quotes around "JustinWu" on line 17, if you're just directly comparing against the text entered.
what im trying to do is get the user to enter a name, in this case the only name accepted is JustinWu. If the user enters the wrong name I want the program to loop and ask the user to re-enter their name.

I tried your suggestions, but the program still wont compile
Please post your updated code?
line 17 what to compare?
string JustinWu="JustinWu";
Here's my updated code

Last edited on
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string acconName;
	string passName;
	string JustinWu="JustinWu";

	cout<<"Welcome to the Bank of Wonderland!"<<endl;

	cout<<"Enter your Name: ";
	cin>>acconName;

	while (acconName!=JustinWu)
	{
		cout<<"Sorry, incorrect name. Re-enter name: ";
		cin>>acconName;
	}


	system("pause");
	return 0;
}
That works for me - is it not compiling for you?

I added a few things here just to make the input/output print out.

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

int main()
{
	string acconName;
	
	cout<<"Welcome to the Bank of Wonderland!"<<endl;
	cout<<"Enter your Name: ";
	cin>>acconName;

	while (acconName!="JustinWu")
	{
		cout<<"\nSorry, " << acconName << " is an incorrect name. Re-enter name: ";
		cin>>acconName;
	}

	cout << "\nWelcome " << acconName << "!" << endl;
	system("pause");
	return 0;
Welcome to the Bank of Wonderland!
Enter your Name: 
Sorry, JustinW is an incorrect name. Re-enter name: 
Sorry, JustnWu is an incorrect name. Re-enter name: 
Welcome JustinWu!
Last edited on
Thank you i made a mistake in the setting when using visual 2010. It works now though!
Topic archived. No new replies allowed.