程序使用继承和需要帮助验证正在输入的汽车的注册



这是我创建的程序,但它在主文件的第124行有一个错误,但我不知道如何修复它并使其正常运行。如果您选择添加新车,程序将要求您输入类型、品牌、颜色、年份和注册号。我需要验证注册并检查它是否没有重复。我已经尝试过了,但目前在主(124行(中有一个错误

main

#include <iostream>
#include "Car.h"
#include "Automobile.h"
using namespace std;
int main()
{
int Choice, Option;
string Color, Registration, Brand, Type;
int Year;
Car carro[100];
int Acount = 0;
do
{
cout << "  -----MENU:-----  " << endl;
cout << "  Select an option from the menu:  " << endl;
cout << " 1) Enter a New Car  " << endl;
cout << " 2) View all Cars  " << endl;
cout << " 3) Exit  " << endl;
cout << " Enter your choice: ";
cin >> Choice;
switch (Choice)
{
case 1:
carro[Acount];
cout << " What type of car is it: " << endl;
cout << " 1) Sedan " << endl;
cout << " 2) Hatchback " << endl;
cout << " 3) Minivan " << endl;
cout << " 4) Crossover " << endl;
cout << " 5) SUV " << endl;
cout << " 6) Coupe " << endl;
cout << " 7) Convertible " << endl;
cout << " 8) Sport " << endl;
cout << " 9) MPV (Multi Purpose Vehicle) " << endl;
cout << " 10) Station Wagon " << endl;
cout << " 11) Truck " << endl;
cin >> Option;
switch (Option)
{
case 1:
Type = " Sedan ";
break;
case 2:
Type = " Hatchback ";
break;
case 3:
Type = " Minivan ";
break;
case 4:
Type = " Crossover ";
break;
case 5:
Type = " SUV ";
break;
case 6:
Type = " Coupe ";
break;
case 7:
Type = " Convertible ";
break;
case 8:
Type = " Sport ";
break;
case 9:
Type = " MPV (Multi Purpose Vehicle) ";
break;
case 10:
Type = " Station Wagon ";
break;
case 11:
Type = " Truck ";
break;
default:
cout << " Invalid Choice " << endl;
break;
}
cout << " What color is the car? ";
cin >> Color;
cout << " What is the car's brand? ";
cin >> Brand;
do 
{
cout << " From what year is the car from? ";
cin >> Year;
} while (Year <= 1700 || Year >= 2022);
cout << " What is the car's license plate number? ";
cin >> Registration;
carro[Acount].setType(Type);
carro[Acount].setColor(Color);
carro[Acount].setBrand(Brand);
carro[Acount].setYear(Year);
carro[Acount].setRegistration(Registration);
Acount++;
break;
case 2:
cout << " ----All Cars Entered---- " << endl;
cout << "______________________________________________________________________________________________" << endl;
for (int i = 0; i < Acount; i++)
{
cout << " The car is a: " << carro[i].getType() << endl;
cout << " The car is: " << carro[i].getColor() << endl;
cout << " The car's brand is: " << carro[i].getBrand() << endl;
cout << " The car is from: " << carro[i].getYear() << endl;
cout << " The car's license plate is: " << carro[i].getRegistration() << endl;
cout << "______________________________________________________________________________________________" << endl;
}
break;
case 3:
cout << " ----Leaving Program---- " << endl;
break;
default:
cout << " Invalid Input " << endl;
break;
}
} while (Choice != 3);
system("pause");
return 0;
}

汽车.h

#pragma once
#include <string>
using namespace std;
class Automobile
{
private:
string Color;
int Year;
string Type;
public:
Automobile();
~Automobile();
string getColor();
int getYear();
string getType();
void setColor(string);
void setYear(int);
void setType(string);
};

Car.h

#pragma once
#include <string>
#include "Automobile.h"
using namespace std;
class Car : public Automobile
{
private:
string Registration;
string Brand;
public:
Car();
~Car();
string getRegistration();
string getBrand();
void setRegistration(const Car[], int, string);
void setBrand(string);
};

汽车.cpp

#include <iostream>
#include "Automobile.h"
using namespace std;
Automobile::Automobile()
{
Color = "";
Year = 0;
Type = "";
}
Automobile::~Automobile()
{

}
string Automobile::getColor()
{
return Color;
}
int Automobile::getYear()
{
if (Year >= 1700 && Year <= 2022)
{
return Year;
}
else
{
cout << " Invalid Year " << endl;
exit(EXIT_FAILURE);
}
}
string Automobile::getType()
{
return Type;
}
void Automobile::setColor(string color)
{
Color = color;
}
void Automobile::setYear(int year)
{
Year = year;
}
void Automobile::setType(string type)
{
Type = type;
}

Car.cpp

#include <iostream>
#include "Automobile.h"
#include "Car.h"
using namespace std;
Car::Car()
{
Registration = "";
Brand = "";
}
Car::~Car()
{

}
string Car::getRegistration()
{
return Registration;
}
string Car::getBrand()
{
return Brand;
}
void Car::setRegistration(const Car C[], int size, string registration) 
{
bool end = false;
while (end == false)
{
bool found = false;
for (int i = 0; i < size; ++i)
{
if (C[i].Registration.compare(registration) == 0)
found = true;
}
if (found == false)
{
end = true;
}
else
{
cout << " LICENSE NUMBER ALREADY IN USE BY ANOTHER CAR, PLEASE ENTER ANOTHER NUMBER: ";
cin >> registration;
}
}
Registration = registration;
}
void Car::setBrand(string brand)
{
Brand = brand;
}

查看您的:

void setRegistration(const Car[], int, string);

在第124行,您用一个参数Registration来调用它,Registration是一个字符串。

carro[Acount].setRegistration(Registration);

这会导致一个错误。

相关内容

  • 没有找到相关文章

最新更新