带字符串消息的函数声明

时间:2016-02-23 02:39:12

标签: c++ function function-declaration

我在编译时得到了一个“错误:'消息'未在此范围内声明”错误,因为int getValue函数的声明。

该函数应该将用户输入的整数传递给主函数 功能。

我是否正确宣布了这些功能?

#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;
}

2 个答案:

答案 0 :(得分:4)

您必须编写参数在创建函数声明时将使用的数据类型。这适用于您编写的所有功能;它们是全局的还是范围内的功能。

更改

int getValue(message);

char getLetter(message);

int getValue(const string& message);

char getLetter(char message);

答案 1 :(得分:3)

您缺少函数声明中的类型。例如:

int getValue(        message);
//           ^^^^^^^ type?

应更改为:int getValue(const std::string& message);