我正在为一项任务开发这个程序,所以我不想要答案……只是朝着正确的方向前进。
我编写了一个程序,用户将在其中输入一个 3 位数的 ID(必需)和当月使用的 KWh 量。输入该标准后,它将打印出一个简短的摘要 + 每个用户该月的费用。这包含在一个循环中,询问是否有更多的 ID 需要输入。
下面是我的打开代码和循环结构:
#include <iostream>
#include <string>
#include <iomanip>
#include <windows.h>
using namespace std;
//function prototypes
string get_user_ID();
int get_cust_kwh();
string display_cust_data();
int main()
{
string userID;
int userKWH;
char answer;
while ((answer !='N')&&(answer !='n')){
    userID = get_user_ID();
    userKWH =get_cust_kwh();
    display_cust_data();
    cout << "\n\nWould you like to add another utility ID? : (Y or N)";
    cin >> answer;
    }
}
system("pause")
return 0;
我可以让循环正常运行,但我的函数似乎没有返回用户输入的值。由于我是函数新手,我确信这是一个愚蠢的错误,但它一直让我发疯。以下是功能:
**********function definitions**********
string get_user_ID(){
    string usrID;
    cout << "\n\nPlease enter in your 3-digit utility ID: ";
    cin >> usrID;
    return usrID;
}
int get_cust_kwh(){
    int usrKWHs;
    cout << "Please enter the total KWH used for the month: ";
     cin >> usrKWHs;
    return usrKWHs;
}
string display_cust_data(){
    string usrID;
    int usrKWHs;
    double userCharge;
    cout << "\n\nUSER ID        KWHours     Charge($)\n";
    cout << fixed << setprecision(2);
    cout << usrID << "            " << usrKWHs << "           " << userCharge << endl;
   }
我正在考虑与函数中变量的使用方式有关。
感谢您提供的任何推动!