ファイルへの書き込みについて。

解決


魔法使いの見習い  2005-02-09 22:06:29  No: 56339

コンパイルする時にどうしてもエラーになって、

ファイルへの書き込みがどうしてもできません。

宣言の仕方の間違えがあれば教えてください。

お願いします。

Visual studio 6.0
Windows XP


魔法使いの見習い  2005-02-09 22:07:03  No: 56340

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <fstream>

const int buffer = 256;

using namespace std;

void handling();
void Description();
void ReturnPoint();
void Selection();
void Display();
void FileOutput();

int IntErro( int c );

/**** Class ****/
//Student class
class Student{
public:
  string name;

  Student(){};
  Student( string s ){SetName(s);};
  void SetName( string s ){name=s;};
  void InputName();
};

void Student::InputName(){
  string Sname;
  cout << "Please enter student's details below." << endl;
  cout << "Name   :: ";
  cin >> Sname;
  SetName(Sname);
}

// Demographic class
class StDemographic:public Student{
  int age;
  string gender;
public:
  StDemographic(){};
  StDemographic(  string Ns, int a, string s ):Student(Ns){SetAge(a);SetGender(s);};
  void SetAge( int a ){age=a;};
  void SetGender( string s ){gender=s;};
  void InputSetAge();
  void InputSetGender();
  void Display();
};

void StDemographic::InputSetAge(){
  int a;
  cout << "Age    :: ";
  cin >> a;
  SetAge(a);
}

void StDemographic::InputSetGender(){
  string g;
  cout << "Gender :: ";
  cin >> g;
  SetGender(g);
}

void StDemographic::Display(){
  cout << "Name   :: " << name << endl;
  cout << "Age    :: " << age << endl;
  cout << "Gender :: " << gender << "\n" << endl;
}

StDemographic List[100];
int TheNumOfSt = 0;

/**** Function *****/
// Description
void Description(){
  cout << "\t Description" << endl;
  cout << "@ Acknowledgement @" << endl;
  cout << "  We appreciate puchasing our program." << endl;
  cout << "  Our program is to take you to incredible views" << endl;
  cout << "@ How to use @" << endl;
  cout << "  How to use our program is that simply you just" << endl;
  cout << "  select your choice that is displayed." << endl;
}

// Screen First Page
void Screen(){
  cout << "\t *****************" << endl;
  cout << "\t * Masao program *" << endl;
  cout << "\t *****************" << endl;
  cout << endl;
  cout << "Please select one number below to begin this program." << endl;
  cout << "1 ) Description" << endl;
  cout << "2 ) Input students' details." << endl;
  cout << "3 ) Display inputed students Details." << endl;
  cout << "4 ) Exit" << endl;
}

// Char Error trap
char CharError(){
  char select;
  cin >> select;
  while ( cin.fail() ){
    while ( select != 'y' || select != 'Y' || select != 'n' || select != 'N' ){
      cin.clear();
      cin.ignore(buffer,'\n');
      cout << "You have been entered incorrect key." << endl;
      cout << "Please try again." << endl;
      cin >> select;
    }
  }
  system("cls");
  return select;
}

// Int Error trap
int IntError( int c ){
  
  cin >> c;
  system("cls");
  while ( cin.fail() ){  
    cin.clear();
    cin.ignore(buffer,'\n');
    cout << "You have been entered incorrect key." << endl;
    cout << "Please try again." << endl;
    getch();
    system("cls");
    Screen();
    cin >> c;
    system("cls");
  }
  return c;
}

// Exit Page
void ExitScreen(){
  cout << "See you again..." << endl;
}

void ReturnPoint(){
  cout << endl;
  cout << "Press ENTER KEY." << endl;
  getch();
  system("cls");
}

void DisplayDetails(){
  cout << "**** Students' details ****" << endl;
  for ( int i = 0 ; i < TheNumOfSt ; i++ ){
    List[i].Display();
  }
}

// Handling function
void Handling(){
  int i,cnt;
  int choice;
  int num;

  cout << "How many student would you like to input?" << endl; 
  cin >> num;
  TheNumOfSt = num;
  for ( i = 0 ; i < num ; i++ ){
    List[i].InputName();
    List[i].InputSetAge();
    List[i].InputSetGender();
    system("cls");
    cout << "Is this correct student's details?" << endl;
    cout << "If yes, press Y." << endl;
    cout << "If no, press N." << "\n" << endl;
    List[i].Display();
    choice = CharError();
    cnt = i;
  
    if ( choice == 'Y' || choice == 'y' ){
      if ( num != cnt + 1 ){
        cout << "Next..." << endl;
      }else{
        cout << "\t **-You have completed-**" << endl;
        cout << endl;
      }
    }else{
      i--;
      cout << "Renter..." << endl;
    }
  }
}

void Selection(){
  int c;
  while ( 1 ){
    
    Screen();
    c = IntError(c);
    system("cls");
    switch(c){
      case 1:
        Description();
        break;
      case 2:
        Handling();
        break;
      case 3:
        DisplayDetails();
        break;
      case 4:
        FileOutput();
        break;
      case 5:
        ExitScreen();
        break;
      default:
        cin.clear();
        cin.ignore(buffer,'\n');
        cout << "You have been entered incorrect key." << endl;
        cout << "Please try again." << endl;
        break;
    }
    ReturnPoint();
    if ( c == 5 ){
      break;
    }
  }
}

// File Output 
void FileOutput(){
   ofstream File("test.txt");    // File is named by programmer.
  for ( int i = 0 ; i < TheNumOfSt ; i++){
    File << List[i].Display() << endl;
  }
  cout << "You have been completed to output in text file." << endl;
}

int main(){
  Selection();
  return 0;
}


K.  2005-02-10 02:54:27  No: 56341

http://www.kumei.ne.jp/c_lang/cpp/cpp_26.htm

このへんあたり参考になりませんかね?


魔法使いの見習い  2005-02-10 21:55:04  No: 56342

ホームページありがとうございます。

とりあえず。

考えて見ます。


(noname)  2005-02-12 05:09:25  No: 56343

なかなか迫力のコードですね(笑
OK,OK.私が答えを教えてあげます。

51行目の後に魔法の呪文その1:
変更前:void Display();
変更後:void Display(), DisplayTo(ostream&);

68〜72行目に魔法の呪文その2:
変更前:
void StDemographic::Display(){
    cout << "Name   :: " << name << endl;
    cout << "Age    :: " << age << endl;
    cout << "Gender :: " << gender << "\n" << endl;
}
変更後:
void StDemographic::DisplayTo(ostream& os){
    os << "Name   :: " << name << "\n";
    os << "Age    :: " << age << "\n";
    os << "Gender :: " << gender << "\n\n";
} void StDemographic::Display(){DisplayTo(cout);}

233行目に魔法の呪文その3:
変更前: File << List[i].Display() << endl;
変更後: List[i].DisplayTo(File);


魔法使いの見習い  2005-02-17 16:46:43  No: 56344

大変ありがとうございました。

大変申し訳ありませんが説明をお願いできませんでしょうか?

プログラムを勉強してまだまもないので多少わからない事が....

お願いします。

説明してほしい箇所は...

DisplayTo(ostream&)

void StDemographic::DisplayTo(ostream& os){
    os << "Name   :: " << name << "\n";
    os << "Age    :: " << age << "\n";
    os << "Gender :: " << gender << "\n\n";
} void StDemographic::Display(){DisplayTo(cout);}←この行と

List[i].DisplayTo(File);

です。

お願いします。


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加