使用继承程序,需要帮助来验证所输入汽车的注册信息

问题描述 投票:0回答:1

这是我创建的程序,但在主文件的第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;

}

Automobile.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);

};

Automobile.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;

}

这是我创建的程序,但在主文件的第124行中有一个错误,但我不知道如何解决它并使它正常运行。如果您选择添加新车,程序将询问您...

c++ class validation inheritance
1个回答
0
投票

看看您的:

© www.soinside.com 2019 - 2024. All rights reserved.