Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. // Because the quiz specifies that all the functions should not return a value, we use
  7. // reference variables when the larger program needs to use the work done inside a function.
  8. // Otherwise, we pass by value to make debugging easier.
  9.  
  10. // These functions get input that is used by other functions, so the functions need to
  11. // write to those variables. They are passed by reference.
  12. void validateUserName(string &userName);
  13. void validateUserInput(string &userInput);
  14.  
  15. // The output file has to be available to the function that actually writes
  16. // data into the file, so outputFile is passed by reference.
  17. void createFile(string userName, ofstream &outputFile);
  18.  
  19. // My compiler required outputfile to be a reference variable. I thought maybe I could get away
  20. // with passing the file object by value and still have the function write to the actual file,
  21. // but I guess it doesn't work that way.
  22. void writeDataToFile(string userInput, ofstream &outputfile);
  23.  
  24. // We are only reading from the input File so userName (which is the filename on disk)
  25. // can be passed by value. The ifstream object is instantiated inside this function,
  26. // all we need back is the contents of the file as a string.
  27. void readDataFromFile(string &fileData, string userName);//read from the file
  28.  
  29. // These functions don't modify the fileData, but they will create the strings containing
  30. // how many even digits and odd digits there are. So fileData is passed by value and
  31. // the digits to be printed are passed by reference.
  32. void checkEvenDigit(string fileData, string &evenDigits);
  33. void checkOddDigit(string fileData, string &oddDigits);
  34.  
  35. // Other functions generate the strings to output, this function is only the statements
  36. // to output to the screen, so they only need to be passed by value.
  37. void displayResults(string evenDigits, string oddDigits);
  38.  
  39. int main() {
  40.  
  41. // User input
  42. // userName will also be the filename of the file we write and read to
  43. string userName, userInput;
  44. // Data we'll read back from the output file
  45. string fileData;
  46. // Even and odd digits to be printed
  47. string evenDigits, oddDigits;
  48.  
  49. // The output filestream object we use to write the output file
  50. ofstream outputFile;
  51. // The input filestream object we use to read back from the output file
  52. ifstream inputfile;
  53.  
  54. // Get input and validate it
  55. validateUserName(userName);
  56. validateUserInput(userInput);
  57.  
  58. // Create, open, and write to the output file
  59. createFile(userName, outputFile);
  60. writeDataToFile(userInput, outputFile);
  61.  
  62. // Read the data back from the file we just wrote
  63. readDataFromFile(fileData, userName);
  64.  
  65. // Get even and odd digits
  66. checkEvenDigit(fileData, evenDigits);
  67. checkOddDigit(fileData, oddDigits);
  68.  
  69. //Output the information about even and odd digits
  70. displayResults(evenDigits, oddDigits);
  71.  
  72. return 0;
  73. }
  74.  
  75. void validateUserName(string &userName){
  76. int counter;
  77. unsigned long int userNameLength;
  78. bool userNameIsValid;
  79.  
  80. // Loop to read user input
  81. do {
  82. cout << "Please enter your name: ";
  83. getline(cin, userName);
  84. userNameLength = userName.length();
  85. userNameIsValid = true;
  86. counter = 0;
  87.  
  88. // Someone's name can have all kinds of letters, punctuation, and
  89. // and spaces ("Martin Luther King, Jr."), but there shouldn't be any numbers
  90. // ("Paco Ignacio Taibo II", not "Paco Ignacio Taibo the 2nd") or newlines.
  91. // Nested loop to check each character from input and make sure it's not a number
  92. while (counter < userNameLength && userNameIsValid == true){
  93. if (isnumber(userName[counter])) {
  94. cout << "Sorry, that does not appear to be a real name." << endl;
  95. userNameIsValid = false;
  96. }
  97. counter++;
  98. }
  99. } while (userNameIsValid == false);
  100. }
  101.  
  102. void validateUserInput(string &userInput){
  103. string currentLine;
  104. bool sentinelEncountered = false;
  105.  
  106. cout << "Now you can enter any text you want (including whitespace), but I'm "
  107. "going to stop reading when you type \"-1\" on its own line. Also, "
  108. "your input can't just be blank besides \"-1\":";
  109.  
  110. // When the sentinel "-1" is encountered, the boolean is set to end the loop.
  111. do {
  112. getline(cin, currentLine);
  113. if (currentLine == "-1") {
  114. sentinelEncountered = true;
  115. } else {
  116. userInput.append(currentLine);
  117. userInput.append("\n");
  118. }
  119.  
  120. // check to make sure input wasn't blank. Counts whitespace, so if a user just
  121. // enters a few newlines or tabs, the program will proceed.
  122. if (userInput.empty()){
  123. cout << "Your input cannot be blank, it was pretty much the only rule "
  124. "I gave you. Please enter some text, and I'll stop reading when "
  125. "you type \"-1\" on its own line:";
  126. sentinelEncountered = false;
  127. }
  128.  
  129. } while (sentinelEncountered == false);
  130.  
  131. }
  132.  
  133. void createFile(string userName, ofstream &outputFile){
  134. outputFile.open(userName);
  135. }
  136.  
  137. void writeDataToFile(string userInput, ofstream &outputFile){
  138. outputFile << userInput;
  139. outputFile.close();
  140. }
  141.  
  142. void readDataFromFile(string &fileData, string userName){
  143. ifstream inputFile;
  144. string currentLine;
  145.  
  146. inputFile.open(userName);
  147.  
  148. while ( inputFile >> currentLine){
  149. fileData.append(currentLine);
  150. }
  151.  
  152. inputFile.close();
  153. }
  154.  
  155. void checkEvenDigit(string fileData, string &evenDigits){
  156. unsigned long int fileDataLength = fileData.length();
  157. unsigned long int counter;
  158. unsigned long int evenCount = 0;
  159. unsigned short int digit;
  160. string digitAsString;
  161.  
  162.  
  163. for (counter = 0; counter < fileDataLength; counter++){
  164. // Only try to get evenness if we're dealing with an actual integer
  165. if (isnumber(fileData[counter])) {
  166. digit = fileData[counter];
  167. // If a number is even, add it to our string we'll eventually output
  168. if (digit % 2 == 0){
  169. // You can't use string.append() with an integer, so we have to make it a string
  170. // static_cast<string>(var) doesn't cast to a string, so this is what I came up with
  171. digitAsString = digit;
  172. evenCount++;
  173. // If this is the first even digit encountered, add some explanatory text to the line
  174. if (evenCount == 1) evenDigits.append("Even digits: ");
  175. evenDigits.append(digitAsString);
  176. evenDigits.append(" ");
  177. }
  178. }
  179. }
  180.  
  181. // If there are no even digits, use a literal output string
  182. if (evenCount == 0) evenDigits = "There are no even digits.";
  183. }
  184.  
  185. void checkOddDigit(string fileData, string &oddDigits){
  186. unsigned long int fileDataLength = fileData.length();
  187. unsigned long int counter;
  188. unsigned long int oddCount = 0;
  189. unsigned short int digit;
  190. string digitAsString;
  191.  
  192.  
  193. for (counter = 0; counter < fileDataLength; counter++) {
  194. // Only try to get oddness if we're dealing with an actual integer
  195. if (isnumber(fileData[counter])) {
  196. digit = fileData[counter];
  197. // If a number is odd, add it to our string we'll eventually output
  198. if (digit % 2 == 1) {
  199. oddCount++;
  200. // You can't use string.append() with an integer, so we have to make it a string
  201. // static_cast<string>(var) doesn't cast to a string, so this is what I came up with
  202. digitAsString = digit;
  203. // If this is the first odd digit encountered, add some explanatory text to the line
  204. if (oddCount == 1) oddDigits.append("Odd digits: ");
  205. oddDigits.append(digitAsString);
  206. oddDigits.append(" ");
  207. }
  208. }
  209. }
  210.  
  211. // If there are no odd digits, use a literal output string
  212. if (oddCount == 0) oddDigits = "There are no odd digits.";
  213. }
  214.  
  215. void displayResults(string evenDigits, string oddDigits){
  216. cout << evenDigits << endl;
  217. cout << oddDigits << endl;
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement