Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This program reads data from a file into an vector. Then, it
- // asks the user for a number. It then compares the user number to
- // each element in the vector, displays the vector element if it is larger.
- // After looking at entire vector, it displays the number of elements in the vector
- // larger than the user number.
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- using namespace std;
- const int SIZE = 365; // Vector size
- // Function Prototype
- void numberGreaterFunction(vector<int>&, int, bool &);
- int main()
- {
- vector<int> stepsTaken; // Vector with 365 elements
- int count = 0; // Loop counter variable
- int userNumber; // User's number
- bool found = false; // Flag that signals number found
- ifstream inputFile; // Input file stream object
- // Open the file.
- inputFile.open("steps.txt");
- // Read the numbers from the file into the array.
- while (count < SIZE && inputFile >> stepsTaken[count])
- {
- stepsTaken.push_back(count);
- count++;
- }
- // Close the file.
- inputFile.close();
- // Prompt user for a number
- cout << "Please enter an integer between 1000 and 12000: ";
- cin >> userNumber;
- // Validate that the user's number is in the range of 1000 to 12000.
- while (userNumber < 1000 || userNumber > 12000)
- {
- cout << "ERROR: Enter a number between 1000 and 12000: ";
- cin >> userNumber;
- }
- numberGreaterFunction(stepsTaken, userNumber, found);
- // Display whether user number is in the vector.
- if (found)
- cout << "Your number " << userNumber << " is in the vector." << endl;
- else
- cout << "Sorry, your number " << userNumber << " is NOT in the vector." << endl;
- return 0;
- }
- //**************************************************************************************************
- //* The numberGreaterFunction searches the vector for elements than the user's number. *
- //* If an element is greater, it is displayed and the count increased. Count is displayed after all*
- //* elements are compared to userNum. If userNum is found in the vector, the flag found is *
- //* set to true. *
- //**************************************************************************************************
- void numberGreaterFunction(vector<int> &vect, int userNum, bool &found)
- {
- int numberGreater = 0; // Counter for the greater numbers
- int count = 0; // Loop counter variable
- // Display the numbers greater than the user number
- cout << "\nThe numbers greater are: " << endl;
- for (count = 0; count < SIZE; count++)
- {
- if (userNum < vect[count])
- {
- cout << vect[count] << " ";
- numberGreater += 1;
- }
- else if (userNum == vect[count])
- found = true;
- }
- cout << endl << endl;
- // Display the count of numbers greater.
- cout << "The numbers greater than your number " << userNum << " are ";
- cout << numberGreater << endl << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement