当我使用homework检查cout向量中的值时,似乎只返回homework[0]中的值。有人能检查一下我的代码然后告诉我哪里出错了吗?
int main()
{
cout << "Please enter midterm and final: " << endl;
double midterm, gfinal;
cin >> midterm >> gfinal;
cout << "Enter all your homework grades, " << endl;
double x;
cin >> x;
// initing a vector object named homework
vector<double> homework;
// show debug on the vector
while (homework.size() != 3)
homework.push_back(x);
if (homework.size() == 0)
{
cout << endl << "You need to enter at least one number";
return 1;
}
// vector before sorting
// since cout << homework did not seem to work I could always just write a debug function to iterate over structures and print them to the console
cout << homework[0] << endl;
cout << homework[1] << endl;
cout << homework[2] << endl;
// sort the vector here
sort(homework.begin(), homework.end());
// vector after sorting
//cout << homework;
cout << homework[0] << endl;
cout << homework[1] << endl;
cout << homework[2] << endl;
int mid = homework.size() / 2;
cout << "The below is mid" << endl;
cout << mid << endl;
double median;
if (homework.size() % 2 == 0)
median = (homework[mid - 1] + homework[mid]) / 2;
else
median = homework[mid];
//streamsize prec = cout.precision(3);
cout << "Your course grade is "
<< 0.2 * midterm + 0.4 * gfinal + 0.4 * median << endl;
//cout.precision(prec);
return 0;
}这是引起我困惑的具体代码:
// vector before sorting
// since cout << homework did not seem to work I could always just write a debug function to iterate over structures and print them to the console
cout << homework[0] << endl;
cout << homework[1] << endl;
cout << homework[2] << endl;
// sort the vector here
sort(homework.begin(), homework.end());
// vector after sorting
//cout << homework;
cout << homework[0] << endl;
cout << homework[1] << endl;
cout << homework[2] << endl;当程序启动时,它需要两个值,所以我插入了100。然后它要求3个值,所以我使用了8090100,当我期望8090100时,所有家庭作业位置的cout显示为80。实际程序的工作方式是,最终的cout按预期返回92。
发布于 2019-07-28 17:03:59
所以,虫子已经修好了。我将添加一个“更多-C++”的-style解决方案使用STL算法。
为了填充向量,我将使用std::copy_n。意思是,从std::cin读取n个值并将它们插入目标向量中。
至于你的问题
我怎么能看到向量里面有什么?
解决方案是迭代向量中的元素,并将向量值复制到ctd::cout。为此,我们将使用std::ostream_iterator
请注意:我总是使用定性的名字,比如std::cout。请考虑一下。我经常使用std::endl,因为它总是被称为刷新,这在大多数情况下是不需要的。另外:所有的变量都应该被初始化。一直都是。
然后,我添加了许多评论。也请考虑一下。这大大提高了代码的可读性和质量。
请参阅:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
constexpr size_t NumberOfHomeworkGrades = 3U;
int main()
{
// Print title and ask use to enter data
std::cout << "\nCalculation of course grade\n\nPlease enter midterm and final: " << '\n';
double midterm{ 0.0 };
double gfinal{ 0.0 };
std::cin >> midterm >> gfinal;
// Ask the use to enter the howmwork grades
std::cout << "Please Enter "<< NumberOfHomeworkGrades << " homework grades\n";
// Get the data from the user and put it into the vector
std::vector<double> homeworkGrades{};
std::copy_n(
std::istream_iterator<double>(std::cin), // We will iterate over std::cin and read data
NumberOfHomeworkGrades, // Over all we read data NumberOfHomeworkGrades times
std::back_inserter(homeworkGrades) // And we psuh the data into our homeworkGrades-vector
);
// Show the vector before sorting. Simply copy all data in the vector to std::cout using the ostream_iterator
std::cout << "\n\nEntered grades before sort\n";
std::copy(homeworkGrades.begin(), homeworkGrades.end(), std::ostream_iterator<double>(std::cout, "\n"));
// Sort the vector here
std::sort(homeworkGrades.begin(), homeworkGrades.end());
// Show the vector adter sorting. Simply copy all data in the vector to std::cout using the ostream_iterator
std::cout << "\n\nSorted grades\n";
std::copy(homeworkGrades.begin(), homeworkGrades.end(), std::ostream_iterator<double>(std::cout, "\n"));
// Calculate the median
double median{ 0 };
// First calculate the mid, to do the calculation only one time and to show the result
size_t mid{ homeworkGrades.size() / 2 };
if (!homeworkGrades.empty())
if (homeworkGrades.size() % 2 == 0)
median = (homeworkGrades[mid - 1] + homeworkGrades[mid]) / 2;
else
median = homeworkGrades[mid];
// Show the result to the user
std::cout << "\n\nThe mid value is (maybe truncated): " << mid
<< "\nThe median value is: " << median
<< "\n\nYour course grade is: " << 0.2 * midterm + 0.4 * gfinal + 0.4 * median << '\n';
return 0;
}发布于 2019-07-28 14:33:00
在您的代码中:
double x;
cin >> x; // <-- reading from cin only once
// initing a vector object named homework
vector<double> homework;
// show debug on the vector
while (homework.size() != 3)
homework.push_back(x); // <- inserting same value three times您只从cin读取一次,即读取单个值。然后,将读取值插入向量homework三次。因此,homework[0]、homework[1]和homework[2]包含相同的值。
考虑将cin >> x放在while循环中,从cin读取三次,而不是只读取一次,也就是说,在循环的每次迭代中从cin读取:
vector<double> homework;
while (homework.size() < 3) {
double x;
cin >> x;
homework.push_back(x);
}发布于 2019-07-28 14:56:34
除了指出的在现代C++中迭代向量的错误之外,您所要做的就是:
#include <vector>
#include <iostream>
int main()
{
std::vector<int> homework{ 70,80,90 }; // For the example
// "See what is inside the vector":
for (auto grade : homework)
std::cout << grade << '\n';
}https://stackoverflow.com/questions/57241832
复制相似问题