带有字符串消息的函数声明
Function declaration with string message
我在编译时收到关于 int getValue 函数声明的 "Error: 'message' was not declared in this scope" 错误。
这个函数应该接受用户输入的整数并将它们传递给主函数
功能。
我是否正确声明了函数?
#include <iostream>
#include <string>
using namespace std;
int getValue(message);
// Compiler message: [Error] 'message' was not declared in this scope.
char getLetter(message);
int main()
{
int thisYear, thisMonth, year, month, ageYear, ageMonth;
char again = 'y';
string message;
// display program instructions
cout << "This program asks you to enter today's year in 4 digits,\n"
<< "and today's month number.\n\n"
<< "Then you will be asked to enter your birth in 4 digits,\n"
<< "and your birth month in 2 digits.\n\n"
<< "The program will calculate and display your age in years and months.\n";
message="Enter today's year in 4 digits";
getValue(message)==thisYear;
message="Enter today's month in 2 digits";
getValue(message)==thisMonth;
do
{
message="Enter your birth year in 4 digits";
getValue(message)==year;
message="Enter your birth month in 2 digits";
getValue(message)==month;
ageYear = thisYear - year;
ageMonth = thisMonth - month;
if (thisMonth < month)
{
ageYear--;
ageMonth += 12;
}
cout << "\nYou are " << ageYear << " years and " << ageMonth << " months old.\n";
message="Do you want to calculate another age? (y/n)";
getLetter(message)==again;
again = tolower(again);
}while (again == 'y');
return 0;
}
/* the function getValue returns an integer value
entered by the user in response to the prompt
in the string message */
int getValue(message)
{
// declare variables
// declare an integer value to enter a value
int value;
cout << message;
cin >> value;
return value;
}
/* the function getLetter returns a character value
entered by the user in response to the prompt
in the string message */
char getLetter(message)
{
char letter;
cout << " Do you wish to enter another date? (y/n)";
cin >> letter;
return letter;
}
您在函数声明中缺少类型。例如:
int getValue( message);
// ^^^^^^^ type?
应该更改为:int getValue(const std::string& message);
.
在创建函数声明时,您必须写明参数将使用哪种数据类型。这适用于您编写的所有函数;它们是全局函数还是范围内函数。
改变
int getValue(message);
char getLetter(message);
到
int getValue(const string& message);
char getLetter(char message);
我在编译时收到关于 int getValue 函数声明的 "Error: 'message' was not declared in this scope" 错误。
这个函数应该接受用户输入的整数并将它们传递给主函数 功能。
我是否正确声明了函数?
#include <iostream>
#include <string>
using namespace std;
int getValue(message);
// Compiler message: [Error] 'message' was not declared in this scope.
char getLetter(message);
int main()
{
int thisYear, thisMonth, year, month, ageYear, ageMonth;
char again = 'y';
string message;
// display program instructions
cout << "This program asks you to enter today's year in 4 digits,\n"
<< "and today's month number.\n\n"
<< "Then you will be asked to enter your birth in 4 digits,\n"
<< "and your birth month in 2 digits.\n\n"
<< "The program will calculate and display your age in years and months.\n";
message="Enter today's year in 4 digits";
getValue(message)==thisYear;
message="Enter today's month in 2 digits";
getValue(message)==thisMonth;
do
{
message="Enter your birth year in 4 digits";
getValue(message)==year;
message="Enter your birth month in 2 digits";
getValue(message)==month;
ageYear = thisYear - year;
ageMonth = thisMonth - month;
if (thisMonth < month)
{
ageYear--;
ageMonth += 12;
}
cout << "\nYou are " << ageYear << " years and " << ageMonth << " months old.\n";
message="Do you want to calculate another age? (y/n)";
getLetter(message)==again;
again = tolower(again);
}while (again == 'y');
return 0;
}
/* the function getValue returns an integer value
entered by the user in response to the prompt
in the string message */
int getValue(message)
{
// declare variables
// declare an integer value to enter a value
int value;
cout << message;
cin >> value;
return value;
}
/* the function getLetter returns a character value
entered by the user in response to the prompt
in the string message */
char getLetter(message)
{
char letter;
cout << " Do you wish to enter another date? (y/n)";
cin >> letter;
return letter;
}
您在函数声明中缺少类型。例如:
int getValue( message); // ^^^^^^^ type?
应该更改为:int getValue(const std::string& message);
.
在创建函数声明时,您必须写明参数将使用哪种数据类型。这适用于您编写的所有函数;它们是全局函数还是范围内函数。
改变
int getValue(message);
char getLetter(message);
到
int getValue(const string& message);
char getLetter(char message);