vendredi 13 mars 2015

run time error in displaying a vector(c++)


I'm having trouble displaying a vector in c++, my syntax seems to match the examples i've seen but program crashes and gives the 11db message in my console. I am creating a program that asks for input and unit types and then stores those, and gives various sample statistics based on the data entered, the breakpoint in my program is specifically on the line:



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


the entire program is here, and I have successfully run it with all features except the vector printing.



#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. I tried to look at the actual function that was being called, but found it very long and above my current comprehension of the language.




Aucun commentaire:

Enregistrer un commentaire