用于打印对象矢量的意外输出

时间:2017-05-19 00:58:30

标签: c++ vector

我不理解我的程序的输出。我只是试图遍历一个对象向量并打印它们的成员函数。代码是这样的:

.cpp文件:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   Odometer.cpp
 * Author: Adrian
 * 
 * Created on May 18, 2017, 6:29 PM
 */

#include "Odometer.h"

Odometer::Odometer() {
    miles = 0;
    efficiency = 0;
}

void::Odometer::resetOdometer() {
    miles = 0;
}

void::Odometer::setEfficiency(double efficiency) {
    this->efficiency = efficiency;
}
void::Odometer::setMiles(int miles) {
    this->miles = miles;
}


void::Odometer::addToOdometer(int milesDriven ){
    miles=miles+ milesDriven;
}
//to self:really unsure what this is 

double::Odometer::gasolineConsumed(){
   return miles/efficiency;
}

double::Odometer::getEfficiency(){
   return efficiency;
}

int::Odometer::getMiles(){
   return miles;
}    

Odometer::Odometer(const Odometer& orig) {
}    

//to self:really unsure what this is

Odometer::~Odometer() {
}

和主文件:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   main.cpp
 * Author: Adrian
 *
 * Created on May 18, 2017, 6:28 PM
 */

#include "Odometer.h"
#include <sstream>
#include <vector>
using namespace std;    


template <typename T>
std::string NumberToString(T Number) {
    std::ostringstream ss;
    ss << Number;
    return ss.str();
}

/*
 * 
 */
int main(int argc, char** argv) {

    int miles;
    double efficiency;
    int numberTrips;
    vector<Odometer> v;
    cout << "Please enter the amount of trips taken : " << endl;
    cin >> numberTrips;
    cout << NumberToString(1) << endl;

    for (int i = 1; i <= numberTrips; i++) {
        cout << "Please enter number of miles for trip "  << NumberToString(i) << endl;
        cin >> miles;
        cout << "Please enter the efficiency for trip "  << NumberToString(i) << endl;
        cin >> efficiency;
        Odometer obj;
        obj.setMiles(miles);
        cout << "miles "  << obj.getMiles() ;

        obj.setEfficiency(efficiency);
        cout << "efficiency "  << obj.getEfficiency();
        v.push_back(obj);    
    }

    //the bug         

    for (std::vector<int>::size_type i = 1; i != v.size()+1; i++) {

        cout << "Miles driven for trip " << NumberToString(i) << " is "  << v[i].getMiles() << endl;
         cout << "Efficiency for trip " << NumberToString(i) << " is "  << v[i].getEfficiency() << endl;
         cout << "Gasoline consumed for trip " <<  NumberToString(i) << " is "  << v[i].gasolineConsumed() << endl;

    }

    return 0;
}

我应该输入里程数,而不是每加仑英里的效率,然后是消耗的汽油。输出应该是效率和汽油消耗的英里数。

我的控制台输出:

Please enter the amount of trips taken : 
1
1
Please enter number of miles for trip 1
10
Please enter the efficiency for trip 1
1
miles 10efficiency 1Miles driven for trip 1 is 16439
Efficiency for trip 1 is 1.51332e-306
Gasoline consumed for trip 1 is 1.#INF

1 个答案:

答案 0 :(得分:0)

C ++中的数组/向量索引从索引0开始,而不是1.你得到你看到的结果,因为你在向量的边界之外访问。

你的输出循环应该是

for (std::vector<int>::size_type i = 0; i < v.size(); i++)

或者使用迭代器而不是索引。