C++ 无法实现两个向量MVS C++;
你好,堆栈溢出用户, 我有这个程序,我试图为我的电影集合,并请原谅我的一些错误和疏忽,因为我是新的C++,但基本上我的程序运行(YAY),但当我选择了这四个选项中的任何一个错误弹出: 我希望第一个程序选项显示如下: Akira 10.0 刀片式转轮10.0 死池8.8 但事实并非如此,它只是看起来: 10.0 10.0 8.8 我的输出只是在新行上显示所有分数,但没有显示电影标题,如果我尝试最后三个选项中的任何一个(最高/最低/平均),它只会显示相同的错误: 如果您可以给我任何指针(或代码示例)作为我需要做的使输出显示为上面的粗体,那将是惊人的 再次请原谅我可能犯的简单错误,因为这是我第一次使用向量C++ 无法实现两个向量MVS C++;,c++,vector,multidimensional-array,C++,Vector,Multidimensional Array,你好,堆栈溢出用户, 我有这个程序,我试图为我的电影集合,并请原谅我的一些错误和疏忽,因为我是新的C++,但基本上我的程序运行(YAY),但当我选择了这四个选项中的任何一个错误弹出: 我希望第一个程序选项显示如下: Akira 10.0 刀片式转轮10.0 死池8.8 但事实并非如此,它只是看起来: 10.0 10.0 8.8 我的输出只是在新行上显示所有分数,但没有显示电影标题,如果我尝试最后三个选项中的任何一个(最高/最低/平均),它只会显示相同的错误: 如果您可以给我任何指针(或代码示
#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//功能原型
无效显示列表(矢量,矢量);
空隙高隔层(矢量);
空隙降低(矢量);
空隙平均(向量);
//第一向量定义
vector movies{//这是一个包含48个字符串的列表,其中包含电影名称(例如:“Akira”、“Blade Runner”};
//第二矢量定义
矢量评级{//这是一个包含48个双打的列表,其中包含每部电影的电影分数
我希望它直接打印在电影标题的右边,然后转到下一行。
此向量内的数字示例:9.9、10.0、5.5、7.5};
//主要
int main()
{
无法解决displayList()中的第一个问题
,您正在显示一部电影,然后循环显示所有收视率。您可能需要在同一个循环中同时显示这两个收视率。此外,如果执行此操作,则需要确保两个向量的大小完全相同,以避免尝试访问超出向量范围的内容,(最好使用电影和分级的std::地图)
话虽如此,对于上面的代码,请尝试将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;
}
}
//显示列表
无效显示列表(矢量电影、矢量分级)
{
对于(int val=0;val 无法解决displayList()中的第一个问题
,您正在显示一部电影,然后循环显示所有收视率。您可能需要在同一个循环中同时显示这两个收视率。此外,如果执行此操作,则需要确保两个向量的大小完全相同,以避免尝试访问超出向量范围的内容,(最好使用电影和分级的std::地图)
话虽如此,对于上面的代码,请尝试将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;
}
}
//显示列表
无效显示列表(矢量电影、矢量分级)
{
对于(int val=0;val