getline c++ 的问题
Problems with getline c++
我已经搜索了几个小时了,但还没有遇到我的问题。我正在从文件中读取第一行是电影的数量,然后是电影名称、导演等。
示例:
2
Friday Night Lights: Upcoming;Bond, James;2061;128;93000000;871530324
Friday Night Lights: Upcoming;Bond, James;2061;128;93000000;871530324
但我似乎无法让 getline 函数正常工作。我已经尝试了几种不同的方法,但它不起作用。
struct MovieData{
string movieTitle;
string director;
int yearReleased;
double runningTime;
double costOfProduction;
double revenueGenerated;
double profitOrLoss;
};
void storingMovieData(ifstream file, MovieData array[], int numberOfMovies);
int main(){
char fileName[50];
MovieData array[];
cout << "Enter file name: ";
cin.getline(fileName, 50);
int numberOfMovies;
ifstream file(fileName);
file >> numberOfMovies;
storingMovieData(file, array, numberOfMovies)
file.close();
return 0;
}
void storingMovieData(ifstream file, MovieData array[], int numberOfMovies){
for(int i = 0; i < numberOfMovies; i++){
getline(file, array[i].movieTitle, ';');
getline(file, array[i].director, ';');
getline(file, array[i].yearReleased, ';');
getline(file, array[i].runningTime, ';');
getline(file, array[i].costOfProduction, ';');
getline(file, array[i].revenueGenerated, ';');
getline(file, array[i].profitOrLoss, ';');
}
}
非常感谢对我的问题的任何帮助!
这些是您问题的根源:
int yearReleased;
double runningTime;
double costOfProduction;
double revenueGenerated;
double profitOrLoss;
getline()
不接受 double/int.
您可能希望读入一些临时字符串,然后在转换为双精度后分配给结构。
我已经搜索了几个小时了,但还没有遇到我的问题。我正在从文件中读取第一行是电影的数量,然后是电影名称、导演等。
示例:
2
Friday Night Lights: Upcoming;Bond, James;2061;128;93000000;871530324
Friday Night Lights: Upcoming;Bond, James;2061;128;93000000;871530324
但我似乎无法让 getline 函数正常工作。我已经尝试了几种不同的方法,但它不起作用。
struct MovieData{
string movieTitle;
string director;
int yearReleased;
double runningTime;
double costOfProduction;
double revenueGenerated;
double profitOrLoss;
};
void storingMovieData(ifstream file, MovieData array[], int numberOfMovies);
int main(){
char fileName[50];
MovieData array[];
cout << "Enter file name: ";
cin.getline(fileName, 50);
int numberOfMovies;
ifstream file(fileName);
file >> numberOfMovies;
storingMovieData(file, array, numberOfMovies)
file.close();
return 0;
}
void storingMovieData(ifstream file, MovieData array[], int numberOfMovies){
for(int i = 0; i < numberOfMovies; i++){
getline(file, array[i].movieTitle, ';');
getline(file, array[i].director, ';');
getline(file, array[i].yearReleased, ';');
getline(file, array[i].runningTime, ';');
getline(file, array[i].costOfProduction, ';');
getline(file, array[i].revenueGenerated, ';');
getline(file, array[i].profitOrLoss, ';');
}
}
非常感谢对我的问题的任何帮助!
这些是您问题的根源:
int yearReleased;
double runningTime;
double costOfProduction;
double revenueGenerated;
double profitOrLoss;
getline()
不接受 double/int.
您可能希望读入一些临时字符串,然后在转换为双精度后分配给结构。