WE'RE SUPPOSED TO DO THE FOLLOWING (THIS WAS MY LAB FOR LAST WEEK AND IM NOT UNDERSTANDING IT. NOT SURE IF I SHOULD CONSIDER DROPPING MY MAJOR BUT ITS HARD FOR ME TO GRAB ONTO THE CONCEPT) I REALLY NEED HELP AND EXPLANATIONS PLEASE! I HATE ASKING FOR HELP BECAUSE I FEEL THAT I BUGG TO MUCH. FOUND THIS FORUM AND REALLY THINK THIS IS AWESOME PLEASE HELP ME !
MY CODE IS A MESS !!
I KNOW THIS IS SUPPOSED TO BE EASY BUT ITS NOT FOR ME.
Write a program that reads an unknown number of integers from a data file called “data.txt” into a vector of integers named V. V is initially empty and grows as the user reads data from file.
Once done copying data into vector V, you need to print the contents of V (write a function print that prints the contents of a vector of any size) and perform some other tasks on the vector as described below.
Your program should do the following:
1. Create an empty vector of integers V.
2. Read the integers from data.txt into V. Print its contents. You may assume data.txt contains the following numbers:
5 6 12 87 100 28 35 66 77 29
3. Remove the last element from the vector.
6. Ask the user to input a key. Then search for the key in vector V and inform the user about the existence (true / false) of the key in V.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
void print( vector<int>);
void Keysearch(vector<int>);
int main()
{
vector <int> v;
int count;
int key;
bool exist = false;
int tmp;
ifstream inputFile;
inputFile.open("data3.txt");
exist = Keysearch (v)
if(!inputFile)
{
cout << "File does not exist";
}
else
{
while (inputFile >> count)
{
//print (v);
cout << count <<" ";
v.push_back(count) ;
}
cout << endl;
inputFile.close();
}
v.pop_back();
for(int val:v)
cout << val;
return 0;
}
void print (vector<int> v)
{
}
bool Keysearch()
{
int key;
cout << "Please enter a key: " << endl;
cin >> key;
for (int i =0; i < key ==true; i++)
{
if (key== vector <int> v)
key == true;
else key == false;
}
|