int array not being displayed in seperate function

I am having trouble getting my streams variable to display in the viewAlbums function, I dont know why, since the code is practically identical to creating an album, any help would mean the world!

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <vector>
#include <fstream>
#include <string>

using namespace std;

ifstream uInput;
ofstream uOutput;

string album[49];
string artist[49];
int streams[49];
int s;
int removeAlbum;
int i = 0;
int x = 0;
char leaveOrStay = 'n';
char menuSelect;
char returnToMenu;
//END OF VARIABLES

    void newAlbum() {//A function to create a new album and register it into the catalog for viewing later.
        cin.ignore();
    cout << "Enter the name of your album" << endl;
    getline(cin,album[i]);
        cout << "Enter the name of the artist of " << album[i] << endl;
        getline(cin,artist[i]);
    for (x = 0;x > 50; x++){
        cout << "loop";
        album[x].size();
        album[x].compare(album[i]);
        if (album[x].compare(album[i]) == 0){
                    cout << "Album name already in catalog, please enter new album.";
                    newAlbum();
                break;
                }
            }
        i++;
}
//END OF FUNCTION
void viewAlbums(){
    cout << "# of albums in your catalog so far:\n";
    for (int n = 0; n < 49; n++){
        if (album[n].size() == 0){
        n = 50;
    }
    if (album[n].size() >= 1){
    cout << n+1 << ") " << album[n] << " by " << artist[n] << "\n        Total streams: " << streams[n] << "\n";
        if (n >= 50){
            break;
        }
    }
    }
    cout << "Press any key to return to the menu." << endl;
    cin >> returnToMenu;
    }
//END OF FUNCTION
void editList(){
    cout << "Enter the number of the album you want removed from your list." << endl;
    
    for (int e = 0; e < 49; e++){
           if (album[e].size() == 0){
           e = 50;
       }
       if (album[e].size() >= 1){
       cout << e+1 << ") " << album[e] << " by " << artist[e] << endl;
           if (e > 49){
               break;
           }
       }
       }
    cin >> removeAlbum;
    removeAlbum--;
    for (int e = 0; e < 49; e++){
        if (album[removeAlbum].size() > 0){
        ;
    }
        }
        album[removeAlbum] = "";
        artist[removeAlbum] = "";
    }
    


//END OF FUNCTION
void editStreams(){
    cout << "Select the album you want to add more streams to by entering its placement in the list." << endl;
    for (int n = 0; n < 49; n++){
         if (album[n].size() == 0){
         n = 50;
     }
     if (album[n].size() >= 1){
         cout << n+1 << ") " << album[n] << " by " << artist[n] << "\n        total streams: " << streams[n] << "\n";
         if (n >= 50){
             break;
         }
     }
     }
    cin >> s;
    cout << "Now, how many times have you streamed this album?" << endl;
    cin >> streams[s];
    cout << "\n" << streams[s];
    
}
int main(int argc, const char * argv[]) {
    while (leaveOrStay == 'N' || 'n'){
uInput.open("Albums.rtf");

        cout << "Hi cpp.com! I removed some ascii art so the code is easier to read, press A to create an album, V to view all your albums youve created, and S to edit your streams for all your albums. Removing albums is broken and ill fix it myself, bye!";
        cin >> menuSelect;
    if (menuSelect == 'A' || menuSelect == 'a'){
        newAlbum();
    }
    else if (menuSelect == 'V' || menuSelect == 'v'){
        viewAlbums();
    }
    else if (menuSelect == 'R' || menuSelect == 'r'){
        editList();
    }
    else if (menuSelect == 'S' || menuSelect == 's'){
        editStreams();
    }
}
    
}
Last edited on
Your indentation within your functions/loops/if-statements is misleading/inconsistent, and might be part of what led to the problem here.

1
2
3
4
5
6
for (int n = 0; n < 49; n++){
    if (album[n].size() == 0){
        n = 50;
    }
    if (album[n].size() >= 1) {
        // ... 


If that first if-statement is entered, the on the second if-statement's evaluation, you are going out of bounds of your album. You should fix this. What specifically is the behavior your are experiencing, and how is it different than the expected behavior?
Last edited on
As a first refactor, possibly something like:

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <fstream>
#include <string>

constexpr size_t MaxCat {50};

struct Cat {
	std::string album;
	std::string artist;
	int streams {};
};

std::ostream& operator<<(std::ostream& os, const Cat& c) {
	return os << c.album << " by " << c.artist << " streamed " << c.streams;
}

void newAlbum(Cat catalog[MaxCat]) {
	Cat entry;
	bool fnd {};
	size_t pos {};

	do {
		fnd = false;
		pos = 0;
		std::cout << "Enter the name of your album: ";
		std::getline(std::cin, entry.album);

		std::cout << "Enter the name of the artist of " << entry.album << ": ";
		std::getline(std::cin, entry.artist);

		for (size_t x {}; x < MaxCat; ++x)
			if (!fnd && (catalog[x].album == entry.album)) {
				std::cout << "Album name already in catalog. Please enter new album.\n";
				fnd = true;
			} else
				if (pos == 0 && catalog[x].album.empty())
					pos = x + 1;
	} while (fnd);

	if (pos != 0)
		catalog[pos - 1] = entry;
	else
		std::cout << "Catalog full!\n";
}

void viewAlbums(const Cat catalog[MaxCat]) {
	std::cout << "# of albums in your catalog so far:\n";

	for (size_t i {}, n {}; i < MaxCat; ++i)
		if (!catalog[i].album.empty())
			std::cout << ++n << ") " << catalog[i] << "\n";
}

void editList(Cat catalog[MaxCat]) {
	std::string album;

	std::cout << "Enter the name of the album you want removed from your list: ";
	std::getline(std::cin, album);

	for (size_t i {}; i < MaxCat; ++i)
		if (catalog[i].album == album) {
			catalog[i].album.clear();
			return;
		}

	std::cout << "Album not found!\n";
}

void editStreams(Cat catalog[MaxCat]) {
	size_t s {};

	do {
		std::cout << "Select the album you want to add more streams to by entering its placement in the list:\n";

		for (size_t n {}; n < MaxCat; ++n) {
			if (!catalog[n].album.empty())
				std::cout << n + 1 << ") " << catalog[n] << "\n";
		}
		std::cout << "Enter placement: ";

	} while ((std::cin >> s) && (--s >= MaxCat || catalog[s].album.empty()) && (std::cout << "Invalid placement\n"));

	std::cout << "How many times have you streamed this album: ";
	std::cin >> catalog[s].streams;
}

int main() {
	/*
	std::ifstream uInput("Albums.rtf");

	if (!uInput)
		return (std::cout << "Cannot open file\n"), 1;
	*/

	char menuSelect {};
	Cat catalog[MaxCat] {};

	while (menuSelect != 'e' && menuSelect != 'E') {
		std::cout << "\n'A' to create an album\n" <<
			"'V' to view all created albums\n" <<
			"'S' to edit album streams\n" <<
			"'R' to remove album\n" <<
			"'E' to exit\n" <<
			"\nEnter option: ";

		std::cin >> menuSelect;
		std::cin.ignore();

		if (menuSelect == 'A' || menuSelect == 'a')
			newAlbum(catalog);
		else if (menuSelect == 'V' || menuSelect == 'v')
			viewAlbums(catalog);
		else if (menuSelect == 'R' || menuSelect == 'r')
			editList(catalog);
		else if (menuSelect == 'S' || menuSelect == 's')
			editStreams(catalog);
		else if (menuSelect != 'E' && menuSelect != 'e')
			std::cout << "Invalid option\n";
	}
}

Topic archived. No new replies allowed.