2012-03-04 9 views
1

私は2番目のgetline()関数を読んでいない理由を理解しようとしています。最初のgetline(cin、input)は、パーティションの入力データを取得するのに問題ありません。私は仕事のデータを取得するために同じことをした、それは動作しません。エラーや何もありません。その行をスキップするだけです。どんな助けでも大歓迎です。ありがとうございました。2番目のgetline()関数をスキップしますか?

#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; 
} 

答えて

4

問題はここにある:

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

これは、入力からの確認文字を読み込みます。 しかし、 '\ n'文字ではない。したがって、次のreadlineは改行文字だけを読み込みます。

入力から読み込むときは、std :: getline()と演算子>>を混ぜないことをお勧めします。

私はこれにコードを変更します

 cout << "Correct? (y/n) "; 
     std::string confirmLine; 
     std::getline(cin, confirmLine); 

     cout << endl << endl; 
     if(confirmLine == "n") 
      continue; 
+0

これは正しい診断であるが、それは有益なソリューションを提供していません。 'cin.ignore'について説明するともっと助かります。 –

+0

@BenVoigt:問題は、std :: getline()と演算子>>を混在させることと関係していると思いますが、演算子>>の使用を取り除きます。私はそれを修正する方法を示す答えを更新しました。 –

関連する問題