⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 writeevent.cpp

📁 C++大学简明教程,解压就行没有密码。希望对大家有用
💻 CPP
字号:
// Tutorial 18: WriteEvent.cpp
// This application writes information about an event on a given
// date to a file.
#include <iostream> // required to perform C++ stream I/O
#include <string>   // required to access string functions
#include <fstream>  // required to perform C++ file I/O
#include <cstdlib>  // required for the exit function

using namespace std; // for accessing C++ Standard Library members

// function main begins program execution
int main()
{
   // define variables to store input
   string fileName;    // file name
   int date;           // event date
   string time;        // event time
   string price;       // event price
   string eventName;   // event name
   string description; // event description
   char response;      // whether user will add more events

   // prompt user for and store file name
   cout << "\nEnter file name: ";
   getline( cin, fileName );

   // ofstream constructor opens file for appending output
   ofstream outputFile( fileName.c_str(), ios::app );

   // exit program if unable to create file
   if ( !outputFile ) // overloaded ! operator
   {
      cout << "Error: File could not be opened." << endl;
      exit( 1 ); // return an error code to the system

   } // end if

   cout << "File opened successfully!" << endl;

   do // store each event that the user enters
   {
      // prompt user for and store the date, time, price, event
      // name and event description
      cout << "\nEnter the day of the event (1-31): ";
      cin >> date; // get date
      cin.ignore(); // remove newline

      cout << "Enter the event time (hh:mm): ";
      getline( cin, time ); // get time

      cout << "Enter the event price: $";
      getline( cin, price ); // get price

      cout << "Enter the event name: ";
      getline( cin, eventName ); // get event name

      cout << "Enter the event description: ";
      getline( cin, description ); // get event description

      // append user input to the file
      outputFile << date << "\n" << time  << "\n$" << price << "\n"
                 << eventName << "\n" << description << "\n";

      // ask user if more events are to be entered
      cout << "\nWould you like to enter more events "
           << "(y = yes, n = no): ";
      cin >> response; // get user's response
   } // repeat if user selected yes
   while ( response == 'y' || response == 'Y' );

   return 0; // indicates successful termination

} // end function main


/**************************************************************************
 * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 * Pearson Education, Inc. All Rights Reserved.                           *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 **************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -