我在这里搞砸了什么?代码中不能有硬编码的值,这就是为什么我的所有提示都是常量。我们还必须调用用户定义的函数来验证输入。
当我编译时,出现以下错误--undefined reference to WinMain,[Error] Id returned 1 exit status。我正在使用Dev C++作为IDE。
#include <iostream> //for I/O
#include <iomanip> //for formatting output
using namespace std;
const string PROGRAM_DESCRIPTION = "Program will calculate the amount "
"accumulated every month you save, \nuntil you reach your goal. ";
const string ENTER_DOLLAR_AMOUNT_MONTHLY = "Enter the dollar amount to be "
"saved each month: ";
int main()
{
double dollarSavedPerMonth;
//displays program description
cout << PROGRAM_DESCRIPTION << endl << endl;
//Prompts user to enter dollar amount to be saved monthly, will validate
//input by calling VerifyDollar
dollarSavedPerMonth = VerifyDollar(ENTER_DOLLAR_AMOUNT_MONTHLY);
cout << endl;
return 0;
}
double VerifyDollar (string Prompt)
{
const string INVALID_DOLLAR_AMOUNT = "Invalid amount, re-enter monthly "
"savings amount.";
double dollarSaved;
cout << Prompt;
cin >> dollarSaved;
while (dollarSaved < 5 || dollarSaved > 5000)
{
cout << INVALID_DOLLAR_AMOUNT;
cout << endl;
cout << Prompt;
cin >> dollarSaved;
}
return dollarSaved;
}
std::string
时,请包含<string>
头文件。在任何情况下,由于在声明之前使用了VerifyDollar
,因此即使到了链接部分,也不应该出现链接器错误。 - chris