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

C++ 如何初始化已传递的结构变量?,c++,io,project,C++,Io,Project,好的,所以我们必须把这个程序从读取用户输入改为从文件中读取,到目前为止,我已经修改了一段代码,这段代码将从文件中读取,但每次我运行代码时,我都会发现这5个错误,我无法理解,所以这是我的代码 // Author: // Source file: // Description: // Compiler used: #include <iostream> #include <iomanip> #include <fstream> using

好的,所以我们必须把这个程序从读取用户输入改为从文件中读取,到目前为止,我已经修改了一段代码,这段代码将从文件中读取,但每次我运行代码时,我都会发现这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);
}
错误为“transCode”:未引用的局部变量和 “transAmt”:未引用的局部变量

错误4-5在这里

int transCode;
double balance,
    transAmt;
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);
}
双流程ATM(双倍余额,双倍金额)
{
记录转换;

无法您初始化了
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;
}
这对我有用

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);
}
int displayMenu();
空白显示正方形(整数宽度);
空白显示三角形(整数宽度);
int getWidth();
void displayUpsideDownTriangle(内部宽度);
空白显示菱形(整数宽度);
int main()
{
int宽度、形状;
做{
形状=显示菜单();
宽度=0;
开关(形状)
{
案例1:
宽度=getWidth();
显示正方形(宽度);
打破
案例2:
宽度=getWidth();
显示三角形(宽度);
打破
案例3:
宽度=getWidth();
显示UpsideDownTriangle(宽度);
打破
案例4:
宽度=getWidth();
显示菱形(宽度);
打破
案例9:
cout将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; 
}
intmain()
{
int宽度、形状、wt;
做{
库特宽度;
wt=getWidth(宽度);
如果(wt!=0)
{
形状=显示菜单();
}
其他的
{
形状=-9;
}
开关(形状)
{
案例1:
显示广场(wt);
打破
案例2:
显示三角形(wt);
打破
案例3:
显示UpsideDownTriangle(wt);
打破
案例4:
显示钻石(wt);
打破
案例9:

您能告诉我为什么需要在getWidth函数中循环获取宽度吗?
getWidth()
返回读取的宽度,然后将其丢弃。捕获它!此函数显示提示,请求宽度值,并返回宽度整数值。宽度必须是奇数,因此,如果输入的不是奇数,则向用户显示错误消息,并请求有效的输入。循环直到输入奇数。不要如果菜单选择为-9或选择无效,则从main调用此函数。@DaoXio此函数显示一条提示,要求输入宽度值,并返回宽度的整数值。宽度必须是奇数,因此如果输入不是奇数,则向用户显示错误消息,并请求有效输入。循环直到输入了一个奇数。如果菜单选择为-9或选择无效,请不要从main调用此函数。如果我这样做,则我必须重做所有调用width的函数。这就是答案。您将只更改getWidth()函数。将原型从
int getWidth(int width);
更改为
int getWidth()
好的,我试过了,程序甚至没有运行,它给出了以下错误:etwidth:function对所有四个shapes@BartholomewAllen请将
getWidth()
的用法更改为我所写的用法。好的,现在我明白你的意思了!谢谢!它可以工作了!:D
 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; 
}