You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++数组存储车辆信息异常:单条数据添加后其余元素乱码且无法新增

车辆库存管理程序的问题修复

问题描述

本人C++开发经验有限,正在编写一个用对象数组存储10辆车辆品牌及价格的程序,需求是将输入的车辆信息存入数组的下一个空位置。目前遇到两个异常:

  • 添加一辆车(比如输入“Honda 3500”)后,数组其余元素的价格显示随机数值;
  • 尝试添加第二辆车时,程序直接提示数组已满,找不到空位置。
    移除价格打印后,其余车辆品牌字段为空,但价格仍显示乱码,怀疑和price字段有关,但不止是打印错误。

原代码

主函数及相关逻辑

int main (void) {
    array<Car, 10> carArr; 
    int input = 0;

    do{
        displayMenu();
        cin >> input;
        if (input == 1){ 
            addData(carArr);
        } else if (input == 2){ 
            deleteData(carArr);
        } else if (input == 3){
            printData(carArr);
        } else if (input == 4){
            break;
        } else {
            cout << "invalid selection" << endl;
        }
    }
    while (1);
}

void displayMenu (){
    cout << "1. Add a car to inventory\n2. Delete a car from inventory\n3. Print inventory\n4. Exit\nYour choice: ";
}

bool addData(array<Car,10>& carArr){
    Car carObj;
    cout << "Make of car, and price of car, separated by spaces: ";
    cin >> carObj.carMake >> carObj.carPrice; 
    for (int i = 0;i < 10;i++) { 
        if (carArr[i].getCarMake().compare("") == 0 ){  // if array[i].carmake is equal to ""
            carArr[i] = carObj;
            cout << "Car Added" << endl;
            return true;
        } else {
            cout << "There are no open Elements in the array" << endl;
            return false;
        }
    }
}

bool deleteData(array<Car,10>& carArr){ // this isnt finished
    return false;
    return true;
}
void printData(array<Car,10>& carArr){
    for (int i = 0;i < 10;i++) {
        cout << "Car #" << i+1 << ": " << carArr[i].getCarMake() << " " << carArr[i].getCarPrice() << endl;
    }
}

Car类定义

#include <iostream>
#include <string>
using namespace std;

class Car {
    public:
    string carMake;
    int carPrice;

    Car (string make,int price) { // use this to set car make and car price
        carMake = make;
        carPrice = price;
    }
    Car () { //default contructor

    }
    string getCarMake () {
        return carMake;
    }
    int getCarPrice () {
        return carPrice;
    }
    void setCarMake (string x){
        carMake = x;
    }
};

问题根源

  1. addData函数循环逻辑错误:
    当前代码遍历数组时,只要第一个元素非空(第一次添加后第一个元素就有值),就直接返回“数组已满”,根本不会检查后续的空位置。正确逻辑应该是遍历完所有元素,确认没有空位置后才提示已满。

  2. Car类默认构造函数未初始化carPrice:
    使用默认构造函数创建的Car对象,carMake作为string会默认初始化为空字符串,但carPrice是int类型,未初始化时会是随机垃圾值,导致打印时出现乱码。


修复后的代码

修改Car类默认构造函数

#include <iostream>
#include <string>
#include <array> // 补充array头文件,原代码遗漏
using namespace std;

class Car {
public:
    string carMake;
    int carPrice;

    Car(string make, int price) {
        carMake = make;
        carPrice = price;
    }
    // 默认构造函数初始化carPrice为0,避免垃圾值
    Car() : carMake(""), carPrice(0) {}

    string getCarMake() {
        return carMake;
    }
    int getCarPrice() {
        return carPrice;
    }
    void setCarMake(string x) {
        carMake = x;
    }
    // 新增setCarPrice方法,方便后续修改价格
    void setCarPrice(int x) {
        carPrice = x;
    }
};

修改addData函数逻辑

bool addData(array<Car,10>& carArr){
    Car carObj;
    cout << "Make of car, and price of car, separated by spaces: ";
    cin >> carObj.carMake >> carObj.carPrice; 
    // 遍历所有元素寻找空位置
    for (int i = 0; i < 10; i++) { 
        // 空位置判断:品牌为空且价格为0(确保是未使用的元素)
        if (carArr[i].getCarMake().empty() && carArr[i].getCarPrice() == 0){
            carArr[i] = carObj;
            cout << "Car Added" << endl;
            return true;
        }
    }
    // 遍历完所有元素未找到空位置,才提示数组已满
    cout << "There are no open Elements in the array" << endl;
    return false;
}

补充说明

  • 原代码主函数使用array但未包含<array>头文件,必须添加否则会编译错误;
  • deleteData函数未实现,后续可补充逻辑(比如根据品牌/索引删除,删除后将后续元素前移或标记为空)。

内容的提问来源于stack exchange,提问作者inkystinky33

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 21:07:21