Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9133509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:30:04+00:00 2026-06-17T08:30:04+00:00

I’m a beginner to C++ programming, and I’m wondering how you would go about

  • 0

I’m a beginner to C++ programming, and I’m wondering how you would go about passing a struct as an argument to a function using cin.

The idea of the code is to input the name of a struct from the user, and have that name be passed to a function. Here’s what I’ve been playing around with:

   class myPrintSpool
    {
    public:
        myPrintSpool();
        void addToPrintSpool(struct file1);
    private:
        int printSpoolSize();
        myPrintSpool *printSpoolHead;
    };

    struct file1
   {
        string fileName;
        int filePriority;
        file1* next;

   };

    int main()
    {
        myPrintSpool myPrintSpool; 
        myPrintSpool.addToPrintSpool(file1);
    return 0; 
    } 

This is able to build. However, I wanted something more along the lines of:

 class myPrintSpool
    {
    public:
        myPrintSpool();
        void addToPrintSpool(struct fileName);
    private:
        int printSpoolSize();
        myPrintSpool *printSpoolHead;
    };

    struct file1
   {
        string fileName;
        int filePriority;
        file1* next;

   };

    int main()
    {
        string fileName; 
        cout << "What is the name of the file you would like to add to the linked list?"; 
        cin >> fileName; 

        myPrintSpool myPrintSpool; 
        myPrintSpool.addToPrintSpool(fileName);
    return 0; 
    } 

Can anyone help how I would go about doing this? Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:30:05+00:00Added an answer on June 17, 2026 at 8:30 am

    This sort of metaprogramming is, in general, extremely advanced in C++. The reason is, unlike interpreted languages, much of what exists in the source file is lost when the file is compiled. In the executable, the string file1 may not show up at all! (it’s implementation dependent, I believe).

    Instead, I would recommend doing some sort of lookup. For instance, you can compare the string passed in in fileName to each struct’s fileName, or, you can just associate any key with your struct. For instance, if you created a std::map<string, baseStruct*> and inherited all of your structs (e.g. file1, file2, …) from baseStruct, then you can lookup in the map which struct is associated with the passed in string. The inheritance is important, because you will need polymorphism to insert structs of differing types into the map.

    There are many other, more advanced topics that we could get into, but this is the general idea. It’s most simple to do some sort of lookup rather than try to instantiate a type at runtime from a string. Here is a more rigorous and more maintainable approach to doing basically the same thing.

    EDIT: If you mean you have only one type of struct called ‘file1’ and you want to instantiate it and pass it to addToPrintSpool, that’s different than my previous answer (which applies if, for example, you want to have multiple structs called file1 and file2 and want to infer which struct to use. Figuring out types dynamically from a string is hard, but setting the string in an instance of a known type is straightforward.)

    To instantiate and use an instance of file1 you can do this:

    //In myPrintSpool, use this method signature.
    //You are passing in an object of type file1 named aFile;
    //note that this object is _copied_ from someFile in your
    //main function to a variable called aFile here.
    void addToPrintSpool(file1 aFile);
    ...
    int main()
    {
        string fileName; 
        cout << "What is the name of the file you would like to add to the linked list?"; 
        cin >> fileName; 
    
        //Instantiate a file1 object named someFile, which has all default fields.
        file1 someFile;
        //Set the filename of aFile to be the name you read into the (local) fileName var.
        someFile.fileName = fileName;
    
        myPrintSpool myPrintSpool; 
        //Pass someFile by value into addToPrintSpool
        myPrintSpool.addToPrintSpool(someFile);
        return 0; 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am using JSon response to parse title,date content and thumbnail images and place
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.