コンパイルする時にどうしてもエラーになって、
ファイルへの書き込みがどうしてもできません。
宣言の仕方の間違えがあれば教えてください。
お願いします。
Visual studio 6.0
Windows XP
#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;
}
http://www.kumei.ne.jp/c_lang/cpp/cpp_26.htm
このへんあたり参考になりませんかね?
ホームページありがとうございます。
とりあえず。
考えて見ます。
なかなか迫力のコードですね(笑
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);
大変ありがとうございました。
大変申し訳ありませんが説明をお願いできませんでしょうか?
プログラムを勉強してまだまもないので多少わからない事が....
お願いします。
説明してほしい箇所は...
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);
です。
お願いします。
ツイート | ![]() |