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!
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
usingnamespace 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, constchar * 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();
}
elseif (menuSelect == 'V' || menuSelect == 'v'){
viewAlbums();
}
elseif (menuSelect == 'R' || menuSelect == 'r'){
editList();
}
elseif (menuSelect == 'S' || menuSelect == 's'){
editStreams();
}
}
}
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?