function header help??!?!?!?!?

We're supposed to make a print function where we are 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).
I don't know where to put the function header???? like i did the prototype but where do i place the function header??? and how do you know where to place it ?

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
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

void print(int v);

int main()
{
  vector <int> v;
  int count;
  int key;
  bool exist = false;
  int tmp;


    ifstream inputFile;
    inputFile.open("data3.txt");

    if(!inputFile)
      {

        cout << "File does not exist";
      }

    else
      {

        while (inputFile >> count)

          {

            cout << count <<" ";
            v.push_back(count) ;

          }
        cout << endl;
        inputFile.close();


      }





    return 0;
}
Last edited on

I don't know where to put the function header???? like i did the prototype but where do i place the function header??? and how do you know where to place it ?

I think what you mean is the function definition.

void print(int v);
This is a function prototype/declaration. It tells C++ that there will be a function called print taking an argument of type int and returning nothing.

1
2
3
4
void print(int v)
{
    // ...
}

This is a function definition, where you will code the body of the function (what it does).

As for where to put the function definition, you can do it this way
1
2
3
4
5
6
7
8
9
10
11
12
void print(int v);

int main()
{
    // ...
    print(/*...*/)
}

void print(int v)
{
    // ...
}


OR

1
2
3
4
5
6
7
8
9
10
void print(int v)
{
    // ...
}

int main()
{
    // ...
    print(/*...*/)
}
Now im struggling trying to output the vector in the function. please help!! i hate not knowing what to do :/

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
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

void print( vector<int>);

int main()
{
  vector <int> v;
  int count;
  int key;
  bool exist = false;
  int tmp;
  print(vector<int> v);
  ifstream inputFile;
  inputFile.open("data3.txt");

    if(!inputFile)
      {

        cout << "File does not exist";
      }

    else
      {


        while (inputFile >> count)

          {
                print (v);

                //cout << count <<" ";                                                                                                                                               
                //v.push_back(count) ;                                                                                                                                               

          }
        cout << endl;
        inputFile.close();

      }
    return 0;
}
    void print (vector<int> v)
    {

      for (int count = 0; count < v.push_back(); count++)
        {
          cout << v[count] << endl;
        }
}
Last edited on
for (int count = 0; count < v.push_back(); count++)
I think you mean v.size().

Line 16:
What are you trying to do here?

Line 30:
Your logic is incorrect here. What you had before was correct. You want to read all the data in your file into your vector, then print out all the data in the vector. What you're doing right now is printing the values of an empty vector.
By the way, you don't need a variable to keep track of the number of elements(count). vectors already do that via the member function size().

Thanks guys I got it to output :
5 6 12 87 100 28 35 66 77 29
5 6 12 87 100 28 35 66 77
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


#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

void print (vector<int>);

int main()
{
  vector <int> v;
  int count;
  int key;
  int tmp;
  int s;
  ifstream inputFile;
  inputFile.open("data3.txt");

    if(!inputFile)
      {
        cout << "File does not exist";
      }
    else
      {
        while (inputFile >> count)
          {
            v.push_back(count) ;
          }
        inputFile.close();
      }

    print(v);

    v.pop_back();

    print(v);

    return 0;
}


void print (vector <int> v)
{
  for (int i=0; i < v.size(); i++)
    {
      cout << v[i] << " ";
    }
  cout << endl;
}



Topic archived. No new replies allowed.