Compare strings in C ++

1

I am learning c ++ and the truth is that I do not find the solution to my problem:

I want to create a program that asks for name and age, and that in case you enter the correct name (Dan), one thing comes up, and if not, then another one.

#include <iostream>
using namespace std;

int main()
{
    int edad, nombre, Dan;      
    cout << "introduce tu nombre y tu edad \n";
    cin >> edad >> nombre;    
    if (nombre == Dan && edad >= 18) {
        cout << "Eres el verdadero Dan \n";
    }
    else {
        cout << "No eres el verdadero Dan \n";
    }
    system("PAUSE");
    return 0;
    
asked by Dan Cezanne Galavan 03.10.2018 в 21:40
source

1 answer

1

You are storing the name in an integer ( int ), the name should be a string of characters:

int edad;
std::string nombre;
//          ~~~~~~ <-- nombre como objeto cadena de caracteres.

std::cout << "introduce tu nombre \n";
std::cin >> nombre;

std::cout << "introduce tu edad \n";
std::cin >> edad;

if (nombre == "Dan" && edad >= 18) {
//            ~~~~~ <-- cadena de caracteres literal.
    std::cout << "Eres el verdadero Dan \n";
}
else {
    std::cout << "No eres el verdadero Dan \n";
}
    
answered by 03.10.2018 в 23:34