【问题标题】:Trouble Implementing Two Vectors MVS C++难以实现两个向量 MVS C++
【发布时间】:2021-03-20 11:01:25
【问题描述】:

您好 Stack Overflow 用户, 我有这个程序,我正在尝试为我的电影集创建这个程序,请原谅我的一些错误和疏忽,因为我是 C++ 新手,但基本上我的程序运行(是的)但是当我选择了四个中的任何一个时弹出此错误的选项: Main Error

我希望第一个程序选项显示如下:

Akira 10.0

银翼杀手 10.0

死侍 8.8

但它没有这样做它看起来像:

10.0

10.0

8.8 Example of my bad output

我的输出只显示新行的所有分数,但没有显示电影标题,如果我尝试最后三个选项(最高/最低/平均)中的任何一个,它只会显示相同的错误:Same error as above

如果您可以给我任何指示(或代码示例),我需要做什么来使输出显示为上面的粗体,那太棒了!

再次请原谅我可能犯的简单错误,因为这是我第一次使用向量。

#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;

// Function Prototypes
void displayList(vector<string>,vector<double>);
void highestRating(vector<double>);
void lowestRating(vector<double>);
void averageRating(vector<double>);

// 1st Vector Defintion
vector<string> movies{ // inside this is a list of 48 strings containing movie names (Ex: "Akira", "Blade Runner" };

// 2nd Vector Definition
vector<double> ratings{ // Inside this is a list of 48 doubles that contain movie scores for each movie 
and i wish for it to print out directly right of the movie title, then turn to the next line. 
Examples of numbers inside this vector: 9.9, 10.0, 5.5, 7.5 };

// Main
int main()
{
    cout << fixed << showpoint << setprecision(1);  
    // Variable Definitions

    const int DISPLAY = 1, HIGHEST = 2, LOWEST = 3, AVG = 4;
    int response;
    cout << "-------Welcome to C++ Newbie's Movie Collection Database-------" << endl;
    cout << "\nEnter 1 to display the whole collection" << "\nEnter 2 to display the 5 highest rated movies by me";
    cout << "\nEnter 3 to display the lowest 5 rated movies by me" << "\nEnter 4 to display the average score of the movies in the collection along with some other math" << endl;
    cin >> response;
    // Menu
        do
        {
            switch (response)
            {
                case(DISPLAY):
                {
                    displayList(movies, ratings);
                    break;
                }
                case(HIGHEST):
                {
                    highestRating(ratings);
                    break;
                }
                case(LOWEST):
                {
                    lowestRating(ratings);
                    break;
                }
                case(AVG):
                {
                    averageRating(ratings);
                    break;
                }
                default:
                {
                    exit(0);
                }
            }
        } while (response == 1 || response == 2 || response == 3 || response == 4);
    return 0;
}


// Display List
void displayList(vector <string> movies, vector <double> ratings)
{
    for (int val = 0; val <= movies.size(); ++val)
    {
        cout << movies[val] << "\t";

        for (int rat = 0; rat <= ratings.size(); ++rat)
        {
            cout << ratings[rat] << endl;

        }
    }
}

// Display highest by me
void highestRating(vector<double> ratings)
{
    double max = ratings[0];

    for (int i = 0; i <= ratings.size(); i++)
    {
        if (ratings[i] >= max)
            max = ratings[i];
    }
    cout << "\nThe highest rated movie is " << max << endl;
}

// Display Lowest by me
void lowestRating(vector<double> ratings)
{
    double min = ratings[0];

    for (int a = 0; a <= ratings.size(); a++)
    {
        if (ratings[a] <= min)
            min = ratings[a];
    }
    cout << "\nThe lowest rated movie is" << min << endl;
}

// Average Score by me 
void averageRating(vector<double> ratings)
{
    double average;
    double total = 0;
    

    for (int x = 0; x <= ratings.size(); x++)
    {
        total += ratings[x];
    }
    average = total / ratings.size();
    cout << "\nThe average is " << average << endl;
    double forTheFunOfIt = fmod(average, 2.0);
    cout << "\nThe Average divided by 2 is " << forTheFunOfIt << endl;
}

【问题讨论】:

    标签: c++ vector multidimensional-array


    【解决方案1】:

    为了解决displayList() 中的第一个问题,您正在显示一部电影,然后循环显示所有评分。您可能需要在同一个循环中显示两者。此外,如果您这样做,则需要确保两个向量具有完全相同的大小,以避免尝试访问超出向量范围的内容(最好使用电影和评级的 std::map)。

    话虽如此,对于您上面的代码,可以尝试将 displayList() 更改为类似

    // Display List
    void displayList(vector <string> movies, vector <double> ratings)
    {
        for (int val = 0; val < movies.size(); val++)
        {
            cout << movies[val] << "\t" << ratings[val] << std::endl;
        }
    }
    

    注意,当val &lt;= movies.size() 时,您不会想要循环,(例如)4 部电影和 4 个评分的向量的索引将是 0-3,而不是 0-4。您将遇到一个错误,表明您超出范围。

    现在对于您的下一个问题,您在用户的初始输入上循环 main(),但您永远不会从用户输入更新响应变量。这将导致您陷入无限循环!一个简单的解决方法是将 cin &gt;&gt; response 移动到你的 do-while 循环的顶部。

    转到接下来的几个问题,对于您的最高、最低和平均而言,这是无效的输出。同样,当索引小于或等于向量的总大小时,您不应该循环。相反,您想要的不止于此。

    最后,您可能需要考虑将向量传递为const references

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      相关资源
      最近更新 更多