我在这里使用的代码非常不错,除非我们开始使用' processData'函数我收到错误
"变量' totalPay'正在使用而未被初始化。"
我已经在这个工作了几个小时,我的大脑麻木了,任何帮助都会受到赞赏。
#include <iostream>
#include <string>
#include <climits>
#include <iomanip>
using namespace std;
// Prototypes
int readInt(string errorMsg = "", int min = INT_MIN, int max = INT_MAX);
void getData(string&, int&, char&, double&);
char getChoice(string prompt, char char1, char char2);
double processData(char service);
void display(string, int, char, double);
// Regular service
const double RCHARGE = 10.00; // Regular service base rate for first REG_MINS.
const double RPERMIN = 0.20; // Regular service cost per minute over REG_MINUTES.
const int REG_MINS = 50; // Regular service free minutes.
// Premium service
const double PCHARGE = 25.00; // Minimum charge for premium service.
const double P_PER_MIN_DAY = 0.10; // Charge per day minutes after 75.
const double P_PER_MIN_NIGHT = 0.05; // Charge per night minutes after 100.
const int P_DAY = 75; // Number of free minutes allowed during the day.
const int P_NIGHT = 100; // Number of free minutes allowed during the night.
int main()
{
string name; // Stores the users name.
int accountNumber; // Stores the users account number.
char serviceType; // Stores the users service type.
double minutes; // Stores the users minutes.
double totalPay; // Recieves the total pay from processData.
bool loopies = true;// Controls the loop while condition is true.
while (loopies == true)
{
getData(name, accountNumber, serviceType, minutes);
totalPay = processData(serviceType);
display(name, accountNumber, serviceType, totalPay);
}
return 0;
}
// Checks ints to make sure they are valid.
int readInt(string errorMsg, int min, int max)
{
int someInt;
bool valid = false;
do
{
// User entering account number
cin >> someInt;
// Verifying the data is valid (0 to 9999)
valid = (someInt >= min && someInt <= max);
// Clearing out invalid data and prompting for re-entry
if (cin.fail() || !valid || !isspace(cin.peek()))
{
cout << "Error! Invalid input." << endl;
cout << errorMsg << " must be whole number between " << min << " and " << max << endl;
cout << "Try Again: ";
cin.clear(); // Clearing the fail state
cin.ignore(numeric_limits<streamsize>::max(), '\n');
valid = false;
}
} while (!valid);
// Clears out following incorrect data once correct
cin.ignore(100, '\n');
return someInt;
}
// Takes all user data and checks input.
void getData(string &name, int &account, char &service, double &bill)
{
cout << "Enter customer name or \"Exit\" to end program: ";
getline(cin, name);
if (name == "Exit" || name == "EXIT" || name == "exit")
{
cout << "Program closing..." << endl;
system("pause");
exit(EXIT_SUCCESS);
}
cout << "Enter account number: ";
account = readInt("Account", 0, 10000);
cout << "Enter service type (R for regular or P for premium): ";
service = getChoice("Service", 'r', 'p' );
}
char getChoice(string input, char char1, char char2)
{
char character; // Stores the users character.
bool valid = false; // Controls the loop.
do
{
cin >> character;
character = tolower(character);
valid = (character == char1 || character == char2);
if (!valid || !isspace(cin.peek())) {
cout << "Invalid input!" << endl;
cout << input << " needs to be " << char1 << " or " << char2 << endl;
cout << "Try again: ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
valid = false;
}
} while (!valid);
cin.ignore(100, '\n');
return toupper(character);
}
double processData(char service)
{
int dayMin; // Stores the users day minutes.
int nightMin; // Stores the users night minutes.
double totalPay; // Stores the total pay.
double dayPay; // Stores the pay for day minutes.
double nightPay; // Stores the pay for night minutes.
if (service == 'r')
{
cout << "Enter minutes used: ";
cin >> dayMin;
dayMin = readInt("Minutes ", 0, INT_MAX);
if (dayMin > REG_MINS)
{
dayMin -= REG_MINS;
totalPay = (dayMin * RPERMIN) + RCHARGE;
}
else
{
totalPay = RCHARGE;
}
totalPay = nightPay + dayPay - RCHARGE;
}
else if (service == 'p')
{
cout << "Enter day minutes: ";
cin >> dayMin;
dayMin = readInt("Minutes ", 0, INT_MAX);
if (dayMin > P_DAY)
{
dayMin -= P_DAY;
dayPay = (dayMin * P_PER_MIN_DAY) + PCHARGE;
}
else
{
dayPay = PCHARGE;
}
cout << "Enter night minutes: ";
cin >> nightMin;
nightMin = readInt("Minutes ", 0, INT_MAX);
if (nightMin > P_NIGHT)
{
nightMin -= P_NIGHT;
nightPay = (nightMin * P_PER_MIN_NIGHT) + PCHARGE;
}
else
{
nightPay = PCHARGE;
}
}
return totalPay;
}
void display(string name, int account, char service, double bill)
{
string switchService;
switch (service)
{
case 'R':
switchService = "Regular";
break;
case 'P':
switchService = "Premium";
break;
default:
cout << "Invalid character.";
}
cout << fixed << setprecision(2);
cout << endl;
cout << "Customer Name: " << name << endl;
cout << "Account number: " << account << endl;
cout << "Service type: " << switchService << endl;
cout << "Amount due: $" << bill << endl << endl;
}
答案 0 :(得分:1)
在processData()
中,如果service
既不是p
也不是r
,则在返回之前永远不会分配totalPay
。
您可以在声明中初始化它,如
double totalPay = 0.0;