[英]Array of structures program has unknown compiling or segmantation error, c++
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.我正在编写这个包含结构的程序,该程序在 repl.it ide 中的第一次迭代后工作然后崩溃,并在我的 cygwin 命令行中运行 2-3 次。 I just started using c++ so I don't see anything immediately but I believe the syntax is correct.我刚开始使用 c++,所以我没有立即看到任何东西,但我相信语法是正确的。 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包含文本文件的 repl.it 链接: https ://repl.it/@ValerieAndy/PrWorkingwStructs
sorry if this is the wrong way to ask a question I'm new here too.抱歉,如果这是问问题的错误方式,我也是新来的。
Your current code fails at您当前的代码失败于
HR_PL hr[12]; //making 12 instances for our array of structs (12 hours)
because因为
struct HR_PL{
string name;
int count;
char songs[];
};
is, as πάντα ῥεῖ points out, invalid.正如 πάντα ῥεῖ 指出的那样,是无效的。
char songs[];
cannot be initialized.无法初始化。
If you are trying to store a c-string in HR_PL
, you'd need a char*
, or preferably a std::string
instead.如果您尝试在HR_PL
存储 c 字符串, HR_PL
需要char*
,或者最好是std::string
。 If you are trying to store an array of strings, you should preferably be using std::vector<std::string>
.如果您尝试存储字符串数组,则最好使用std::vector<std::string>
。
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[];
你展示的代码不应该编译,因为它不是有效的 C++,但即使它是有效的,你也会因为灵活的数组成员char songs[];
遇到麻烦char songs[];
that you don't allocate space for.您没有为其分配空间。
Here's a version using std::vector
for arrays and <random>
for random numbers.这是一个使用std::vector
数组和<random>
随机数的版本。 I've also removed using namespace std;
我还删除了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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.