Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <thread>
- #include <chrono>
- #include <fstream>
- #include <vector>
- #include <stdlib.h>
- // Name space resolutions
- using namespace std ;
- //prototypes
- void Menu(int &);
- // Forward declare the slow_print function
- void slow_print(string message , unsigned int);
- // Main function
- int main()
- {
- int choice = 0;
- string filename ;
- string mystring ;
- Menu(choice) ;
- do
- {
- switch(choice)
- {
- case 1:
- // begin block statement
- {
- // Due to the way that cin works we have to clear the input first
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- // if we dont the above then prompt prints twice first time round
- do{
- cout <<"\nPlease enter name of file to read or enter M to return to menu : " ;
- getline(cin, filename);
- }while (filename.length()==0) ;
- if (filename.compare("M")!=0)
- {
- ifstream myfile(filename);
- if(!myfile) //Always test the file open.
- {
- cout<<"Error opening file"<< endl;
- return -1 ;
- }
- while (std::getline(myfile, mystring)) {
- // output the line:
- slow_print(mystring, 80);
- }
- myfile.close() ;
- }
- // for unix / linux systems only
- system("clear");
- break;
- } //end block statements
- case 2:
- break ;
- default:
- // for unix / linux systems only
- system("clear");
- cout << "\nInvalid input Please re enter " ;
- }
- // call menu again
- Menu(choice) ;
- }while(choice!=2) ;
- cout << "Exiting menu thankyou for using this " << endl ;
- return (EXIT_SUCCESS) ;
- }
- // Implement the slow_print function
- /**
- * Function to print each character in a string with a delay (a "typewriter" effect)
- * @param message The string to print
- * @param millis_per_char Milliseconds to take to print each character
- */
- void slow_print(string message, unsigned int millis_per_char)
- {
- const std::string red("\033[0;31m");
- const std::string green("\033[1;32m");
- const std::string yellow("\033[1;33m");
- const std::string cyan("\033[0;36m");
- const std::string magenta("\033[0;35m");
- const std::string reset("\033[0m");
- // Range loops are "for each" constructs; here: for each character in the string
- for (const char c: message)
- {
- // flush is used to make sure the buffer is emptied to the terminal immediately
- cout << cyan << c << reset << flush;
- // Ask the thread to sleep for at least n millis.
- this_thread::sleep_for(std::chrono::milliseconds(millis_per_char));
- }
- }
- void Menu(int &choice)
- {
- cout << "\n Choose an option:";
- cout << "\n...................................";
- cout << "\n 1- Instructions ";
- cout << "\n 2- Exit ";
- cout << "\n\nEnter your choice: ";
- cin >> choice ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement