C++ 带字符串消息的函数声明
编译时,int-getValue函数的声明出现“Error:'message'未在此范围内声明”错误 此函数用于获取用户输入的整数,并将其传递给main 功能 我是否正确地声明了函数C++ 带字符串消息的函数声明,c++,function,function-declaration,C++,Function,Function Declaration,编译时,int-getValue函数的声明出现“Error:'message'未在此范围内声明”错误 此函数用于获取用户输入的整数,并将其传递给main 功能 我是否正确地声明了函数 #include <iostream> #include <string> using namespace std; int getValue(message); // Compiler message: [Error] 'message' was not declared in t
#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;
}
#包括
#包括
使用名称空间std;
int getValue(消息);
//编译器消息:[错误]“消息”未在此范围内声明。
信件(信息);
int main()
{
int this year,this month,year,month,ageYear,ageMonth;
char再次='y';
字符串消息;
//显示程序指令
cout函数声明中缺少类型。例如:
应该更改为:int-getValue(const-std::string&message);
创建函数声明时,必须编写参数将使用的数据类型。这适用于您编写的所有函数,无论它们是全局函数还是范围内函数
改变
intgetvalue(消息);
chargetletter(消息);
到
intgetvalue(常量字符串和消息);
char getLetter(char message);
一般来说,我不建议按值传递类类型。什么意思?通过使用字符串?我指的是传递std::string
(即类)的事实通过值作为getValue
函数中的参数。哦,是的,我理解你的意思,但是对于这个例子,op使用字符串,所以我认为最好使用它。是的,它在我的编辑器的第41行。它是getValue(字符串消息)=今年;
行:“message=”以4位数字输入今天的年份“`getValue(string message)=今年;
message=”以2位数字输入今天的月份“
getValue(string message)=本月;
int getValue( message);
// ^^^^^^^ type?