首页 > 解决方案 > 在 Test Driver 中无法通过 getName 函数、getStockNo 函数或 getNoInStock 函数输出正确的信息

问题描述

Test Driver.cpp文件中,我无法获取getNamegetStockNogetNoInStock以输出正确的信息。用于分类和价格的 set 函数和 get 函数。我不知道如何从getName函数中获取字符。我已附上 CPP 文件和头文件。我只有Test Driver.cpp文件有问题

//Test Driver.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <string>
#include "GameRecord.h"

using namespace std;

int main()
{
    // Creating the object of the GameRecord class
    GameRecord gr1;
    // Setting the values in the GameRecord object gr1 as per the given question
    gr1.setName("VideoGame");
    gr1.setStockNo(200);
    gr1.setClassification(10);
    gr1.setPrice(250.0);
    gr1.setNoInStock(5);

    //problem here
    char video;
    int size = 0;
    gr1.getName(&video, size);
    cout << "Getting Name: " << video << endl;

    //problem here
    long stkNo = 0;
    gr1.getStockNo();
    cout << "Getting Stock Number: " << stkNo << endl;

    int classification;
    gr1.getClassification(classification);
    cout << "Getting Classification: " << classification << endl;

    double price;
    gr1.getPrice(price);
    cout << "Getting Price: $" << price << endl;

    //problem here
    int NoInStock = 0;
    gr1.getNoInStock();
    cout << "Getting Number in Stock: " << NoInStock << endl;

    // Creating another object of the GameRecord class
    GameRecord gr2("VideoGame2", 100, 5, 150.0);

    // To print the Gamerecord object values, call the printRecord() function
    gr2.printRecord();
}

//GameRecord.cpp file
#include <iostream>
#include <string.h>
#include "GameRecord.h"

using namespace std;

// Default Constructor used to initialize the data members of the class
GameRecord::GameRecord()
{
    strcpy_s(m_sName, "");
    m_lStockNo = 0;
    m_iClassification = 0;
    m_dPrice = 0.0;
    m_iCount = 0;
}

// Argumented Constructor is used to initializing the data members of the class with the
respective values GameRecord::GameRecord(const char* name, long sn, int cl, double or)
{
    strcpy_s(m_sName, sizeof(m_sName), name);
    m_lStockNo = sn;
    m_iClassification = cl;
    m_dPrice = or ;
    m_iCount = 1;
}

// Destructor of the class
GameRecord::~GameRecord()
{
}

/* Getter Setter Method of the GameRecord class, Getter method is used to
    get the respective values and setter is used to set the values*/

void GameRecord::getName(char* name, int nameSize)
{
    strcpy_s(name, nameSize, m_sName);
}

void GameRecord::setName(const char* name)
{
    strcpy_s(m_sName, sizeof(m_sName), name);
}

long GameRecord::getStockNo()
{
    return m_lStockNo;
}

void GameRecord::setStockNo(long sn)
{
    m_lStockNo = sn;
}

void GameRecord::getClassification(int& cl)
{
    cl = m_iClassification;
}

void GameRecord::setClassification(int cl)
{
    m_iClassification = cl;
}

void GameRecord::getPrice(double& c)
{
    c = m_dPrice;
}

void GameRecord::setPrice(double c)
{
    m_dPrice = c;
}

int GameRecord::getNoInStock()
{
    return m_iCount;
}

void GameRecord::setNoInStock(int count)
{
    m_iCount = count;
}

void GameRecord::printRecord()
{
    cout << m_sName << " " << m_lStockNo << " " << m_iClassification << " " << m_dPrice << " "
         << m_iCount << endl;
}

//GameRecord.h file
#pragma once

#include <iostream>
#include <string.h>

using namespace std;

class GameRecord
{
    private:
    char m_sName[128]; // used to store the name of a videogame or videogame system.
    long m_lStockNo; // used to store a unique stock number for the videogame or videogame
    system int m_iClassification; // used to code specific information about the videogame or
    videogame system
        .double m_dPrice; // used to store the price of this videogame or videogame system
    int m_iCount; // used to store the number of copies of this item currently in the
    inventory.

        public
        : GameRecord(); // constructor set the member variables to the following initial values.

    // set the member variables to the values passed into the function and initialize the
    m_iCount variable to one(1).GameRecord(const char* name, long sn, int cl, double price);

    ~GameRecord(); //The destructor shall be an empty, place - holder function.

    // copy the member variable m_sName into the character array pointed to by the function argument.
    void getName(char* name, int nameSize);

    // function will copy the function argument into the member variable m_sName.
    void setName(const char* name);

    // function shall return the value stored in the member variable m_lStockNo.
    long getStockNo();

    // function will copy the function argument into the member variable m_lStockNo.
    void setStockNo(long sn);

    // function will copy the member variable m_iClassification into the interger variable referenced by the function argument.
    void getClassification(int& cl);

    // function will copy the function argument into the member variable m_iClassification.
    void setClassification(int cl);

    // function will copy the member variable m_dPrice into the double variable pointed to by the function argument.
    void getPrice(double& c);

    // function will copy the function argument into the member variable m_dPrice.
    void setPrice(double c);

    int getNoInStock(); // function shall return the value stored in the member variable
    m_iCount.

        void
        setNoInStock(int count); // function will copy the function argument into the member
    variable m_iCount.

        void
        printRecord(); // shall print to the screen all data found in the record
};

标签: c++

解决方案


该功能getName应用作

int size=...;
char*name=new char[size];
c.gerName(name,size);

因为它通过将名称复制到输入中的指针来工作


推荐阅读