为什么我的有效数据一直输出到错误的 switch 语句上

Why does my valid data keep outputting onto the wrong switch statement

提问人:Zximy 提问时间:9/3/2023 最后编辑:Jonny HenlyZximy 更新时间:9/3/2023 访问量:34

问:

我的有效数据(记录.txt)不断输出到错误的大小写语句。

记录.txt:

AB12MP349 Fusion5 20 17000.00
33435KMOP324 BMW 40 25000.00
AB12MP349 Audi 100 4000.00
AB12MP349 Pagni 1 2000000.00

如下图所示,当用户按 1 时,它会提供 1/3 的有效记录,但将最后两条有效记录显示到 InvalidRecords 上。

输出:

./test

Welcome to  Car Database Lookup
----------------------------------------
Press 1 to display a table of valid car records: 
Press 2 to display a table full of InvalidRecords: 
Type here: 1
            Car ID               Model           Quantity           Price

            AB12MP349            Fusion5                20           17000              
Press 2 to display a table full of InvalidRecords: 
Type here: 2
Invalid car IDs:
33435KMOP324
BMW
40
25000.00
AB12MP349
Audi
100
4000.00
AB12MP349
Pagni
1
2000000.00
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <vector>
#include <cctype>
using namespace std;

// Class to store information about cars
class CarData {
public:
    string CarID;
    string Model;
    int Quantity;
    double Price;

    // Constructor to initialize CarData class(Parameters)
    CarData(const string &carID, const string &model, int quantity, double price)
        : CarID(carID), Model(model), Quantity(quantity), Price(price) {}
};

// Function prototypes
bool dataValidation(char d);
bool IsValidCarID(const string &carID);
bool DataCarPrice(double price);
void TakeCarData(vector<CarData> &carInfo, vector<string> &invalidCarIDs);

int main() {
    // Menu for User
    enum uiforUser {
        DISPLAY_CARINFO = 1,
        DISPLAY_ERROR = 2,
    };

    int userInput;

    cout << "\nWelcome to Pierce Car Database Lookup\n";
    cout << setfill('-');
    cout << setw(40) << "-" << endl;
    cout << "Press " << DISPLAY_CARINFO << " to display a table of valid car records: ";
    cout << endl;
    cout << "Press " << DISPLAY_ERROR << " to display a table full of Invalid Records: ";
    cout << "\nType here: ";
    cin >> userInput;

    // Used a Vector because it can hold many values and etc(Sequence Container !)
    vector<string> invalidCarIDs;
    vector<CarData> carInfo;

    switch (userInput) {
        // Menu To display Valid Records
        case DISPLAY_CARINFO: {
            TakeCarData(carInfo, invalidCarIDs);  // Getting Parameters from Function(TakeCarData)

            // Output for valid Records
            cout << setfill(' ');
            cout << setw(27) << "Car ID";
            cout << setw(20) << "Model";
            cout << setw(19) << "Quantity";
            cout << setw(16) << "Price";
            cout << endl << endl;

            // for loop that uses a pointer(Record), this allows me to access the data from carInfo!
            for (const CarData &Record : carInfo) {
                cout << setw(29) << Record.CarID;
                cout << setw(19) << Record.Model;
                cout << setw(18) << Record.Quantity;
                cout << setw(16) << Record.Price;
                cout << endl;
            }
        }
        break;

        // Menu to display Record Errors
        case DISPLAY_ERROR: {
            TakeCarData(carInfo, invalidCarIDs);  // Grabbing Parameters from Function below
            cout << "Invalid car IDs:" << endl;

            // for loop that uses a pointer(invalidID), this is allows me to access to data Validation Function
            for (const string &invalidID : invalidCarIDs) {
                cout << invalidID << endl;
            }
        }
        break;
    }

    return 0;
}

// DataValidation Logic !
bool dataValidation(char d) {
    // Checks if a character's range from A-Z, also checks for "O"
    return (d >= 'A' && d <= 'Z' && d != 'O');
}

bool DataCarPrice(double &price) {
    if (price <= 5000) {
        cout << "test";
    }
    return false;
}

bool IsValidCarID(const string &carID) {
    // checks length of CarID(9 characters long)
    if (carID.length() != 9) {
        return false;
    }

    if (!dataValidation(carID[0]) || !dataValidation(carID[1])) {
        return false;
    }

    for (int i = 2; i < 6; ++i) {
        // isalnum is used because it can check if a value is alphanumeric
        if (!isalnum(carID[i]) || carID[i] == 'O') {
            return false;
        }
    }

    for (int i = 3; i < 6; ++i) {
        if (!isdigit(carID[i])) {
            return false;
        }
    }

    return true;
}

void TakeCarData(vector<CarData> &carInfo, vector<string> &invalidCarIDs) {
    ifstream RecordFile;
    RecordFile.open("Records.txt");

    if (!RecordFile) {
        cout << "File not found. Please check the file path." << endl;
        system("pause");
        exit(EXIT_FAILURE);
    }

    // Read data from the file and add it to the vector
    string carID, model;
    int quantity;
    double price;

    while (RecordFile >> carID >> model >> quantity >> price) {
        carInfo.emplace_back(carID, model, quantity, price);  // Add car data to the vector
    }
    while (RecordFile >> carID) {
        invalidCarIDs.emplace_back(carID);
    }

    RecordFile.close();

    // Open an output file (if needed)
    ofstream DataBase;
    DataBase.open("Output.txt");
    if (!DataBase) {
        cout << "Output file not found. Please check the file path." << endl;
        system("pause");
        exit(EXIT_FAILURE);
    }

    // Close files when done
    DataBase.close();
}

我修改了 TakeCarData 函数,将有效的汽车数据与无效的汽车 ID 分开,并根据用户的选择填充相应的向量。

C++ 向量 IFSTREAM 数据处理

评论

1赞 Ripi2 9/3/2023
欢迎来到 SO!你在哪里进行验证?此外,使用调试器(或在很多地方“cout”一些变量)来查看这些“while”循环的运行情况。
1赞 Jonny Henly 9/3/2023
似乎没有一个汽车ID是有效的。举个例子,它的第 5 个字符 () 不是数字。AB12MP349carID[4]M
0赞 Sam Varshavchik 9/3/2023
你会很高兴地听到你不需要任何人的帮助来解决这个问题,只需要一个你已经拥有的工具:你的调试器!这正是调试器的用途。它一次运行一行程序,并显示正在发生的事情,这是每个 C++ 开发人员都必须知道如何做的事情。在调试器的帮助下,您将能够快速找到您编写的这个程序和所有未来程序中的所有问题,而无需向任何人寻求帮助。您是否已经尝试过使用调试器?如果不是,为什么不呢?调试器向你展示了什么?
0赞 Zximy 9/4/2023
谢谢山姆,最终使用调试器解决了问题!

答: 暂无答案