如何初始化已经传递的结构变量?

时间:2016-03-08 05:22:01

标签: c++ io project

好的,所以我们必须改变这个程序,从读取用户的输入到读取文件,到目前为止,我已经改变了一大段代码,它将读取文件,但每次我去运行代码我得到了这5个我无法弄清楚的错误,所以这里是我的代码

// Author:      
// Source file: 
// Description: 
// Compiler used:   

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

struct records
{
    int code;
    double amount;
};
// Function Prototypes
void displayTitle();
double getBegBal(ifstream&);
void displayBal(records);
records getData(ifstream&);
double processCheck(double, double);
double processDeposit(double, double);
double processATM(double, double);
double processSvcChg(double);


//Global Constants
const double    CHARGE = 10,
ATMFEE = 2;

int main()
{
    //Variable Declarations
    int transCode;
    double balance,
        transAmt;


    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    records trans;
    ifstream inFile;
    inFile.open("c:\\checkIn.dat");

    displayTitle();
    balance = getBegBal(inFile);
    getData(inFile);

    while (!inFile.eof())
    {
        trans = getData(inFile);

        switch (trans.code)
        {
        case 1: balance = processCheck(balance, trans.amount); break;
        case 2: balance = processDeposit(balance, trans.amount); break;
        case 3: balance = processATM(balance, trans.amount); break;
        }
        displayBal(trans);
        if (balance < 0)
            balance = processSvcChg(balance);
        getData(inFile);
    }

    return 0;
}


void displayTitle()
{
    cout << "\n       Check Register\n\n";
}

double getBegBal(ifstream& inFile)
{
    //double bal;
    records balance;
    cout << "  Enter beginning balance ";
    inFile >> balance.amount;
    return balance.amount;
}

void displayBal(records balance)
{
    cout << "\t\tBalance = $" << setw(10) << balance.amount;
}

records getData(ifstream& inFile)
{
    records rec;
    cout << "\n\n  Enter transaction code (0 to exit) ";
    inFile >> rec.code;
    if (rec.code > 0)
    {
        cout << "\n  Enter transaction amount ";

    }
    return rec;
}

double processCheck(double bal, double amt)
{
    cout << "\n  Check =    " << setw(10) << amt;
    return (bal - amt);
}

double processDeposit(double bal, double amt)
{
    cout << "\n  Deposit =  " << setw(10) << amt;
    return (bal + amt);
}
double processATM(double bal, double amt)
{
    records trans;

    cout << "\n  ATM     =  " << setw(10) << trans.amount;
    bal = bal - amt;
    displayBal(trans);
    bal = bal - ATMFEE;
    cout << "\n  ATM Fee =  " << setw(10) << ATMFEE;
    return (bal);
}
double processSvcChg(double bal)
{
    records trans;
    cout << "\n  Service chg =" << setw(8) << CHARGE;
    bal = bal - CHARGE;
    displayBal(trans);
    return (bal);
}

错误#2-3在这里

int transCode;
double balance,
    transAmt;

错误是说'transCode':未引用的局部变量和 'transAmt':未引用的局部变量

错误#4-5在这里

double processATM(double bal, double amt)
{
    records trans;

    cout << "\n  ATM     =  " << setw(10) << trans.amount;
    bal = bal - amt;
    displayBal(trans);// the error points here saying that the variable trans is uninitialized 
    bal = bal - ATMFEE;
    cout << "\n  ATM Fee =  " << setw(10) << ATMFEE;
    return (bal);
}
double processSvcChg(double bal)
{
    records trans;
    cout << "\n  Service chg =" << setw(8) << CHARGE;
    bal = bal - CHARGE;
    displayBal(trans); // the error points here saying that the variable trans is uninitialized
    return (bal);
}

请谢谢你的帮助!

3 个答案:

答案 0 :(得分:2)

您已初始化width = 0;并将其传递给getWidth()。因此,width % 2 != 0评估为false,并且getWidth()中的提示不会显示。

除非你的作业需要它,否则

getWidth()不需要任何参数,因为它只是为了读取宽度并重新加入它。

do语句对于在执行循环体一次后评估条件很有用。

int getWidth()
{
    int width = 0;
    do {
        cout << "Enter width " << endl;
        cin >> width;
    } while (width % 2 != 0);
    return width;
}

然后,在getWidth()函数中使用main(),如下所示:

width = getWidth();

答案 1 :(得分:1)

它为我工作。

int displayMenu();
void displaySquare(int width);
void displayTriangle(int width);
int getWidth(); 
void displayUpsideDownTriangle(int width);
void displayDiamond(int width);

int main()
{
    int width, shapes;
    do {

        shapes = displayMenu();
        width = 0;

        switch (shapes)
        {
        case 1: 
            width = getWidth();
            displaySquare(width);
            break;
        case 2: 
            width = getWidth();
            displayTriangle(width);
            break;
        case 3: 
            width = getWidth();
            displayUpsideDownTriangle(width);
            break;
        case 4: 
            width = getWidth();
            displayDiamond(width);
            break;
        case -9: 
            cout << "End of Program " << endl;
        default: 
            cout << "Please choose one of the shapes..." << endl;


        }

    } while (shapes != -9);




    system("pause");
    return 0;
}
//this function sets up the display for the user
int displayMenu() {
    int shapes;
    cout << "\n~~ Shape Display menu ~~ " << endl << endl;
    cout << "     1....Square\n" <<
        "     2....Triangle\n " <<
        "    3....Upside Down triangle\n " <<
        "    4....Diamond\n\n " <<
        "   -9....Exit Program " << endl;
    cout << endl;
    cout << "  Make a selection " << endl;
    cin >> shapes;
    return shapes;

}
int getWidth()
{
    int width = 1;
    do {
        if (width % 2 != 0)
        {
            cout << "Enter width " << endl;
            cin >> width;
        }
        else
        {
            cout << "Please enter odd number only. \nEnter width " << endl;
            cin >> width;
        }
    } while (width % 2 == 0);

    return width;
}
void displaySquare(int width)
{

    int  rows, columns;


    for (rows = 0; rows < width; ++rows)
    {
        for (columns = 0; columns < width; ++columns)
        {
            cout << "# ";
        }

        cout << endl;
    }

}
void displayTriangle(int width)
{

        int rows, Spacing, ColHashtag;


        for (rows = 1; rows < width; rows++) //controls the rows 
        {
            for (Spacing = (width - rows); Spacing >= 1; Spacing--) // spaces out the rows to make an isoceles triangle
            {
                cout << " ";
            }
            for (ColHashtag = 1; ColHashtag <= (rows * 2) - 1; ColHashtag++) //controls the columns 
            {
                cout << "#";
            }
            cout << endl;

        }


}
void displayUpsideDownTriangle(int width)
{
    int rows, Columns, spacing;

    //sets up the rows for the top 
    for ((rows = width - 1); rows >= 1; rows--)
    {
        for (Columns = 1; Columns <= width - rows; Columns++) // sets up the columns 
        {
            cout << " "; // spaces out the symbols to make an isoceles triangle 
        }
        for (spacing = 1; spacing <= 2 * rows - 1; spacing++)
        {
            cout << "#";
        }
        cout << endl;
    }

}
void displayDiamond(int width)
{
    displayTriangle(width);
    displayUpsideDownTriangle(width);
}

答案 2 :(得分:0)

用这个

替换int main()和getWidth()
 int main()
{
int width, shapes,wt;  
do {

cout << "Enter width " << endl;
cin >> width; 
    wt=getWidth(width);

    if(wt!=0)
    {
    shapes = displayMenu();
    }
    else
    {
        shapes=-9;
    }
    switch (shapes)
    {
    case 1: 
        displaySquare(wt);
        break;
    case 2: 
        displayTriangle(wt);
        break;
    case 3: 
        displayUpsideDownTriangle(wt);
        break;
    case 4: 
        displayDiamond(wt);
        break;
    case -9: 
        cout << "End of Program " << endl;
        break;
    default: 
        cout << "Please choose one of the shapes..." << endl;


    }

} while (shapes != -9);

system("pause");
return 0;
}

 int getWidth(int width)
{

while (width % 2 != 0) {
    cout << "Enter width " << endl;
    cin >> width;
}
return width; 
}