[英]Array of structures program has unknown compiling or segmantation error, c++
我正在編寫這個包含結構的程序,該程序在 repl.it ide 中的第一次迭代后工作然后崩潰,並在我的 cygwin 命令行中運行 2-3 次。 我剛開始使用 c++,所以我沒有立即看到任何東西,但我相信語法是正確的。 該程序將歌曲列表保存在一個空文本文件中,但也將歌曲保存到一個空數組中,以便我以后可以參考它。
#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 鏈接: https ://repl.it/@ValerieAndy/PrWorkingwStructs
抱歉,如果這是問問題的錯誤方式,我也是新來的。
您當前的代碼失敗於
HR_PL hr[12]; //making 12 instances for our array of structs (12 hours)
因為
struct HR_PL{
string name;
int count;
char songs[];
};
正如 πάντα ῥεῖ 指出的那樣,是無效的。
char songs[];
無法初始化。
如果您嘗試在HR_PL
存儲 c 字符串, HR_PL
需要char*
,或者最好是std::string
。 如果您嘗試存儲字符串數組,則最好使用std::vector<std::string>
。
你展示的代碼不應該編譯,因為它不是有效的 C++,但即使它是有效的,你也會因為靈活的數組成員char songs[];
遇到麻煩char songs[];
您沒有為其分配空間。
這是一個使用std::vector
數組和<random>
隨機數的版本。 我還刪除了using namespace std;
因為這樣做是不好的做法。
#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.