显示向量时出现运行时错误(c ++)

时间:2015-03-14 00:56:29

标签: c++ vector

我在使用c ++显示向量时遇到问题,我的语法似乎与我见过的示例匹配,但程序崩溃并在我的控制台中显示11db消息。我正在创建一个程序,要求输入和单位类型,然后存储这些,并根据输入的数据提供各种样本统计,我的程序中的断点是专门在线:

for(int i=0;i<totalMeters.size();i++) 

整个程序都在这里,我已成功运行除了矢量打印之外的所有功能。

#include <iostream>
#include<cmath>
#include<string>
#include<vector>
using namespace std;
double number1=100000;
double number2=-100000;
double howClose;
double newnum;
double sum=0;
double numberOfValues=0;
string unit;
string number1Unit;
string number2Unit;
double waitingNum;
vector <double> totalMeters;
void unitizer()
{
    while(cin>>newnum)
    {
        cout<<"what is the unit?(cm,m,in,ft)"<<endl;
        cin>>unit;
        if(unit!="ft"&&unit!="m"&&unit!="in"&&unit!="cm")
        {
            cout<<"incorect input"<<endl;
            unitizer();
        }
        cout<<newnum<<endl;
        if(newnum<number1)
        {
            number1=newnum;
            number1Unit=unit;
        }
        if(newnum>number2)
        {
            number2=newnum;
            number2Unit=unit;
        }
        sum+=newnum;
        numberOfValues++;
        cout<<"the smallest value so far is "<<number1<<number1Unit<<".";
        cout<<"the largest value so far is "<<number2<<number2Unit<<".";




        if(number1==number2)
        {
            cout<<number1<<number1Unit<<" is equal to "<<number2<<number2Unit<<endl;
        }

        if (unit=="cm")
            waitingNum=newnum/100;
        if (unit=="in")
            waitingNum=newnum*.0254;
        if (unit=="ft")
            waitingNum=newnum*.3048;
        if (unit=="m")
            waitingNum=newnum;
        totalMeters.push_back(waitingNum);


        howClose=abs(number1-number2);
        cout<<"The difference between the two values is "<<howClose<<"."<<endl;
        if(howClose<.001)
            cout<<"Wow, that's close"<<endl;

        cout<<"input one number please.(enter a non number to end the program.)"<<endl;
    }
}

void finalResults()
{
    cout<<"The sum of all the values is "<<sum<<"."<<endl;
    cout<<"the number of values enters is "<<numberOfValues<<"."<<endl;


}


void vectorOutput()
{
    for(int i=0;i<totalMeters.size();i++)
    {
        cout<<totalMeters[i]<<endl;

    }



}

int main(int argc, const char * argv[]) {
    cout<<"input one number please."<<endl;
    unitizer();
    finalResults();
    vectorOutput();
    return 0;
}

P.S。我试着看一下被调用的实际函数,但发现它非常长,超过了我目前对该语言的理解。

1 个答案:

答案 0 :(得分:0)

const char * argv []这不是标准的C ++。您不应该使用全局变量并使用更好的名称或注释,因为很难理解您的逻辑而不费力。 - 尼尔柯克

简单地取出额外的补充似乎可以解决问题