2013-04-14 30 views
1

問題是我無法輸入'c'的值,它是一個無限循環。我不能看到我做錯了。我對C++很陌生。無法輸入字符,並導致無限循環

#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

class manu 
{ 
    private: 
     int sqdno; 
     string name,speciality,take; 
     ofstream fileip; 
     ifstream fileop; 
    public: 
     manu() 
     { 
      fileip.open("Manchester United.txt"); 
      fileop.open("Manchester United.txt"); 
     }  
    public: 
     int input() 
     { 

      while(cin>>sqdno>>name>>speciality) 
      { 
       fileip<<sqdno<<' '<<name<<' '<<speciality<<endl; 
      } 
     } 
    public: 
     int display() 
     { 

      fileop>>take; 
      while(fileop.good()) 
      { 
       cout<<take<<endl; 
       fileop>>take; 
      } 
     } 
}; 

int main() 
{ 

    int c; 
    manu m; 
    cout<<"Enter squad details(press 'Ctrl+z' to exit on input):\n"; 

    do 
    { 
    cout<<"Select a choice:"<<endl; 
    cout<<"1.Input to file"<<endl; 
    cout<<"2.Display from file"<<endl; 
    cout<<"3.Exit"<<endl; 
    cin>>c; 
    switch(c) 
    { 
     case 1: 
      cout<<"Input squad_no,name and speciality of players:"; 
      m.input(); 
      break; 
     case 2: 
      m.display(); 
      break; 
     default: 
      cout<<"enter a valid choice"; 
    } 
    }while(c<3); 
} 
+0

'殼體「1」: '是你最想要的東西。 –

+0

獲取輸入函數我的班級manu – WhiteFlameBlaze

回答

1

以上代碼不會在視覺標準輸入輸出編譯8編譯器,因爲顯示和需要一個返回值輸入功能。

因此,要麼將返回類型更改爲void要麼從函數返回一些int類型的值。

我不能輸入「C」,它的無限循環值:

你,如果你想在無限循環運行,以修改交換機的情況下。

和你的功能輸入也有缺陷,因爲它也是一個無窮loop.You可以採取下面的代碼的參考,並相應地修改代碼....

#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

class manu 
{ 
    private: 
     int sqdno; 
     string name,speciality,take; 
     ofstream fileip; 
     ifstream fileop; 
    public: 
     manu() 
     { 
      fileip.open("Manchester United.txt"); 
      fileop.open("Manchester United.txt"); 
     }  
    public: 
     void input() 
     { 

      cin>>sqdno>>name>>speciality; 

      fileip<<sqdno<<' '<<name<<' '<<speciality<<endl; 

     } 
    public: 
     void display() 
     { 

      fileop>>take; 
      while(fileop.good()) 
      { 
       cout<<take<<endl; 
       fileop>>take; 
      } 
     } 
}; 

int main() 
{ 

    int c; 
    manu m; 
    cout<<"Enter squad details(press 'Ctrl+z' to exit on input):\n"; 

    while (1) 
    { 
     cout<<"Select a choice:"<<endl; 
     cout<<"1.Input to file"<<endl; 
     cout<<"2.Display from file"<<endl; 
     cout<<"3.Exit"<<endl; 
     cin>>c; 
     switch(c) 
     { 
      case 1: 
       cout<<"Input squad_no,name and speciality of players:"; 
       m.input(); 
       break; 
      case 2: 
       m.display(); 
       break; 
      case 3: 
      exit(1); 
      default: 
       cout<<"enter a valid choice"; 
       break; 
     } 
    } 
} 
+0

感謝您的回覆,但是我使用'Ctrl + Z'退出'input()'中的while循環,我相信這是'文件結束'指標。那麼while循環必須正確停止, – WhiteFlameBlaze

+0

當你在鍵盤上輸入1時,存儲在c中的值是49或'1',而不是1. –

+0

你的代碼工作正常,謝謝。但我希望做的它通過輸入Ctrl + z作爲輸入。 – WhiteFlameBlaze