我的函数inputData存在一种奇怪的问题。对于第一部电影的输入来说,它工作正常。但是,在制作第二部电影时,它会跳过询问您输入第二部电影的名称的过程,直接提示您输入第二部电影的销售额。电影的名称必须能够包含空格。有任何想法吗?任何事情都将不胜感激。谢谢
struct Movie{
string movieName;
double firstWeek;
double secondWeek;
double thirdWeek;
double fourthWeek;
double total;
double avg;
};
//原型
void dispMenu();
char menuChoice();
void inputData(Movie *mov);
float getValidSale();
void TotalAvgSales(Movie *mov);
void displayMovieData(Movie mov);
int main(){
cout << "Welcome to Movie Data\n";
// movie 1 and 2 structure variables
Movie movie1, movie2;
char choice = 'B';
do { // loop while not D
choice = menuChoice(); // get menu choice
switch (choice) // menu options
{
case 'A':
case 'a':
// input for first movie
cout << "First Movie\n";
inputData(&movie1);
// input for second movie
cout << endl << "Second Movie\n";
inputData(&movie2);
break;
case 'B':
case 'b':
// compute for movie 1
TotalAvgSales(&movie1);
// compute for movie 2
TotalAvgSales(&movie2);
cout << "Completed!\n";
break;
case 'C':
case 'c':
// display for first movie
cout << "First Movie\n";
displayMovieData(movie1);
// display for second movie
cout << "Second Movie\n";
displayMovieData(movie2);
case 'd':
exit;
}
} while (choice != 'D');
}
//显示菜单
void dispMenu(){
cout << "Menu Options:\n";
cout << "A) Add Data\n";
cout << "B) Compute Total and Avg\n";
cout << "C) Display Data\n";
cout << "D) Quit\n";
}
//获取菜单选项并验证其是否为有效输入
char menuChoice(){
char choice; // option selected
dispMenu(); // displays menu
cin >> choice;
choice = toupper(choice); // if they enter lowercase, changes it to uppercase
cin.ignore(1, '\n'); // ignores input buffer
while ( choice < 'A' || choice > 'D') // make sure it is A B C or D
{
dispMenu(); // display menu
cin >> choice;
choice = toupper(choice); // if enter lower case, make upper case
cin.ignore(1, '\n'); // ignore input buffer
}
return choice;
}
float getValidSale(){
float sale;
cout << "Please enter in the number of tickets sold...\n";
cin >> sale;
while ( sale < 0){
cout << "Please enter in the number of tickets sold (>0)...";
cin >> sale;
}
return sale;
}
下面是问题功能
void inputData(Movie *mov){
// enter movie name
cout << "Please enter in Movie name...";
getline (cin, mov->movieName);
// enter first week sales
cout << "First Week Number of Tickets Sold...\n";
cout << "Please enter the number of tickets sold...";
cin >> mov->firstWeek;
// verify input >0a
while(mov->firstWeek < 0){
cout << "Please enter in the number of tickets sold (>0)...";
cin >> mov->firstWeek;
}
// enter second week sales
cout << "Second Week Number of Tickets Sold...\n";
cout << "Please enter the number of tickets sold...";
cin >> mov->secondWeek;
// verify input >0
while(mov->secondWeek < 0){
cout << "Please enter in the number of tickets sold (>0)...";
cin >> mov->secondWeek;
}
// enter third week sales
cout << "Third Week Number of Tickets Sold...\n";
cout << "Please enter the number of tickets sold...";
cin >> mov->thirdWeek;
// verify input >0
while(mov->thirdWeek < 0){
cout << "Please enter in the number of tickets sold (>0)...";
cin >> mov->thirdWeek;
}
// enter second week sales
cout << "Fourth Week Number of Tickets Sold...\n";
cout << "Please enter the number of tickets sold...";
cin >> mov->fourthWeek;
// verify input >0
while(mov->fourthWeek < 0){
cout << "Please enter in the number of tickets sold (>0)...";
cin >> mov->fourthWeek;
}
}
//计算平均销售额
void TotalAvgSales(Movie *mov){
// adding up the sales of each week
mov -> total = mov->firstWeek + mov->secondWeek + mov->thirdWeek + mov->fourthWeek;
// finding the average
mov -> avg = mov->total/4;
}
//显示数据
void displayMovieData(Movie mov)
{
cout << fixed << setprecision(2);
cout << "First Movie\n";
cout << "Division Name: " << mov.movieName << endl;
cout << "First Quarter Sales: "<< mov.firstWeek << endl;
cout << "Second Quarter Sales: "<< mov.secondWeek << endl;
cout << "Third Quarter Sales: " << mov.thirdWeek << endl;
cout << "Fourth Quarter Sales: "<< mov.fourthWeek << endl;
cout << "Total Number of Tickets Sold: " << mov.total << endl;
cout << "Average Number of Tickets Sold: " << mov. avg << endl;
}
我相信我已解决。就在上方getline(cin,mov-> movieName);我加了一个cin.clear();还有一个cin.ignore();这似乎有效。