How to declare a variable mactch as new string?

Greetings community. I'm new to C++ and am seeking advice regarding how to store a variable match (==) as a new string. If I recall correctly, a bool statement can be used to store matching int variables, however I am looking for a command for storing string variables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cout << "ASSIGN YOUR LOGIN:" << endl;

    cout << "USERNAME: ";
            string username_declaration;
            getline (cin, username_declaration);
    cout << "PASSWORD: ";
            string password_declaration;
            getline (cin, password_declaration);
        cout << endl;
    
    cout << "RE-ENTER YOUR LOGIN TO VERIFY:" << endl;
    
    cout << "USERNAME: ";
            string username_confirmation;
            getline (cin, username_confirmation);
    cout << "PASSWORD: ";
            string password_confirmation;
            getline (cin, password_confirmation);
        cout << endl;

//THE SECTION BELOW IS WHAT I NEED HELP WITH. NEW TO C++ SO AM UNFAMILIAR WITH HOW TO APPROACH THIS:

string username_input = username_declaration == username_confirmation;
Last edited on
1
2
3
4
5
6
  if (username_input == username_declaration)
  {  // They match - Do something
  }
  else
  {  // They don't match - do something else
  }


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Thank you for your response and my apologies, thank you shining light on the formatting tools. I greatly appreciate your response but, respectfully, it doesn't provide a solution to my challange. I am already familiar with if, else and else if statements.

Essentially, I desire to store an if condition as a new string. I want to declare a string called "username_input" ...where once read, the system will identify this string as the stored condition for "username_declaration == username_confirmation"

From what I've seen online something similar can be achieved for int's with a bool command. However, I am unfamiliar for how to achieve the similar result for strings (I've tried formatting it similarly to a bool format but it still doesn't work)
once read, the system will identify this string as the stored condition for "username_declaration == username_confirmation"


What do you mean by this? That the string will be "true" or "false"? Or that it will literally be "username_declaration == username_confirmation"? You can just assign the value to a boolean then print it:

1
2
bool username_input = ;username_declaration == username_confirmation;
std::cout<<username_input; //outputs 1 or 0 depending on the boolean value 


If you want true or false:
std::cout << std::boolalpha << username_input; //outputs 'true' or 'false'
Thank you for your response, the solutions you've provided seem interesting, as I'm a new learner.

To further elaborate, with reference to the intial code I provided above... let's say that I enter "cat" as my username_declaration and username_confirmation", which is recognised as a match (==)... I am curious as to whether I can assign a/that condition into "username_input" that will identify the match and thereby output the word "cat" automatically?


You're still not being vary clear. What is it that you want to store in username_input? A string, or a boolean? Saying you want to store "a condition" doesn't really mean anything.
Last edited on
Topic archived. No new replies allowed.