它跳过第二个getline()函数
It skips the second getline() function?
我一直在想为什么它不读取我的第二个getline()函数。第一个getline(cin,input)可以很好地获取分区的输入数据。我在获取工作数据时也做了同样的事情,但没有用。没有任何错误。它只是跳过了那一行。任何帮助都将不胜感激。非常感谢。
#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;
}
问题就在这里:
cout << "Correct? (y/n) ";
cin >> confirm;
这将从输入中读取确认字符但不是'\n'字符。因此,下一个读取行将只读取该换行符。
从输入中读取时,最好不要混合std::getline()和运算符>>。
我会把代码改成这样:
cout << "Correct? (y/n) ";
std::string confirmLine;
std::getline(cin, confirmLine);
cout << endl << endl;
if(confirmLine == "n")
continue;
相关文章:
- 错误 没有与参数列表匹配的重载函数"getline"实例
- 错误:调用'getline'没有匹配函数
- 确切地说,如何解释 std::getline(stream, string) 函数在C++中填充的字符串
- 我在C++中使用 getline() 函数时遇到问题
- getline 函数似乎在 C++ 中不起作用
- 如何以及何时使用 getline 函数执行计算?
- 对c++中的输入使用getline函数时忽略第一个字符
- 使用getline和字符串函数C++程序中的分段错误
- C++ 获取线错误。如何避免此错误?"没有重载函数"getline"的实例
- 如何使此函数使用 getline 读取字符串并使用 int 执行相同的行为?
- 无匹配函数呼叫getline()
- 当将 getline 与 int 一起使用时,如何修复"没有重载函数 'getline' 的实例与参数列表匹配"
- 错误:函数"getLine"必须具有原型
- 我可以在函数 getline 中使用 2 个或更多分隔符C++吗?
- 没有与参数列表匹配的重载函数"getline"实例
- 错误 没有与参数列表 C++ 匹配的重载函数"getline"实例
- 视觉C++ Getline 问题(没有重载函数"getline"实例
- "没有重载函数'getline'的实例.."
- 在c++类中重载全局函数getline
- C++ getline() 错误,没有与参数列表匹配的重载函数"getline"实例