calling function into main.

hi i cant figure out how to call a function into main
this is the function-
im passing a vector over to this function and then trying to call it in main
void sortfile(const std::vector<std::string>& lines)

any help is appreciated thanks
#include <iostream>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <vector>
#include <string>

void sortfile(const std::vector<std::string>& lines);

int main()
{
  std::vector<std::string> lines = {"Debbie", "Anna", "Lisa", "Kathy"};

  // calling the sort function
  sortfile(lines);
  system("pause");
  return 0;
}


Be aware that you can't sort a const vector
Last edited on
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
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include <string>
#include<iterator>

void loadfile()
{
	std::ifstream file("weblog.txt");
	if (file.is_open())
	{
		std::cout << "File found!" << std::endl;
	}
	else std::cout << "File NOT found!" << std::endl;
	std::vector<std::string> lines;
	std::string loading;

	while (std::getline(file, loading))
	{
		lines.push_back(loading);
	}
	
}

void sortfile(std::vector<std::string>& lines)
	{
	
		std::sort(lines.begin(), lines.end());
		std::ofstream NewLog("SortedWeblog.txt");
		for (int i = 0; i < lines.size(); i++)
		{
			NewLog << lines[i] << std::endl;
		}
	}


int main()
{
	loadfile();
	std::vector<std::string> lines;
	std::string loading;
	lines.push_back(loading);
	sortfile(lines);
	system("pause");
	return 0;
}




ok im getting a blank text file...i had it working using only one function but i cant do that, need to use two functions

this whole thing is supposed to take in a text file,put it in a vector, sort it and then write the sorted files into a text file
Last edited on
You should pass the vector to the loadfile function. At the moment you read the file into a temp variable that gets lost when the function ends.
thanks! that worked
Topic archived. No new replies allowed.