如何在另一个函数中访问一个函数中的变量
How to access a variable in one function in another function?
我正在尝试编写一个C++代码,其中用户输入一个日期,并使用John Conway的末日算法输出该日期落下的星期几。
我正在尝试访问在函数 dayofthemonth 中声明和定义的变量 z,并在名为 dayoftheweek 的函数中使用它。请记住,我对C++很陌生,所以如果你能尽可能简单地回答,那将有很大帮助。代码如下:
#include <iostream>
#include <string>
using namespace std;
int dayofthemonth (int year, int month){
int z;
if ((year % 400 == 0 || year % 100 !=0) && (year % 4 == 0)){ //reference day of the month for leap years
cout << year << " is a leap year." << endl;
if (month == 1){
cout << "The doomsday for month " << month << " is 4." << endl;
z = 4;
}
if (month == 2){
cout << "The doomsday for month " << month << " is 1." << endl;
z = 1;
}
if (month == 3){
cout << "The doomsday for month " << month << " is 0." << endl;
z = 0;
}
if (month == 4){
cout << "The doomsday for month " << month << " is 4." << endl;
z = 4;
}
if (month == 5){
cout << "The doomsday for month " << month << " is 9." << endl;
z = 9;
}
if (month == 6){
cout << "The doomsday for month " << month << " is 6." << endl;
z = 6;
}
if (month == 7){
cout << "The doomsday for month " << month << " is 11." << endl;
z = 11;
}
if (month == 8){
cout << "The doomsday for month " << month << " is 8." << endl;
z = 8;
}
if (month == 9){
cout << "The doomsday for month " << month << " is 5." << endl;
z = 5;
}
if (month == 10){
cout << "The doomsday for month " << month << " is 10." << endl;
z = 10;
}
if (month == 11){
cout << "The doomsday for month " << month << " is 7." << endl;
z = 7;
}
if (month == 12){
cout << "The doomsday for month " << month << " is 12." << endl;
z = 12;
}
}
else{ //reference day of the month for non-leap years
cout << year << " is not a leap year." << endl;
if (month == 1){
cout << "The doomsday for month " << month << " is 3." << endl;
z = 3;
}
if (month == 2){
cout << "The doomsday for month " << month << " is 0." << endl;
z = 0;
}
if (month == 3){
cout << "The doomsday for month " << month << " is 0." << endl;
z = 0;
}
if (month == 4){
cout << "The doomsday for month " << month << " is 4." << endl;
z = 4;
}
if (month == 5){
cout << "The doomsday for month " << month << " is 9." << endl;
z = 9;
}
if (month == 6){
cout << "The doomsday for month " << month << " is 6." << endl;
z = 6;
}
if (month == 7){
cout << "The doomsday for month " << month << " is 11." << endl;
z = 11;
}
if (month == 8){
cout << "The doomsday for month " << month << " is 8." << endl;
z = 8;
}
if (month == 9){
cout << "The doomsday for month " << month << " is 5." << endl;
z = 5;
}
if (month == 10){
cout << "The doomsday for month " << month << " is 10." << endl;
z = 10;
}
if (month == 11){
cout << "The doomsday for month " << month << " is 7." << endl;
z = 7;
}
if (month == 12){
cout << "The doomsday for month " << month << " is 12." << endl;
z = 12;
}
}
}
int doomsday (int year){ //reference day of the week
int a, b, c , d, e, x;
a = ((year/100) % 4);
b = (year % 100);
c = (b/12);
d = (b % 12);
e = (d/4);
x = ((c + d + e + (5*a) + 2) % 7);
if (x == 0){
cout << "The doomsday for " << year << " is Sunday." << endl;
}
else if (x == 1){
cout << "The doomsday for " << year << " is Monday." << endl;
}
else if (x == 2){
cout << "The doomsday for " << year << " is Tuesday." << endl;
}
else if (x == 3){
cout << "The doomsday for " << year << " is Wednesday." << endl;
}
else if (x == 4){
cout << "The doomsday for " << year << " is Thursday." << endl;
}
else if (x == 5){
cout << "The doomsday for " << year << " is Friday." << endl;
}
else if (x == 6){
cout << "The doomsday for " << year << " is Saturday." << endl;
}
}
void dayoftheweek(int month, int day, int year}(
int r;
cout << "You want to find out what day of the week " << month << "/" << day << "/" << year << " lies on." << endl;
doomsday(year);
r = (day - z) + 14; //offset between the given day and the result of dayofthemonth function.
cout << r << endl;
}
int main(){
int year, month, day;
cout << "Enter the year." << endl;
cin >> year;
cout << "Enter the month using numbers 1-12." << endl;
cin >> month;
cout << "Enter the day." << endl;
cin >> day;
dayoftheweek(month, day, year);
system ("PAUSE");
return 0;
}
您的函数具有返回类型 int
,但实际上不返回任何值。返回 z
,然后将其作为参数传递给下一个函数。
另一个观察结果是,这个函数int dayofthemonth (int year, int month)
没有调用者。
也许你应该改变为,
r = (day - z) + 14;
至
r = (day - dayofthemonth(year, month)) + 14;
//前提是此函数返回z
。
如果你想要一种简单的方法来在代码的所有作用域中使用一个变量,你可以在main函数之前和"#include <...>"s和"using namespace..."之后声明它。但是当你使用这种方式时要小心,因为如果你在代码中使用变量 2 或更多时间并且不将其设为 0,你会看到错误的答案。最后,我在代码中使用了"cin.get()"而不是"system("pause")",因为如果你想在Linux中编译你的代码,"system("pause")"会给你一个错误。
#include <iostream>
#include <string>
using namespace std;
int z;
void dayofthemonth (int year, int month)
{
z = 0;
if ((year % 400 == 0 || year % 100 !=0) && (year % 4 == 0)){ //reference day of the month for leap years
cout << year << " is a leap year." << endl;
if (month == 1){
cout << "The doomsday for month " << month << " is 4." << endl;
z = 4;
}
if (month == 2){
cout << "The doomsday for month " << month << " is 1." << endl;
z = 1;
}
if (month == 3){
cout << "The doomsday for month " << month << " is 0." << endl;
z = 0;
}
if (month == 4){
cout << "The doomsday for month " << month << " is 4." << endl;
z = 4;
}
if (month == 5){
cout << "The doomsday for month " << month << " is 9." << endl;
z = 9;
}
if (month == 6){
cout << "The doomsday for month " << month << " is 6." << endl;
z = 6;
}
if (month == 7){
cout << "The doomsday for month " << month << " is 11." << endl;
z = 11;
}
if (month == 8){
cout << "The doomsday for month " << month << " is 8." << endl;
z = 8;
}
if (month == 9){
cout << "The doomsday for month " << month << " is 5." << endl;
z = 5;
}
if (month == 10){
cout << "The doomsday for month " << month << " is 10." << endl;
z = 10;
}
if (month == 11){
cout << "The doomsday for month " << month << " is 7." << endl;
z = 7;
}
if (month == 12){
cout << "The doomsday for month " << month << " is 12." << endl;
z = 12;
}
}
else{ //reference day of the month for non-leap years
cout << year << " is not a leap year." << endl;
if (month == 1){
cout << "The doomsday for month " << month << " is 3." << endl;
z = 3;
}
if(month == 2){
cout << "The doomsday for month " << month << " is 0." << endl;
z = 0;
}
if (month == 3){
cout << "The doomsday for month " << month << " is 0." << endl;
z = 0;
}
if (month == 4){
cout << "The doomsday for month " << month << " is 4." << endl;
z = 4;
}
if (month == 5){
cout << "The doomsday for month " << month << " is 9." << endl;
z = 9;
}
if (month == 6){
cout << "The doomsday for month " << month << " is 6." << endl;
z = 6;
}
if (month == 7){
cout << "The doomsday for month " << month << " is 11." << endl;
z = 11;
}
if (month == 8){
cout << "The doomsday for month " << month << " is 8." << endl;
z = 8;
}
if (month == 9){
cout << "The doomsday for month " << month << " is 5." << endl;
z = 5;
}
if (month == 10){
cout << "The doomsday for month " << month << " is 10." << endl;
z = 10;
}
if (month == 11){
cout << "The doomsday for month " << month << " is 7." << endl;
z = 7;
}
if (month == 12){
cout << "The doomsday for month " << month << " is 12." << endl;
z = 12;
}
}
}
void doomsday (int year)
{ //reference day of the week
int a, b, c , d, e, x;
a = ((year/100) % 4);
b = (year % 100);
c = (b/12);
d = (b % 12);
e = (d/4);
x = ((c + d + e + (5*a) + 2) % 7);
if (x == 0)
cout << "The doomsday for " << year << " is Sunday." << endl;
else if (x == 1)
cout << "The doomsday for " << year << " is Monday." << endl;
else if (x == 2)
cout << "The doomsday for " << year << " is Tuesday." << endl;
else if (x == 3)
cout << "The doomsday for " << year << " is Wednesday." << endl;
else if (x == 4)
cout << "The doomsday for " << year << " is Thursday." << endl;
else if (x == 5)
cout << "The doomsday for " << year << " is Friday." << endl;
else if (x == 6)
cout << "The doomsday for " << year << " is Saturday." << endl;
}
void dayoftheweek(int month, int day, int year)
{
int r;
cout << "You want to find out what day of the week " << month << "/" << day << "/" << year << " lies on." << endl;
doomsday(year);
r = (day - z) + 14; //offset between the given day and the result of dayofthemonth function.
cout << r << endl;
}
int main(){
int year, month, day;
cout << "Enter the year." << endl;
cin >> year;
cout << "Enter the month using numbers 1-12." << endl;
cin >> month;
cout << "Enter the day." << endl;
cin >> day;
dayoftheweek(month, day, year);
cin.get();
return 0;
}
相关文章:
- 函数向量_指针有不同的原型,我可以构建一个吗
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 如何仅为一个函数添加延迟
- 构造函数正在调用一个使用当前类类型的函数
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 如何创建函数管道,以便函数一个接一个地运行?
- 如何巧妙地编写两个函数——一个用于检查是否存在解决方案,另一个用于获取所有解决方案
- 在c++中的复制构造函数/一个声明语句中的初始化的延续中使用chain方法
- C :基类调用自己的虚拟函数 - 一个反图案
- 如何在这个交换函数(一个单独的链表)中找到错误
- 两个相同的函数(一个使用模板模式,另一个不使用)
- 你怎么能一次给一个函数一个参数呢
- 为什么要做两个函数?(一个是非const,另一个是const)
- 当代码在其他地方使用时,如何保证函数一个接一个地被调用