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 8608569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:38:47+00:00 2026-06-12T03:38:47+00:00

I have a program where I’m specifying the file to be read using cin,

  • 0

I have a program where I’m specifying the file to be read using cin, so I run the program ./prog < file.txt, but in the following code, cin doesn’t grab anything. Could someone explain why line is empty after the code executes?

 void Building::build(){
    char mode;
    cin >> mode >> sizeFloors >> numFloors;

    if(mode == 'M')
        readMap(sizeFloors, numFloors);

}

^^ this executes fine

void Building::readMap(int floorSize, int numFloors){

    string line;
    int curFloor(numFloors - 1);

    while( curFloor >= 0 ){
        cin >> line;

        if(line.empty()){
            cout << "Error: input file too short" << endl;
            exit(1);
        }
    }   

^^ here line.empty() returns true

this is the input file

M
4
1
WWWW
WWWW
WWWW
WWWW

so clearly line shouldnt return empty

  • 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-12T03:38:49+00:00Added an answer on June 12, 2026 at 3:38 am

    When mixing formatted input (i.e., using operator>>()) and unformatted input (e.g., std::getline()) you need to make sure that you are at a location you are interested in. The formatted input operators stop reading the moment their format is satisfied. For example, reading a character just reads that one character. Any following charactters, e.g., a newline, is left in the input. std::getline() stops reading at the first newline received. I guess, after you entered your menu selection you hit newline and this is where std::getline() stops (same if the menu selection is in a file on its own).

    A typical approach when switching between formatted and unformatted I/O is to skip all leading spaces:

    std::getline(std::cin >> std::ws, line);
    

    Alternatively, you can ignore everything up to and including the first newline:

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    

    Which approach is used clearly depends on the content of your data. For example, if you want to read code where leading namespace matters skipping up to the first non-whitespace is not an award winning idea. However, in many situations using std::ws works just fine.

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

Sidebar

Related Questions

I have program proba2.exe and file text.txt in same folder, when I set command
I have program that sends a file to a printer using 'lpr' command. I
I have program which reads MSMQ using GetAllMessages but it does not remove messages
I have program that requires Python 3, but I develop Django and it uses
I have a program that gets a JSON from the server using getJSON and
i have program which prints all char from char_min to char_max here is code
I have program using gtkmm, gtkglextmm and exiv2. I want to include these libraries
Ok I have created a program which saves text into settings file. So at
I'm confused. I have program which I run as a non-administrator user. This program
I have program with asp.net c# but now i need to create an application

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.