-1

I just started coding so sorry if the code is too convoluded or messy, any tips are preciated

#include <iostream>

using namespace std;


class Nacion {
public:
    string nombre;
    string tematica;
    string entorno;
    Nacion (string aNombre , string aTematica , string aEntorno){
              nombre = aNombre;
              tematica = aTematica;
              entorno = aEntorno;
    }


};

int main(){
int eligNac;
string Categoria;
string Naciones;

Nacion nacion1 ("nombre" , "tematica" , "entorno");
Nacion nacion2 ("nombre" , "tematica" , "entorno");
Nacion nacion3 ("nombre" , "tematica" , "entorno");

cout << "Elije una categoria:\n";
cout << "Naciones\n";
cout << "Campeones" << endl;
cin >> Categoria;
if (Categoria == "Naciones")
    {

    cout << "Elije una nación:\n";
    cout << "1.-Demacia\n";
    cout << "2.-Freldjord\n";
    cout << "3.-Piltover\n" << endl;

    cin >> eligNac  >> endl;
    if(eligNac = 1){
      cout << nacion1 << endl;
    }



}

This is the one line of code I have troubles with, I don't know how to display the info of the object when an imput is given

Robert
  • 1

1 Answers1

0

Your probably want to look into C++ operator overloading: https://en.cppreference.com/w/cpp/language/operators , specifically, the operator<< part.

In your case, you probably want to do something like:

std::ostream & operator<<(std::ostream & os, const Nacion & n) {
    os << n.nombre << " / " << n.tematica << " / " << n.entorno << std::endl;
    return os;
}

This defines an "output operator" for your custom type, so that the compiler knows what to do when you ask it to do something like: cout << nacion1;

Also, you want to get rid of >> endl in cin >> eligNac >> endl;, and you probably want to do comparison if(eligNac == 1) instead of assignment if(eligNac = 1).

DeducibleSteak
  • 1,398
  • 11
  • 23