-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsong.cpp
77 lines (60 loc) · 1.18 KB
/
song.cpp
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
77
// song.cpp
#include <iostream>
#include "song.h"
#include <cstring>
using namespace std;
// ignore any newlines in input
void IgnoreNewLines()
{
while (cin.peek() == '\n')
cin.ignore();
}
// default constructor
Song::Song()
{
strcpy_s
(title, "");
strcpy_s(artist, "");
}
Song::Song(const char* t, const char* a)
{
Set(t, a);
}
void Song::Set(const char* t, const char* a)
{
if (strlen(t) < MAX_CHARS)
strcpy_s(title, t);
if (strlen(a) < MAX_CHARS)
strcpy_s(artist, a);
}
// output operator
ostream& operator<<(ostream& os, const Song& song)
{
os << song.title << ", " << song.artist;
return os;
}
// input operator
istream& operator>>(istream& is, Song& song)
{
// ignore any newlines
IgnoreNewLines();
// read until the comma
cin.getline(song.title, Song::MAX_CHARS, ',');
// ignore the space in between
if (cin.peek() == ' ')
cin.ignore();
// ignore any newlines
IgnoreNewLines();
// read until new line
cin.getline(song.artist, Song::MAX_CHARS);
return is;
}
// equality test operator
bool operator==(const Song& lhs, const Song& rhs)
{
if (strcmp(lhs.title, rhs.title))
return false;
if (strcmp(lhs.artist, rhs.artist))
return false;
return true;
}