Click here to Skip to main content
15,906,301 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to get a user inputted string variable the name of a new object.
i want the name of an object in class airplane to be taken from the user. Anything i try makes the computer think i want to redefine the variable name i gave to store the user input and make it the actual name of the object, rather than the object being name what is stored in the variable name. Im thinking pointer might be the answer but ive just started getting into those and dont know how to use it correctly

What I have tried:

string flight;
cin>> flight;
Airplane flight;

and as previously mentioned ive tried making flight a pointer before the last line but that didnt work, that might be me misusing pointers though:(
Updated 9-Jan-24 19:16pm

The name of an object can be stored in a member variable in the class. In your case, it could look like this:
class MyClass {
    string flight;

MyClass Airplane;
cin >> Airplane.flight;
Share this answer
Variable names don't really exist while your code is running: they are used by the compiler to create storage locations which may be in memory, but may be entirely within the processor which has a large set of registers that have nothing to do with memory. The actual names you give them are fixed when you compile your app, and are discarded when the EXE file you run is created.

You can't "create a variable" while your code is running, you create a storage location (on the heap for example) and store the location of that in a variable or a collection (a reference to which which is stored in a variable).

Think of variables like books: the book has a title (the variable has a name), and you can have many books on a shelf (many variables in an app), but to add information to it you don't magically create a new book, you open an existing one and write on a page. Books are variables in your code, pages are the storage locations within the book - and just as you add info by finding a blank page to write on, in your code you add a new storage location to a list (for example) and store the info in that.

The class contains members which can include the name, address, age, destination, and so on - so you create an instance of the class and fill in it's individual info for each user you want to add to your app!
Share this answer
CPallini 10-Jan-24 2:12am    
As Griff stated, you cannot do that.
An alternative could be using an unordered_map to link the user supplied string to newly constructed object. Try
#include <iostream>
#include <unordered_map>
using namespace std;

class Airplane
  unsigned tons;
  Airplane(unsigned tons):tons(tons){}
  unsigned get_tons() const {return tons;}

int main()
  string ap_name;
  unsigned ap_tons;
  unordered_map<string, Airplane> m_ap;

  m_ap.emplace( "plane1", 100 );
  m_ap.emplace( "plane2", 120 );
  m_ap.emplace( "plane3", 130 );

  // example of look-up by name
  cout << "weight of plane2 is " << m_ap["plane2"].get_tons() << "\n";

  // user will name next airplane
  cout << "name your airplane: ";
  cin >> ap_name;
  cout << "specify its weight (tons): ";
  cin >> ap_tons;

  m_ap.emplace( ap_name, ap_tons );

  for (auto const & [name, plane] : m_ap )
    cout << "m_ap[" << name << "] weights " << plane.get_tons() << "\n";
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900