Lab 2 homework

https://www.mediafire.com/folder/0dxuju3ek4mdb//Lab2.0

I have a lab for school above are my materials. So far I have:

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
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <playlist.h>
#include <fstream>
#include <stdlib.h>

using namespace std;

void readLine(vector<string> playlist);
int totalTime();
void displayData();

int main()
{
	vector<string> playlist;
	open("Playlist.txt");

	readLine(playlist);
	totalTime();
	displayData();

	system("pause");

	return 0;
}
void readLine(vector<string> playlist)
{
	string currentline;
	int i = 0;
	while (getline("Playlist.txt", currentline) && !empty(currentline))
	{
		getline("Playlist.txt", playlist[i]);
		i = i + 1;
	}
}
int totalTime() 
{

}
void displayData()
{

}


If someone could fix the two errors under "getline" and help me proceed I would deeply appreciate it!!!
Last edited on
Here is how you can read the file into a vector.
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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

void readLine(vector<string> &playlist);

void main(void)
{
  vector<string> input;
  
  readLine(input);
  for (int i=0; i < input.size(); i++)
    cout << input[i] << endl; 

  system("pause");
}

void readLine(vector<string> &playlist)
{
   ifstream src("Playlist.txt");

  if (!src)
  {
    cerr << "Error opening file";
    exit(EXIT_FAILURE);
  }

  string buffer;
 
  while (src)
  {
    getline(src, buffer);
    playlist.push_back(buffer);
  }
}
You should also consider using alternatives for system() in your programs.
Something like:

1
2
3
4
5
6
7
8
9
10
11
12
#include <conio.h>

int main(){
	/*
	Your code
	*/

	// Here instead of using system("pause") use:
	_getch();	// getch() is deprecated
	return 0;
}
Last edited on
@AcarX

_getch() works only on windows, system is standard.
He could use getchar() instead. There're also helper functions for linux if he doesn't want to press enter.
http://www.cplusplus.com/forum/articles/11153/
Yes getchar could be an alternative.
Topic archived. No new replies allowed.