本文介绍了给出错误代码说“向量下标超出范围”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 该程序将编译,但我不能输入一个味道,因为它会给我一条消息说"向量下标超出范围"。该任务是提示用户指出他们想要的大小,以及输入多达10种口味(在程序中向量为)。有什么建议吗? #include< iostream> #include< string> #include< iomanip> #include< vector> #include< set> 使用命名空间std; class Yogurt_Order { public: Yogurt_Order(); void printWelcomeMessage(); double getYogurtSize (string& yogurtSize); int getYogurtFlavors(vector< string>& flavor); void printOrder(string& yogurtSize,vector< string>& flavor,int& counter,int& numFlavors); bool addAnotherOrderQ(); void printTotalCosts(双小计); 私人: 字符串大小; vector< string>味道; }; int main(){ $ //初始化循环变量 bool repeat = 1; int counter = 1; int numFlavors = 1; //大小和味道的变量 string yogurtSize; 字符串顺序; vector< string>味道; Yogurt_Order订单; //成本变量 double subtotal = 0; //打印欢迎信息 // printWelcomeMessage(); orders.printWelcomeMessage(); //在用户完成之前继续获取订单 while(repeat == 1){ //更新大小和小计 subtotal = subtotal + orders.getYogurtSize(yogurtSize); //更新口味 orders.getYogurtFlavors(flavor); $ //打印当前订单 orders.printOrder( yogurtSize,flavor,counter,numFlavors); $ //确定是否继续重复 repeat = orders.addAnotherOrderQ(); $ counter ++; } //打印小计,总计和税额 orders.printTotalCosts(subtotal); $ 返回0; } $ $ Yogurt_Order :: Yogurt_Order() { size ="" ;; b $ b} $ 无效Yogurt_Order :: printWelcomeMessage() { cout<< "欢迎来到我的冰冻酸奶!\ n";; cout<< " \ n" ;; return; } double Yogurt_Order :: getYogurtSize(string& yogurtSize) { size = yogurtSize; const double small_value = 2.19; const double medium_value = 3.49; const double large_value = 4.49; cout<< "你想要什么尺寸?\ n";; cout<< "请输入小,中或大:" ;; getline(cin,yogurtSize); cout<< " \ n"; $ double cost = 0; if(yogurtSize ==" small") { cost = small_value; } if(yogurtSize = =" medium") { cost = medium_value; } if(yogurtSize ==" large") { cost = large_value; } 退货成本; } int Yogurt_Order :: getYogurtFl avors(矢量<串GT;&安培;味道) { int numFlavors = 0; do { flavor.reserve(10); cout<< "输入风味" << numFlavors + 1<< " ;:" ;; $ getline(cin,flavor [numFlavors]); numFlavors ++; if(flavor [numFlavors] ==" DONE") { numFlavors--; } } while((flavor [numFlavors - 1]!=" ; DONE"&&(numFlavors< 10)); $ 返回numFlavors ; $ } $ 无效Yogurt_Order :: printOrder(字符串& yogurtSize,vector< string>& flavor,int& counter, int& numFlavors) { cout<< "**订单" <<计数器<< ":" << yogurtSize<< " " ;; if(flavor [numFlavors - 1] ==" DONE") { for(size_t i = 0; i< numFlavors - 2; i ++) { if(flavor [i]!=" DONE") { flavor [i] .resize(4 ); $ cout<<味道[i]<< " - " ;; } } } 返回; } bool Yogurt_Order :: addAnotherOrderQ() { 字符串重复; cout << "是否要添加其他订单?\ n";; cout<< "请输入是或否:" ;;" getline(cin,repeat); if(repeat ==" yes") { cout<< " \ n" ;; 返回1; } 其他 { 返回false; } } $ void Yogurt_Order :: printTotalCosts(double subtotal) { const double tax_value = 0.0875; cout.precision(2); cout<<固定; cout<< " \ n" ;; cout<< "小计:$" <<小计<< endl; cout<< "税(8.75%):$" <<小计* tax_value<< endl; cout<< "总计:$" <<小计+(小计* tax_value)<< endl; cout<< " \ n" ;; return; }   ; &NBSP;   解决方案 在调试器中逐行逐步执行代码,您将能够看到错误发生的原因。 The program will compile, but I cannot enter a flavor since it will give me a message saying "vector subscript out of range". The assignment is to prompt the user to indicate what size they want, along with inputting up to 10 flavors (which is the vector in the program). Any suggestions?#include <iostream>#include <string>#include <iomanip>#include <vector>#include <set>using namespace std;class Yogurt_Order {public:Yogurt_Order();void printWelcomeMessage();double getYogurtSize(string & yogurtSize);int getYogurtFlavors(vector <string>& flavor);void printOrder(string & yogurtSize, vector <string>& flavor, int & counter, int & numFlavors);bool addAnotherOrderQ();void printTotalCosts(double subtotal);private:string size;vector <string> flavors;};int main() {//Initializing the loop variablesbool repeat = 1;int counter = 1;int numFlavors = 1;// Variables for size and flavorsstring yogurtSize;string order;vector <string> flavor;Yogurt_Order orders;// Variable for costdouble subtotal = 0;//Prints the welcome message//printWelcomeMessage();orders.printWelcomeMessage();// Continues to get orders until the user is donewhile (repeat == 1) {// Updates the size and subtotalsubtotal = subtotal + orders.getYogurtSize(yogurtSize);//Updates the flavorsorders.getYogurtFlavors(flavor);// Prints the current orderorders.printOrder(yogurtSize, flavor, counter, numFlavors);// Determine whether or not to keep repeatingrepeat = orders.addAnotherOrderQ();counter++;}// Prints out the subtotal, total, and taxorders.printTotalCosts(subtotal);return 0;}Yogurt_Order::Yogurt_Order(){size = "";}void Yogurt_Order::printWelcomeMessage(){cout << "Welcome to My Frozen Yogurt!\n";cout << "\n";return;}double Yogurt_Order::getYogurtSize(string & yogurtSize){size = yogurtSize;const double small_value = 2.19;const double medium_value = 3.49;const double large_value = 4.49;cout << "What size would you like?\n";cout << "Please enter small, medium, or large: ";getline(cin, yogurtSize);cout << "\n";double cost = 0;if (yogurtSize == "small"){cost = small_value;}if (yogurtSize == "medium"){cost = medium_value;}if (yogurtSize == "large"){cost = large_value;}return cost;}int Yogurt_Order::getYogurtFlavors(vector<string>& flavor){int numFlavors = 0;do{flavor.reserve(10);cout << "Enter flavor " << numFlavors + 1 << ": ";getline(cin, flavor[numFlavors]);numFlavors++;if (flavor[numFlavors] == "DONE"){numFlavors--;}} while ((flavor[numFlavors - 1] != "DONE") && (numFlavors < 10));return numFlavors;}void Yogurt_Order::printOrder(string & yogurtSize, vector<string>& flavor, int & counter, int & numFlavors){cout << "**Order " << counter << ": " << yogurtSize << " ";if (flavor[numFlavors - 1] == "DONE"){for (size_t i = 0; i < numFlavors - 2; i++){if (flavor[i] != "DONE"){flavor[i].resize(4);cout << flavor[i] << "-";}}}return;}bool Yogurt_Order::addAnotherOrderQ(){string repeat;cout << "Would you like to add another order?\n";cout << "Please enter yes or no: ";getline(cin, repeat);if (repeat == "yes"){cout << "\n";return 1;}else{return false;}}void Yogurt_Order::printTotalCosts(double subtotal){const double tax_value = 0.0875;cout.precision(2);cout << fixed;cout << "\n";cout << "Subtotal: $" << subtotal << endl;cout << "Tax (8.75%): $" << subtotal*tax_value << endl;cout << "Total: $" << subtotal + (subtotal*tax_value) << endl;cout << "\n";return;}      解决方案 Step through your code line by line in the debugger and you will be able to see why the error occurs. 这篇关于给出错误代码说“向量下标超出范围”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-30 12:23