如何修复这个结构字符串函数 C++? [复制]

Posted

技术标签:

【中文标题】如何修复这个结构字符串函数 C++? [复制]【英文标题】:How to fix this structure string function C++? [duplicate] 【发布时间】:2020-04-16 01:27:56 【问题描述】:

我的函数 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;

【问题讨论】:

显示的代码使用std::getline&gt;&gt; 来读取输入。如果您不完全了解它们的工作原理,您会发现这样的小惊喜。经验教训:如果您手头的任务是读取一行输入,这就是 std::getline 的用途,而不是 &gt;&gt;。这就是std::getline 的工作:输入一行。 &gt;&gt; 运算符不,我重复一遍,输入一行文本。它做了一些微妙的不同。如果您不知道确切它的作用,那么就会出现这种性质的山寨现象。用std::getlines 替换所有&gt;&gt;s。有关更多信息,请参阅链接的问题。 【参考方案1】:

我相信我修好了它。正上方 getline (cin, mov->movieName); 我加了一个 cin.clear(); 和一个 cin.ignore(); 这似乎有效。

【讨论】:

以上是关于如何修复这个结构字符串函数 C++? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

如何编写函数,用C++语言实现将一个字符串插入到另一个字符串的指定位置,最好用指针指向字符串来实现?

如何修复条件绑定的初始化程序必须具有可选类型,而不是“字符串”? [复制]

C++分割字符串? [复制]

c++ 的 substr() 是如何工作的? [复制]

将double转换为字符串C++? [复制]

将字符串添加到输入变量时如何修复错误? [复制]