[try Beta version]
Not logged in

 
Looping Problems

Jul 2, 2014 at 11:19pm
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 Jul 3, 2014 at 1:17am
Jul 2, 2014 at 11:40pm
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.
Jul 2, 2014 at 11:50pm
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
Jul 2, 2014 at 11:51pm
Please post your updated code?
Jul 2, 2014 at 11:51pm
line 17 what to compare?
Jul 2, 2014 at 11:58pm
string JustinWu="JustinWu";
Jul 2, 2014 at 11:58pm
Here's my updated code

Last edited on Jul 3, 2014 at 1:18am
Jul 3, 2014 at 12:01am
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;
}
Jul 3, 2014 at 12:02am
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 Jul 3, 2014 at 12:05am
Jul 3, 2014 at 12:09am
Thank you i made a mistake in the setting when using visual 2010. It works now though!
Topic archived. No new replies allowed.