vectors to functions, bool key search help!!!

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;
    }

1
2
3
// Give your vector a name.
void print( vector<int>); 
void Keysearch(vector<int>);



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool Keysearch() // Missing parameter
{
  int key;
  cout << "Please enter a key: " << endl;
  cin >> key;

  // For loop is incorrect. The loop should just iterate through the vector
  // Finding the key should be done inside the loop
  for (int i =0; i < key ==true; i++)
    {
      if (key== vector <int> v) // Should be: key == v.at(i);
        key == true; // Should be "return true;", since it's a bool.
      else key == false; // I presume you meant "return false;", but it's still incorrect
    }
// return false should be AFTER the whole for loop, because if it's inside the loop
// the loop compares the first integer and if it is not the key, it would do an early stop
// If "return false" is after the loop, it'll return false after iterating through whole loop 

Last edited on
The following code solves your assignment. It might be fairly easy if you learn C++ enough.

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
75
76
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	int number;
	std::string str;
	std::stringstream ss;
	std::vector<int> numbers;

	std::ifstream inFile("data.txt");

	// Check if the file is open
	if(!inFile.is_open())
	{
		std::cerr << "The file data.txt could not be opened. Please try again." << std::endl;
		std::cin.get(); 
		return 1;
	}

	// One way to input the numbers into the vector
	while(inFile >> str)
	{
		ss << str;
		ss >> number;

		numbers.push_back(number);

		ss.str("");
		ss.clear();
	}

	// Print out the vector contents
	std::cout << "The vector loaded with " << numbers.size() << " element(s). " << std::endl;

	if("Print_out_vector_contents")
	{
		std::cout << "  ";
		for(std::vector<int>::iterator it = numbers.begin(); it != numbers.end(); it++)
		{
			std::cout << *it;
			if((it + 1) != numbers.end()) std::cout << ", ";
		}		
	}

	// Remove the last element from the vector
	numbers.erase(numbers.end() - 1);

	// Ask the user to input a key to inform the existence of the key in the vector
	int key = -(int)(0xFFFF);

	std::cout << std::endl;
	std::cout << "Enter a key : "; std::cin >> key;

	std::vector<int>::iterator it = std::find(numbers.begin(), numbers.end(), key);

	if(it != numbers.end()) 
	{
		std::cout << "Existence : true" << std::endl; 
	}
	else 
	{
		std::cout << "Existence : false" << std::endl;
	}

	std::cin.ignore();
	std::cin.get();

	return 0;
}
Topic archived. No new replies allowed.