新手问题 - 编码标准 - 我该怎么写呢 [英] Newbie question - coding standards - how should I write this
问题描述
我正在尝试写一个程序来计算执行程序的当前年龄。通过询问2个问题来完成:
- 当年是什么
- 你的出生年份是什么
在用户输入这些之后,需要计算用户的当前年龄。
我完成了它,因为它并不是那么困难。但是我想按照标准来做这件事,我想把主要的函数原型和main下面的实际函数定义。然而,我无法让它工作。
这就是我现在所拥有的但如果我这样做的话,''cout part''正在显示但只有文字显示这是你现在的年龄。由此判断,我猜下一行的部分出了问题:cout<< x(a,b)<< endl;
有人有什么建议吗?
这就是我现在所拥有的:
#include< vcl.h> //需要Borland
#include< iostream> //需要输入输出
使用命名空间std;
// -------------- -------------------------------------------------- -----------
int x(int,int); / *函数声明
或原型* /
int main()
{
int a; //声明变量
int b; //声明变量
cout<< 在此输入当前年份:;
cin>> a;
cout<< 输入出生年份:;
cin>> b;
cout<< 这是你现在的年龄:" ;;
cout<< x(a,b)<< endl;
返回0;
}
int x(int a,int b)//函数定义
{
int x =(a - b);
int d;
cin>> d;
返回x;
}
我正在尝试写一个程序来计算执行程序的当前年龄。通过询问2个问题来完成:
- 当年是什么
- 你的出生年份是什么
在用户输入这些之后,需要计算用户的当前年龄。
我完成了它,因为它并不是那么困难。但是我想按照标准来做这件事,我想把主要的函数原型和main下面的实际函数定义。然而,我无法让它工作。
这就是我现在所拥有的但如果我这样做的话,''cout part''正在显示但只有文字显示这是你现在的年龄。由此判断,我猜下一行的部分出了问题:cout<< x(a,b)<< endl;
有人有什么建议吗?
这就是我现在所拥有的:
#include< vcl.h> //需要Borland
#include< iostream> //需要输入输出
使用命名空间std;
// -------------- -------------------------------------------------- -----------
int x(int,int); / *函数声明
或原型* /
int main()
{
int a; //声明变量
int b; //声明变量
cout<< 在此输入当前年份:;
cin>> a;
cout<< 输入出生年份:;
cin>> b;
cout<< 这是你现在的年龄:" ;;
cout<< x(a,b)<< endl;
返回0;
}
int x(int a,int b)//函数定义
{
int x =(a - b);
int d;
cin>> d;
返回x;
}
你的代码是完全正确但我不喜欢不明白为什么你已经采取了
展开 | 选择 | Wrap | 行号
这是因为你的函数x(int,int)内部问对于将存储在int d变量中的另一个用户输入,我已经评论过,现在它应该如下工作。
展开 | 选择 | Wrap | 行号
展开 | 选择 | Wrap | 行号
I am trying to write to a program that will compute the current age of the one executing the program. It is done by asking 2 questions:
- What is the current year
- What is your birth year
After these have been entered by the user the current age of the user needs to be computed.
I accomplished it because it is not all that difficult. However I want to do it according to standards and I would like to have the function prototype above main and the actual function definition below main. I am however not able to get it to work.
This is what I have now but if I do it like this the ''cout part'' is being displayed but only the text ''this is your age now''. Judging by this I guess something is wrong with the part on the next line: cout << x (a, b) << endl;
Does anybody have any suggestions?
This is what I have now:
#include <vcl.h> //needed for Borland
#include <iostream> //needed for input output
using namespace std;
//---------------------------------------------------------------------------
int x (int, int); /* function declaration
or prototype */
int main()
{
int a; //declaration variables
int b; //declaration variables
cout << "Type current year here: ";
cin >> a;
cout << "Type year of birth: ";
cin >> b;
cout << "This is your age now: ";
cout << x (a, b) << endl;
return 0;
}
int x (int a, int b) // function definition
{
int x = (a - b);
int d;
cin >> d;
return x;
}解决方案I am trying to write to a program that will compute the current age of the one executing the program. It is done by asking 2 questions:
- What is the current year
- What is your birth year
After these have been entered by the user the current age of the user needs to be computed.
I accomplished it because it is not all that difficult. However I want to do it according to standards and I would like to have the function prototype above main and the actual function definition below main. I am however not able to get it to work.
This is what I have now but if I do it like this the ''cout part'' is being displayed but only the text ''this is your age now''. Judging by this I guess something is wrong with the part on the next line: cout << x (a, b) << endl;
Does anybody have any suggestions?
This is what I have now:
#include <vcl.h> //needed for Borland
#include <iostream> //needed for input output
using namespace std;
//---------------------------------------------------------------------------
int x (int, int); /* function declaration
or prototype */
int main()
{
int a; //declaration variables
int b; //declaration variables
cout << "Type current year here: ";
cin >> a;
cout << "Type year of birth: ";
cin >> b;
cout << "This is your age now: ";
cout << x (a, b) << endl;
return 0;
}
int x (int a, int b) // function definition
{
int x = (a - b);
int d;
cin >> d;
return x;
}Your code is quite correct but I don''t understand why you have taken
Expand|Select|Wrap|Line Numbers
It''s because inside your function x(int, int) ask for another user input that will be stored in the int d variable, I already commented that, now it should work as below.
Expand|Select|Wrap|Line Numbers
Expand|Select|Wrap|Line Numbers
这篇关于新手问题 - 编码标准 - 我该怎么写呢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文登录 关闭
扫码关注1秒登录发送“验证码”获取 | 15天全站免登陆