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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:17:51+00:00 2026-05-31T00:17:51+00:00

I have been trying to figure out why it doesn’t read my second getline()

  • 0

I have been trying to figure out why it doesn’t read my second getline() function. The first getline(cin, input) works fine for getting the input data for partition. I did the same for getting the job datas, and it doesn’t work. No errors or anything. it just skips that line. Any help will be greatly appreciated. Thank you.

#include <iostream>
#include <string>
#include "memory.h"

#define show(a) std::cout << #a << ": " << (a) << std::endl

using std::cin;
using std::cout;
using std::endl;

int main(void)
{
    //Variables
    int MEM_SIZE = 0;

    vector<int> partition_input;
    vector<int> job_input;


    while(true)
    {
        //INPUT PARTITION DATA
        while(true)
        {
            char confirm;           
            string input;
            int temp;               //Temporary variable to store extracted data 
            int num_part = 0;       //Iterator to check partition size and memory, and partition limits
            int check_size = 0;

            cout << "Please enter the memory size and partitions in the following format: \n";
            cout << "1000 250 250 250 250 \n";
            cout << "> ";

            getline(cin, input);        //The whole of user input is stored in variable "line"
            istringstream os(input); //"line" is passed to stringstream to be ready to be distributed

            while(os >> temp)
            {
                partition_input.push_back(temp);
                check_size += partition_input[num_part];
                num_part++;
            }


            //sets the first input to MEM_SIZE
            //AND deletes it from the partition vector
            MEM_SIZE = partition_input[0];
            partition_input.erase(partition_input.begin(), partition_input.begin()+1);

            check_size -= MEM_SIZE;

            cout << "Memory size: " << MEM_SIZE << endl;
            cout << "# of Partitions: " << partition_input.size() << endl;
            cout << "Partition Sizes: ";

            for(int i = 0; i < partition_input.size(); i++)
                cout << partition_input[i] << ' ';
            cout << endl;

            cout << "Correct? (y/n) ";
            cin >> confirm;

            cout << endl << endl;
            if(confirm == 'n')
                continue;

            //Checks the total partition size and the total memory size
            if(!(MEM_SIZE >= check_size)) 
            {
                cout << "ERROR: Total partition size is less than total memory size." << endl;
                system("pause");
                continue;
            }
            //Check if it exceeded the max number of partition limit
            if((num_part-1) >=5)
            {
                cout << "ERROR: You have entered more than the allowed partition(5). Please try again. \n";
                system("pause");
                continue;
            }

            break;
        }

        //START INPUT JOB DATA

        do
        {
            string input2;
            cout << "Please enter the size of each job in the following format." << endl;
            cout << "300 100 200 400" << endl;
            cout << "> ";

            //*******************
            //IT SKIPS THESE TWO LINES
            getline(cin, input2);
            istringstream oss(input2);  
            //*******************

            int j;
            while(oss >> j)
                job_input.push_back(j);

            for(int i = 0; i < job_input.size(); i++)
                cout << job_input[i] <<  ' ';
            break;
        }while(false);
        //END OF INPUT JOB DATA


        break;
    }

    Memory A1(MEM_SIZE, partition_input.size(), partition_input, job_input.size(), job_input);

    //A1.FirstFit();
    //    A1.BestFit();
    //    A1.NextFit();
    //    A1.WorstFit();

    //system("pause");
    return 1;
}
  • 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-05-31T00:17:52+00:00Added an answer on May 31, 2026 at 12:17 am

    The problem is here:

            cout << "Correct? (y/n) ";
            cin >> confirm;
    

    This reads the confirm character from the input. But not the ‘\n’ character. Thus the next readline will read just that newline character.

    It is best not to mix std::getline() and operator>> when reading from the input.

    I would change the code to this:

            cout << "Correct? (y/n) ";
            std::string confirmLine;
            std::getline(cin, confirmLine);
    
            cout << endl << endl;
            if(confirmLine == "n")
                continue;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying for hours but couldn't figure out why it doesn't show
I have been trying to figure out a solution but nothing has really presented
I have been trying to figure out a way to tag several methods from
I am fairly new to Emacs and I have been trying to figure out
I have been trying to figure this out for way to long tonight. I
I have been going mad trying to figure out why my scripts weren't working,
I have been pulling my hair out trying to figure out what I can't
I have been pounding my head against a wall trying to figure out what
Here's a fun one I've been trying to figure out. I have the following
I have been trying to figure out how to implement a simple xml rpc

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.