Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. // This program reads data from a file into an vector. Then, it
  2. // asks the user for a number. It then compares the user number to
  3. // each element in the vector, displays the vector element if it is larger.
  4. // After looking at entire vector, it displays the number of elements in the vector
  5. // larger than the user number.
  6.  
  7. #include <iostream>
  8. #include <fstream>
  9. #include <string>
  10. #include <vector>
  11. using namespace std;
  12.  
  13. const int SIZE = 365; // Vector size
  14.  
  15. // Function Prototype
  16. void numberGreaterFunction(vector<int>&, int, bool &);
  17.  
  18. int main()
  19. {
  20. vector<int> stepsTaken; // Vector with 365 elements
  21. int count = 0; // Loop counter variable
  22. int userNumber; // User's number
  23. bool found = false; // Flag that signals number found
  24. ifstream inputFile; // Input file stream object
  25.  
  26. // Open the file.
  27. inputFile.open("steps.txt");
  28.  
  29. // Read the numbers from the file into the array.
  30. while (count < SIZE && inputFile >> stepsTaken[count])
  31. {
  32. stepsTaken.push_back(count);
  33. count++;
  34. }
  35.  
  36. // Close the file.
  37. inputFile.close();
  38.  
  39. // Prompt user for a number
  40. cout << "Please enter an integer between 1000 and 12000: ";
  41. cin >> userNumber;
  42.  
  43. // Validate that the user's number is in the range of 1000 to 12000.
  44. while (userNumber < 1000 || userNumber > 12000)
  45. {
  46. cout << "ERROR: Enter a number between 1000 and 12000: ";
  47. cin >> userNumber;
  48. }
  49.  
  50. numberGreaterFunction(stepsTaken, userNumber, found);
  51.  
  52. // Display whether user number is in the vector.
  53. if (found)
  54. cout << "Your number " << userNumber << " is in the vector." << endl;
  55. else
  56. cout << "Sorry, your number " << userNumber << " is NOT in the vector." << endl;
  57.  
  58. return 0;
  59. }
  60.  
  61. //**************************************************************************************************
  62. //* The numberGreaterFunction searches the vector for elements than the user's number. *
  63. //* If an element is greater, it is displayed and the count increased. Count is displayed after all*
  64. //* elements are compared to userNum. If userNum is found in the vector, the flag found is *
  65. //* set to true. *
  66. //**************************************************************************************************
  67.  
  68. void numberGreaterFunction(vector<int> &vect, int userNum, bool &found)
  69. {
  70. int numberGreater = 0; // Counter for the greater numbers
  71. int count = 0; // Loop counter variable
  72.  
  73. // Display the numbers greater than the user number
  74. cout << "\nThe numbers greater are: " << endl;
  75. for (count = 0; count < SIZE; count++)
  76. {
  77. if (userNum < vect[count])
  78. {
  79. cout << vect[count] << " ";
  80. numberGreater += 1;
  81. }
  82. else if (userNum == vect[count])
  83. found = true;
  84. }
  85. cout << endl << endl;
  86.  
  87. // Display the count of numbers greater.
  88. cout << "The numbers greater than your number " << userNum << " are ";
  89. cout << numberGreater << endl << endl;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement