String Evaluation Problem?

I am attempting to evaluate user input to see if it conflicts with a single string and not a array of strings. The user cannot enter "Admin" as their login name, and if they do it resets the prompt for a new entry; however, if the user does not enter "Admin" and they press "Y" to confirm his or her name, the string they enter (PendingChar_Name) is not assigned to the CheckedChar_Name, and therefore the program cannot display the CheckedChar_Name as the user's name.

Obviously I cannot evaluate and assign strings this way, otherwise it would work - or, the flow of control in my program is broken. What am I missing in evaluating and assigning PendingChar_Name and CheckedChar_Name?

My code is as follows:

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
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main(){

	system("TITLE Game");
	system("COLOR 4");

	string PendingChar_Name;
	string ValidChar_Name;
	string InValidChar_Name = "Admin";
	string CheckedChar_Name;
	char reply;
    char cDecision;

do {
	system("CLS");
	std::cout << "What is your name?" << endl;
	std::cin >> PendingChar_Name;

}while (PendingChar_Name == InValidChar_Name);

if (PendingChar_Name != InValidChar_Name)
	PendingChar_Name = CheckedChar_Name;

do {
	system("CLS");
	std::cout << "Your name is " << CheckedChar_Name << "." << endl;
	std::cout << "Is this name correct (Y/N)?" << endl;
	cin >> cDecision;

}while(cDecision == 'Y' || cDecision == 'y');

    system("CLS");
    std::cout << "Welcome to The Game, " << CheckedChar_Name << "!" << endl;
    std::cout << "\n\nType 'q' followed by pressing the 'Enter' key to quit: ";
    cin >> reply;

return 0;
}
Last edited on
Your problem is here: PendingChar_Name = CheckedChar_Name; swap the operands
Also notice that this line: }while(cDecision == 'Y' || cDecision == 'y'); will make the loop restarting if the user says 'y'
Interesting. I cant believe I didn't consider which way the operands flowed in this statement. Thank you for enlightening my oversight.

It is clear I am giving the user only one option with the code:

}while(cDecision == 'Y' || cDecision == 'y');.

The user wont be able to reset their name by typing "N/n" for "No/no" to re enter the do/while loop. Would you suggest using an if/else if loop to allow both Y' and N' to determine the course of the program?

Thanks again.
Here is some pseudocode -from line 19 to 35 of your code-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
while(true)
{
    read name;
    if  (name is valid) // ie name != InValidChar_Name
    {
           ask confirmation;
           read answer
           if ( answer == yes )
           {
                checked_name = name;
                break; // exit from the loop
           }
    }
}
Topic archived. No new replies allowed.