2015-11-04 97 views
-2

所以這是一個船舶的數據庫,我的問題是,我必須檢查用戶是否輸入一個整數,但是當程序將註冊表保存到數組中時,我將顯示結果註冊表變量,其中用戶將該數字置爲一個爭用數需要幫助來確定爲什麼我的數組沒有保存輸入?

int tester1 = 0; 
cout << "What is the registry of the ship?: "; 
      cout << " >>"; 



      if (cin >> tester1) 
      { 
      tester1 >> array[counter].registry; 
      cin.clear(); 
      cin.ignore(1000, '\n'); 
      } 

      else 
      { 
       while (!(cin >> tester1)) 
       { 
        cout << "Please enter an integer!!" << endl; 
        cout << " >>"; 
        cin >> tester1; 
        cin.clear(); 
        cin.ignore(1000, '\n'); 
       } 


      } 
+2

'tester1 >>陣列[計數器] .registry;'那是什麼?請發佈[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 –

+0

tester1抓取用戶輸入並檢查是否爲int,因此如果是int,則tester1將其保存到數組中,使用 –

+0

tester1 >> array [counter] .registry; –

回答

0
cin>>tester1; 
while(!cin) { 
    cin.clear(); 
    cin.sync(); 
    cout << "Please enter an integer!!" << endl; 
    cout << " >>"; 
    cin>>tester1; 
} 
array[counter].registry=tester1; 
+0

如果你的switch語句是一個字符你怎麼能檢查用戶輸入一個字符來允許切換髮生 –

+3

你可以通過解釋你在做什麼和爲什麼來改善這個答案。 – MicroVirus

+0

@YusmePradera'var_char = getchar(); //無論輸入多少個字符,只記錄第一個字符 –

相關問題