I'm writing this program that includes structs, the program works then crashes after the first iteration in repl.it ide, and runs 2-3 times in my cygwin command line. I just started using c++ so I don't see anything immediately but I believe the syntax is correct. The program saves song list in an empty text file, but also saves the songs into an empty array so I could possibly reference it later.
#include<cstdlib> //for random function
#include<ctime> //for random function
#include<string>
#include<fstream>
#include<sstream> //for int to str function
#include<iostream>
using namespace std;
struct HR_PL{
string name;
int count;
char songs[];
};
string intToString(int a);
int main() {
HR_PL hr[12]; //making 12 instances for our array of structs (12 hours)
//datatype arrayName[no of elements]
char song_list[12] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'k'};
int rand_for_pl;
char user_response;
fstream likeFile;
for (int i = 0; i < 12; i++) {
hr[i].count = 0;
//hr[i].songs[10]; Array is created in HR_PL
cout << "\nHour " << i+1 << " playlist: " << endl;
hr[i].name = intToString(i+1);
for (int j = 0; j < 10; j++) {
rand_for_pl = rand() % 12;
cout << song_list[rand_for_pl];
cout << ",";
hr[i].songs[j] = song_list[rand_for_pl];
}
cout << endl << endl;
cout << "Did you like the playlist? ";
cin >> user_response;
if (user_response == 'y') {
likeFile.open("like.txt", ios::app); //Create the output file to append
cout << "Great! We have saved the playlist for you." << endl;
if (likeFile.is_open()) {
likeFile << "Hour " << i+1 << " Playlist: ";
for(int s = 0; s < 10; s++){
likeFile << hr[i].songs[s];
likeFile << " ";
}
likeFile << "\n";
}
likeFile.close();
}
else {
cout << "Sorry! We hope you will like the upcoming playlist." << endl;
}
}//endfor
return 0;
}//endmain
string intToString(int a){
ostringstream temp;
temp << a;
return temp.str();
};
repl.it link that has text file: https://repl.it/@ValerieAndy/PrWorkingwStructs
sorry if this is the wrong way to ask a question I'm new here too.
The code you showed should not compile since it's not valid C++, but even if it was valid, you'd be in trouble because of the flexible array member char songs[];
that you don't allocate space for.
Here's a version using std::vector
for arrays and <random>
for random numbers. I've also removed using namespace std;
since doing that is a bad practice.
#include <iostream>
#include <fstream>
#include <string>
#include <random> // for random functions
#include <vector> // instead of using C style arrays
struct HR_PL {
std::string name{};
// "count" is not needed, use "songs.size()" instead
std::vector<std::string> songs{};
};
int main() {
std::random_device rd; // used for seeding the pseudo random number generator (PRNG)
std::mt19937 generator(rd()); // A better PRNG than rand()
std::vector<HR_PL> hr(12); // making 12 instances for our array of structs (12 hours)
// datatype arrayName[no of elements]
std::vector<std::string> song_list = {"a", "b", "c", "d", "e", "f",
"g", "h", "j", "k", "l", "k"};
// the distribution of the values generated by the PRNG
std::uniform_int_distribution<uint32_t> song_dist(0, song_list.size() - 1);
// the distribution and generator placed in a lambda for convenience
auto random_song = [&]() { return song_dist(generator); };
int rand_for_pl;
char user_response;
for(int i = 0; i < 12; i++) {
std::cout << "\nHour " << i + 1 << " playlist:\n";
hr[i].name = std::to_string(i + 1);
for(int j = 0; j < 10; j++) {
rand_for_pl = random_song();
std::cout << song_list[rand_for_pl];
std::cout << ",";
// adding songs to the vector can be done using "push_back"
hr[i].songs.push_back(song_list[rand_for_pl]);
}
std::cout << "\n\nDid you like the playlist? ";
std::cin >> user_response;
if(user_response == 'y') {
// Create the output file or append to it:
std::fstream likeFile("like.txt", std::ios::app);
// consider moving the below output until the playlist is actually saved
std::cout << "Great! We have saved the playlist for you.\n";
if(likeFile) { // in bool context, likeFile is true if opened
likeFile << "Hour " << i + 1 << " Playlist: ";
for(int s = 0; s < 10; s++) {
likeFile << hr[i].songs[s];
likeFile << " ";
}
// the loop above would be better written like this:
/*
for(const std::string& song : hr[i].songs) {
likeFile << song << " ";
}
*/
likeFile << "\n";
}
// likeFile closes when it goes out of scope so you don't have to
} else {
std::cout << "Sorry! We hope you will like the upcoming playlist.\n";
}
} // endfor
return 0;
} // endmain