掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
ファイルへの書き込みについて。 (ID:56340)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
#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; }
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.