1

Ctrl+でループを終了しようとしていますZが、機能しません。コードを注意深く調べましたが、問題がわかりません。あなたの助けに感謝します。Dev-C++ を使用しています。コードは次のとおりです。

#include <iostream>
#include<conio.h>

using namespace std;

class student 
{
  private:
   string name;
   int age;
   double GPA;
  public:

   void read ();

};

void student::read()
{
  do
   {   //enter student data
     cout << "Name:  " ;
     cin>>name;
     cout<<"Age:  ";
     cin>>age;
     cout << "GPA:  ";
     cin>>GPA;

    cout<<"\n\n  any key to continue or Ctrl+Z to exit."<<endl<<endl;
   }
   while(getch()!=EOF);  //Ctrl+Z to exit
}


int  main()
{
  student stud;
  stud.read();
  return 0;
}
4

3 に答える 3

2

WindowsコンソールI/OとC++ストリームI/Oを混在させています。ゲイリー・ラーソンを言い換えると、テラリウムで互換性のない種を混ぜ合わせました。

次のように、C++構造だけを使用してみてください。

std::cout << "Enter name, age, GPA; or CTRL-Z to exit\n";
while ( cin >> name >> age >> GPA )
{
  // do something with one set of input
}

または、do-while形式を維持したい場合:

do
{   //enter student data
  cout << "Name:  " ;
  if( !cin>>name ) break;
  cout<<"Age:  ";
  if( !cin>>age) break;
  cout << "GPA:  ";
  if( !cin>>GPA) break;
}
while(cin);  //Ctrl+Z to exit
于 2012-04-15T07:07:04.857 に答える