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

  • SEARCH
  • Home
  • 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 9233777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:37:57+00:00 2026-06-18T06:37:57+00:00

Thanks for looking at my question in advanced. It may be quite a simple

  • 0

Thanks for looking at my question in advanced. It may be quite a simple one!

I have a menu where the user can select which file they wish to run through the system.

The code below is what my menu options do:

int menuLoop = 1;
int userChoice;
std::string getInput;

while(menuLoop == 1)
{
    std::cout << "Menu\n\n" 
         << "1. 20 names\n"
         << "2. 100 names\n"
         << "3. 500 names\n"
         << "4. 1000 names\n"
         << "5. 10,000 names\n"
         << "6. 50,000 names\n\n";
    std::cin >> userChoice;

    std::string getContent;

    if(userChoice == 1)
    {
        std::cout << "\n20 names\n"; 
        std::ifstream openFile("20.txt");
    }
    else if(userChoice == 2)
    {
        std::cout << "\n100 names\n"; 
        std::ifstream openFile("1C.txt");
    }
    else if(userChoice == 3)
    {
        std::cout << "\n500 names\n"; 
        std::ifstream openFile("5C.txt");
    }
    else if(userChoice == 4)
    {
        std::cout << "\n1000 names\n"; 
        std::ifstream openFile("1K.txt");
    }
    else if(userChoice == 5)
    {
        std::cout << "\n10,000 names\n"; 
        std::ifstream openFile("10K.txt");
    }
    else if(userChoice == 6)
    {
        std::cout << "\n50,000 names\n"; 
        std::ifstream openFile("50K.txt");
    }

The code that follows in the while loop deals with the values that are in the selected file, but the code is the same for each option. The next line is:

if(openFile.is_open())

Because of the way I have done it, it is saying “openFile” is undefined, which I completely understand but I was wondering whether anyone knows how I could get around?

  • 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-18T06:37:58+00:00Added an answer on June 18, 2026 at 6:37 am

    Declare openFile just once earlier in the while loop like so:

    std::ifstream openFile;
    

    This gives you an std::ifstream that is not associated with any particular file. Then in each of your if statements use std::ifstream::open rather than the std::ifstream constructor:

    openFile.open("20.txt");
    

    Of course, make sure each one has the correct file name.

    This way, the openFile object’s scope will be the while loop but you can open a different file depending on your condition.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question may sound simple but I can't get the answer i'm looking for
Thanks for looking at this question. I wanted to know how can I use
Hi and thanks for looking at this newbi question. I have looked for anykind
This may have a simple answer (and I hope it does) but looking online
I am new to TDD and DDD and I have one simple question regarding
and thanks for looking at this question. I am porting a Linux (2.6.37) driver
Thanks for looking on this problem. I have a page that is totally valid
Ok, so my question is something I have been looking for for a while.
This question arises from a problem we have here, and we're looking for a
I have a question. Recently I was looking into ways to implement hardware rendering

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.