c++:使用特定的命令退出for循环push_back命令
C++: exiting a for loop push_back command with a specific command
我正在学习c++中的push_back向量,我到了可以使用它的地方。然而,我所编写的代码(粘贴在下面)在每次循环结束时询问用户是否想要继续循环。我发现这非常不方便,所以我想更改代码,当用户输入("EXIT")时,它将中断for循环。在这种情况下,我该如何更改代码呢?我粘贴了整个代码,以防我可能需要更改for循环之外的部分。
#define all student_marks.begin(), student_marks.end()
using namespace std;
int main()
{
vector<double> student_marks; //create container
double mark;
char more_students = 'y'; //set default to yes ('y')
while (more_students=='y' || more_students=='Y') {
cout<<"Enter mark for student #"<<student_marks.size()+1<<":";
cin>>mark; //enter mark
student_marks.push_back(mark); //push_back
cout<<"More students?(y/n)";
cin>>more_students; //user selects to break or continue the loop
}
double sum = accumulate(all, 0.0), average = sum/student_marks.size(); //sum and ave
cout<<endl
<<"Total mark:tt"<<sum<<endl
<<"Average mark:t"<<average<<endl
<<"Highest mark:t"<<*max_element(all)<<endl
<<"Lowest mark:t"<<*min_element(all)<<endl<<endl;
cout<<"-----Score list-----"<<endl;
sort(all,greater<double>()); //sort list
for (size_t i=0; i<student_marks.size(); i++)
cout<<"#"<<i+1<<". "<<student_marks[i]<<endl; //outputs results as list
return 0;
}
只要用户输入有效的标记,就可以留在循环中:
cout << "Enter mark for student #1:"
while(cin >> mark) {
students_marks.push_bak(mark);
cout << "Enter mark for student #" << marks.size() + 1;
}
当用户输入double
以外的标志,例如EOF
标志时,循环将退出。
相关文章:
- "error: no matching function for call to"构造函数错误
- 表示"accepting anything for this template argument" C++概念的通配符
- 如何在C++中从两个单独的for循环中添加两个数组
- 从命令行c++发送文本文件名
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 在Linux for Windows上编译C++代码时出错
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 调用专用模板时出错"no matching function for call to [...]"
- 为什么我的for循环不能正确获取argv
- 为什么我不能在 FOR LOOP 中使用 i/10,C++?
- 为什么我在使用 for 循环和 cout 命令时没有打印出矢量
- 使用 for 循环并将更改命令作为变量
- 如何从 c++ 程序启动 zsh/sh/bash 检查信号 SIGTTIN for 命令"cat &"
- 无法从命令行 VS2017 运行 msbuild for C++
- 在 C++ 中使用 -s for make 命令时不会显示哪些消息?
- C++-使用system()for Windows命令
- 从命令行启动时,在for循环期间不显示C++cout语句
- c++:使用特定的命令退出for循环push_back命令
- DeviceIoControl for SCSI查询命令返回错误50