quantumech

Untitled

Aug 8th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. /**
  2.  * This example code does the following:
  3.  *      1) Asks the user for his/her name
  4.  *      2) Gets name that the user inputted
  5.  *      3) Prints the name back to the user
  6.  *      4) Opens a text file, 'example-text-file.txt', containing the text 'That name is stupid'
  7.  *      5) Prints text from text file
  8.  */
  9.  
  10. #include <iostream>
  11. #include <fstream>
  12. #include <string>
  13. #include "AdaptedConsole.hpp"
  14.  
  15. // In order for the library to work, you must use adaptedConsole::cin instead of std::cin
  16. using adaptedConsole::cin;
  17. // You can use the standard library for everything else though
  18. using std::cout;
  19. using std::endl;
  20. using std::fstream;
  21. using std::string;
  22. using std::getline;
  23.  
  24.  
  25. int main()
  26. {
  27.     // Prompt user for his/her name and use Emscripten-Console's 'cin' to get user input
  28.     cout << "What is your name? " << endl;
  29.     string name;
  30.     cin >> name;
  31.  
  32.     // Print the users name
  33.     cout << "Your name is: " << name << endl;
  34.  
  35.     // Instantiate handle to 'example-text-file.txt'
  36.     // PREFACE ALL PATHS WITH '/' OR FILE HANDLING WON'T WORK
  37.     fstream textFile = fstream("/example-text-file.txt");
  38.  
  39.     // Get first line of text from 'example-text-file.txt'
  40.     string text;
  41.     getline(textFile, text);
  42.  
  43.     // Print text from text file
  44.     cout << text << endl;
  45.  
  46.     // Close file like a responsable human being
  47.     fclose(textFile);
  48. }
Add Comment
Please, Sign In to add comment