矢量打印不正确的数据

时间:2018-11-23 02:23:08

标签: class vector printing

有人可以告诉我为什么我的程序打印错误的值吗? 我主要关注youtube视频中的矢量使用方法。和编写的程序一样,我能够运行它。我试图为我的程序实现与汽车相同的方法(视频仅供学生使用)。但是我有问题。有趣的是,学生程序运行良好,我不记得对其进行任何更改,现在它也根本不打印任何内容。

因此,我正在尝试制作一个程序,将汽车品牌,年份和价格输入到一个类的向量中,然后打印出所有存储的信息。但是我目前得到的结果好坏参半。 我要么获得空白以获取结果,要么获得大量数字,并获得品牌(字符串)的空白

当我不使用引用&时,我得到空白结果,而&则得到我在下面写的内容。

有人有主意吗?

当我输入时: 品牌-兰博 年-1997 价格25000

我得到: 汽车品牌: 车年:2686588 车价:6.95144e-308


main.cpp

#include <iostream>
#include <string>
#include "Car.h"
#include <vector>


using namespace std;

Car::Car(){

}
Car::Car(string brand, int year, double price){
brand;
year;
price;
}
Car::~Car(){

}
string Car::getBrand() const{
return brand;
}
int Car::getYear() const{
return year;
}
double Car::getPrice()const{
return price;
}

void Car::setBrand(string brand){
brand;
}
void Car::setYear(int year){
year;
}
void Car::setPrice(double price){
price;
}




void enterData(vector<Car>&);
void showData(const vector<Car>&);

int main(){

vector<Car> myGarage;
int chc;

cout << "-- M A I N  M E N U --\n\n";
do{
    cout << "Please make a choice\n\n";
    cout << "1. Enter car details" << endl;
    cout << "2. Show car details" << endl;
    cout << "3. Exit" << endl;

    cin >> chc;
    switch(chc){
        case 1:
            enterData(myGarage);
        break;
        case 2:
            showData(myGarage);
        break;
        case 3:
            cout << "Have a nice day!";
        break;
    }

}
while(chc != 3);

}

void enterData(vector<Car>& newMyGarage){
string brand;
int year;
double price;

cout << "How many cars are in your garage?";
int garageSize;
cin >> garageSize;

for(int i = 0; i < garageSize; i++){
    cout << "Car brand: ";
    cin >> brand;
    cout << "Car year: ";
    cin >> year;
    cout << "Car price: ";
    cin >> price;

    Car newCar(brand, year, price);
    newMyGarage.push_back(newCar);
    cout << endl;
}



}
void showData(const vector<Car>& newMyGarage){
  unsigned int size = newMyGarage.size();
   for(unsigned int s = 0; s < size; s++){
        cout << "Car brand: " << newMyGarage[s].getBrand() << endl;
        cout << "Car year: " << newMyGarage[s].getYear() << endl;
        cout << "Car price: " << newMyGarage[s].getPrice() << endl;
        cout << endl;
       }

}

头文件Car.h

#ifndef CAR_H_INCLUDED
#define CAR_H_INCLUDED
#include <iostream>
#include <string>

using namespace std;

class Car{
public:
    string brand;
    int year;
    double price;
    Car();
    Car(string, int, double);
    ~Car();
    string getBrand() const;
    int getYear() const;
    double getPrice() const;
    void setBrand(string);
    void setYear(int);
    void setPrice(double);


};
#endif // CAR_H_INCLUDED

0 个答案:

没有答案