1

所以我仍在解决这个问题,并认为我的做法是正确的。但是,现在它只显示“ABC Industries”和“Report”,而不是仅在未输入信息时才显示。我在哪里弄乱了我的代码?

//This program displays a company's name and report.
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;

class Heading {
private:
    string company;
    string report;

public:
    Heading() {
        company = "ABC Industries";
        report = "Report";
    }


    Heading(string c, string r) {
        company = c;
        report = r;
    }


    void storeInfo(string company, string report);

    string getCompany() {
        return company;
    }
    string getReport() {
        return report;
    }
};


void Heading::storeInfo(string c, string r) {
    c = company;
    r = report;
}

void storeInfo(Heading&);
void showInfo(HANDLE, Heading);
void placeCursor(HANDLE, Heading);

int main() {
    Heading company;
    HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
    storeInfo(company);
    showInfo(screen, company);
    cin.ignore();
    cin.get();
    return 0;
}

/*****storeInfo*****/
void storeInfo(Heading &item) {
    string company;
    string report;
    cout << "\nPlease enter the company name.\n";
    getline(cin, company);
    cout << "\nPlease enter the report name.\n";
    \
    getline(cin, report);
    item.storeInfo(company, report);
}

/*****placeCursor*****/
void placeCursor(HANDLE screen, int row, int col) {
    COORD position;
    position.Y = row;
    position.X = col;
    SetConsoleCursorPosition(screen, position);
}

/*****showInfo*****/
void showInfo(HANDLE screen, Heading item) {
    system("cls");
    placeCursor(screen, 6, 25);
    cout << "********************************" << endl;
    placeCursor(screen, 8, 32);
    cout << item.getCompany() << endl;
    placeCursor(screen, 11, 36);
    cout << item.getReport();
    placeCursor(screen, 13, 25);
    cout << "********************************" << endl;
}
4

2 回答 2

2

更新

这是一个版本,它显示了如何在您期望它们时显示默认值:

在Coliru现场观看

class Heading {
private:
    string company;
    string report;

    static string defaultCompany() { return "ABC Industries"; }
    static string defaultReport () { return "Report"; }

public:
    Heading(string const& c = defaultCompany(), string const& r = defaultReport()) 
        : company(c), report(r) { }

    void storeInfo(string const& c, string const& r)
    {
        company = c;
        report  = r;
    }

    string getCompany() const { return company.empty()? defaultCompany() : company; }
    string getReport()  const { return report.empty()?  defaultReport()  : report;  }
};

如您所见,您需要检查收到的输入是否为空,因为您将始终无条件地将成员设置为storeInfo.

完整程序:

#include <iostream>
#include <string>

using namespace std;

namespace // windows_cruft
{
    using HANDLE = unsigned;
    HANDLE STD_OUTPUT_HANDLE = 1;
    HANDLE GetStdHandle(HANDLE) {return 42;}

    struct COORD { int X,Y; };
    void SetConsoleCursorPosition(HANDLE, COORD){}
}

class Heading {
private:
    string company;
    string report;

    static string defaultCompany() { return "ABC Industries"; }
    static string defaultReport () { return "Report"; }

public:
    Heading(string const& c = defaultCompany(), string const& r = defaultReport()) 
        : company(c), report(r) { }

    void storeInfo(string const& c, string const& r)
    {
        company = c;
        report  = r;
    }

    string getCompany() const { return company.empty()? defaultCompany() : company; }
    string getReport()  const { return report.empty()?  defaultReport()  : report;  }
};

void storeInfo(Heading&);
void showInfo(HANDLE, Heading);
void placeCursor(HANDLE, Heading);

int main() {
    Heading company;
    HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
    storeInfo(company);
    showInfo(screen, company);
    cin.ignore();
    cin.get();
    return 0;
}

/*****storeInfo*****/
void storeInfo(Heading &item) {
    string company;
    string report;
    cout << "\nPlease enter the company name.\n";
    getline(cin, company);
    cout << "\nPlease enter the report name.\n";
    getline(cin, report);
    item.storeInfo(company, report);
}

/*****placeCursor*****/
void placeCursor(HANDLE screen, int row, int col) {
    COORD position;
    position.Y = row;
    position.X = col;
    SetConsoleCursorPosition(screen, position);
}

/*****showInfo*****/
void showInfo(HANDLE screen, Heading item) {
    //system("cls");
    placeCursor(screen, 6, 25);
    cout << "********************************" << endl;
    placeCursor(screen, 8, 32);
    cout << item.getCompany() << endl;
    placeCursor(screen, 11, 36);
    cout << item.getReport() << endl;
    placeCursor(screen, 13, 25);
    cout << "********************************" << endl;
}
于 2013-10-18T00:31:54.753 回答
2

你有你的任务回到前面。这:

void Heading::storeInfo(string c, string r) {
    c = company;
    r = report;
}

应该是这样的:

void Heading::storeInfo(string c, string r) {
    company = c;
    report = r;
}

正如sehe提到的,定义它:

void Heading::storeInfo(const string& c, const string& r)

允许编译器帮助您避免这样的事情。

于 2013-10-18T00:32:41.853 回答