C++ 通过函数传递静态变量时出现问题

C++ 通过函数传递静态变量时出现问题,c++,C++,我想我只是做得太多太快了,我还在上第一门课,现在还早。我在这个网站上搜索过,但即使是类似问题的答案现在也没有点击 我想要做的就是保留我的用户输入变量,并将它们传递到他们计算然后打印的时候,甚至写出来听起来很简单,但我被卡住了 #include <iostream> #include <iomanip> #include <string> using namespace std; // Function Prototypes void getTime12();

我想我只是做得太多太快了,我还在上第一门课,现在还早。我在这个网站上搜索过,但即使是类似问题的答案现在也没有点击

我想要做的就是保留我的用户输入变量,并将它们传递到他们计算然后打印的时候,甚至写出来听起来很简单,但我被卡住了

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

// Function Prototypes
void getTime12();
void getTime24();
void convert24to12();
void convert12to24();
void print24();
void print12();
void menu();

int main()
{

    int choice;

    do
    {

    // Display menu options
    menu();
    cin >> choice;

    if (choice == '1')
    {
        getTime12();
        convert12to24();
        print24();
    }

    else if (choice == '2')
    {
        getTime24();
        convert24to12();
        print12();
    }

} while (choice != '99');

system("pause");
return 0;
}

void menu()
{
    //Declare variabls to return
cout << endl;
cout << "1. To convert time from 12-hour notation to 24-hour notation."   << endl;
cout << "2. To convert time from 24-hour notation to 12-hour notation." << endl;
cout << "99. To quit the program." << endl << endl;
cout << "Your choice (1, 2, 99): ";
cout << endl << endl;
}


//function to get the hours

void getTime12()
{
static int get12Hrs, get12Mins, get12Secs;
static string AMPM = "AM";

//ask for the hour using cin
cout << "Enter the hours: ";
cin >> get12Hrs;
cout << endl;

cout << "Enter minutes: ";
cin >> get12Mins;
cout << endl;

cout << "Enter seconds: ";
cin >> get12Secs;
cout << endl;

//ask user for AM PM input
cout << "Enter AM or PM: ";
cin >> AMPM;

//For PM
if (AMPM == "PM" || AMPM == "Pm" || AMPM == "pm")
{
    AMPM = "PM";
}

}


void getTime24()
{
static int get24Hrs, get24Mins, get24Secs;
static string AMPM = "AM";

//ask for the hour using cin
cout << "Enter the hours: ";
cin >> get24Hrs;
cout << endl;

cout << "Enter minutes: ";
cin >> get24Mins;
cout << endl;

cout << "Enter seconds: ";
cin >> get24Secs;
cout << endl;

//ask user for AM PM input
cout << "Enter AM or PM: ";
cin >> AMPM;

//For PM
if (AMPM == "PM" || AMPM == "Pm" || AMPM == "pm")
{
    AMPM = "PM";
}

}

//Function to change to 24-hour notation
void convert24to12()
{
    if (get24Hrs < 12)
    {
        get24Hrs = get24Hrs;
        get24Mins = get24Mins;
        get24Secs = get24Secs;
    }
    if (AMPM == "PM")
    {
        get24Hrs = get24Hrs - 12;
        get24Mins = get24Mins;
        get24Secs = get24Secs;
    }
}

//Function to change to 12-hour notation
void convert12to24()
{
    if (get12Hrs > 12)
    {
        get12Hrs = get12Hrs;
        get12Mins = get12Mins;
        get12Secs = get12Secs;
    }
    if (AMPM == "PM")
    {
        get12Hrs = get12Hrs + 12;
        get12Mins = get12Mins;
        get12Secs = get12Secs;
    }
}



void print12()
{
    cout << "The 12-hour time is " << get24Hrs << ":";

    cout.width(2);
    cout.fill('0');

    cout << get24Mins << " " << get24Secs;
    cout << AMPM << endl;
    cout << endl << endl;

 }


void print24()
{
    cout << "The 24-hour time is " << get12Hrs << ":";

    cout.width(2);
    cout.fill('0');

    cout << get12Mins << " " << get12Secs;
    cout << AMPM << endl;
    cout << endl << endl;

}
#包括
#包括
#包括
使用名称空间std;
//功能原型
void getTime12();
void getTime24();
void convert24to12();
无效转换为12到24();
无效打印24();
无效打印12();
无效菜单();
int main()
{
智力选择;
做
{
//显示菜单选项
菜单();
cin>>选择;
如果(选项='1')
{
getTime12();
转换为12到24();
打印24();
}
else if(选项='2')
{
getTime24();
转换为12();
打印12();
}
}while(选项!=“99”);
系统(“暂停”);
返回0;
}
无效菜单()
{
//声明要返回的变量

cout您的代码中有很多问题,因此我将尽全力:

main()
中,
choice
是一个
int
,但您正在检查它是
字符
1、2还是99。这将获取那些等于(我认为)49、50……等字符的
ASCI code
值。如果要检查整数值,只需删除括号

另外,您需要在
main()
中声明所有变量,然后在每个函数调用中传递所需的变量

另外,还要尝试找到一个更好的命名,这样事情就不会变得混乱

尝试执行
main()
中的所有
std::cout
语句,以及函数中的计算。只是为了更好地练习

这段代码是您的代码,但需要编译,您需要自己解决其余的问题,如果需要,我会提供帮助。这段代码通过值传递变量,而不是通过引用传递变量。。在Visual Studio中使用F-10和F-11逐步进入代码,确保逻辑正确。将鼠标悬停在每个语句上的所有变量上,以查看I流动就是你想要的。祝你好运

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;


// Function Prototypes
void getTime12(string, int,int, int);
void getTime24(string, int, int, int);
void convert12to24(string, int, int, int);
void convert24to12(string, int, int, int);
void print24(string, int, int, int);
void print12(string, int, int, int);
void menu();

int main()
{
    static string AMPM = "AM";
    int get12Hrs = 0, get12Mins = 0, get12Secs = 0;
    int get24Hrs = 0, get24Mins = 0, get24Secs = 0;

    int choice;

    do
    {
        // Display menu options
        menu();
        cin >> choice;

        if (choice == 1)
        {
            getTime12(AMPM, get12Hrs, get12Mins, get12Secs);
            convert12to24(AMPM, get12Hrs, get12Mins, get12Secs);
            print24(AMPM, get12Hrs, get12Mins, get12Secs);
        }

        else if (choice == 2)
        {
            getTime24(AMPM, get24Hrs, get24Mins, get24Secs);
            convert24to12(AMPM, get24Hrs, get24Mins, get24Secs);
            print12(AMPM, get24Hrs, get24Mins, get24Secs);
        }

    } while (choice != 99);

    system("pause");
    return 0;
}

void menu()
{
    //Declare variabls to return
    cout << endl;
    cout << "1. To convert time from 12-hour notation to 24-hour notation." << endl;
    cout << "2. To convert time from 24-hour notation to 12-hour notation." << endl;
    cout << "99. To quit the program." << endl << endl;
    cout << "Your choice (1, 2, 99): ";
    cout << endl << endl;
}

//function to get the hours
void getTime12(string AMPM, int get12Hrs, int get12Mins, int get12Secs)
{

    //ask for the hour using cin
    cout << "Enter the hours: ";
    cin >> get12Hrs;
    cout << endl;

    cout << "Enter minutes: ";
    cin >> get12Mins;
    cout << endl;

    cout << "Enter seconds: ";
    cin >> get12Secs;
    cout << endl;

    //ask user for AM PM input
    cout << "Enter AM or PM: ";
    cin >> AMPM;

    //For PM
    if (AMPM == "PM" || AMPM == "Pm" || AMPM == "pm")
    {
        AMPM = "PM";
    }

}

void getTime24(string AMPM, int get24Hrs, int get24Mins, int get24Secs)
{

    //ask for the hour using cin
    cout << "Enter the hours: ";
    cin >> get24Hrs;
    cout << endl;

    cout << "Enter minutes: ";
    cin >> get24Mins;
    cout << endl;

    cout << "Enter seconds: ";
    cin >> get24Secs;
    cout << endl;

    //ask user for AM PM input
    cout << "Enter AM or PM: ";
    cin >> AMPM;

    //For PM
    if (AMPM == "PM" || AMPM == "Pm" || AMPM == "pm")
    {
        AMPM = "PM";
    }

}

//Function to change to 24-hour notation
void convert24to12(string AMPM, int get24Hrs, int get24Mins, int get24Secs)
{
    if (get24Hrs < 12)
    {
        get24Hrs = get24Hrs;
        get24Mins = get24Mins;
        get24Secs = get24Secs;
    }
    if (AMPM == "PM")
    {
        get24Hrs = get24Hrs - 12;
        get24Mins = get24Mins;
        get24Secs = get24Secs;
    }
}

//Function to change to 12-hour notation
void convert12to24(string AMPM, int get12Hrs, int get12Mins, int get12Secs)
{
    if (get12Hrs > 12)
    {
        get12Hrs = get12Hrs;
        get12Mins = get12Mins;
        get12Secs = get12Secs;
    }
    if (AMPM == "PM")
    {
        get12Hrs = get12Hrs + 12;
        get12Mins = get12Mins;
        get12Secs = get12Secs;
    }
}

void print12(string AMPM, int get24Hrs, int get24Mins, int get24Secs)
{
    cout << "The 12-hour time is " << get24Hrs << ":";

    cout.width(2);
    cout.fill('0');

    cout << get24Mins << " " << get24Secs;
    cout << AMPM << endl;
    cout << endl << endl;

}

void print24(string AMPM, int get12Hrs, int get12Mins, int get12Secs)
{
    cout << "The 24-hour time is " << get12Hrs << ":";

    cout.width(2);
    cout.fill('0');

    cout << get12Mins << " " << get12Secs;
    cout << AMPM << endl;
    cout << endl << endl;

}
#包括
#包括
#包括
使用名称空间std;
//功能原型
void getTime12(字符串,int,int,int);
void getTime24(字符串,int,int,int);
void convert12to24(字符串,int,int,int);
void convert24to12(字符串,int,int,int);
void print24(字符串,int,int,int);
void print12(字符串,int,int,int);
无效菜单();
int main()
{
静态字符串AMPM=“AM”;
int get12小时=0,get12分钟=0,get12秒=0;
int get24小时=0,get24分钟=0,get24秒=0;
智力选择;
做
{
//显示菜单选项
菜单();
cin>>选择;
如果(选项==1)
{
getTime12(AMPM,get12小时,get12分钟,get12秒);
转换为12到24(安培、12小时、12分钟、12秒);
打印24(每分钟、每12小时、每12分钟、每12秒);
}
else if(选项==2)
{
getTime24(AMPM、get24小时、get24分钟、get24秒);
转换为24到12(AMPM、24小时、24分钟、24秒);
打印12(AMPM、24小时、24分钟、24秒);
}
}而(选择!=99);
系统(“暂停”);
返回0;
}
无效菜单()
{
//声明要返回的变量

也许你想将
get24Hrs
声明为全局变量。我建议你编写一个小得多的程序来检查全局变量和静态变量是如何工作的。我的导师不允许使用全局变量。因此,我正在寻找一个替代方法,然后你需要在main()中声明所有变量并在每次函数调用时将其作为参数传递。您知道怎么做吗?void convert24to12(int&)..或void convert24to12(int get24Hrs,string AMPM…etc)这两种方法都有效。如果他打算通过推荐:那么,第一种方法。否则,第二种方法。我会帮助你谢谢你花时间帮忙,我知道我在犯错误,但我会继续。我正在努力,对吗now@Chris只要你继续:)这段代码不起作用。例如,也许你正在考虑通过引用传递示例
void getTime12(string&M,int&get12Hrs,int&get12min,int&get12Secs)
我们同意编译它,他会做其余的工作。@barmakshemirani和他在上面提到他将尝试通过引用我看不懂细节。我看到的get函数是按值传递的。如果这是某种形式的辅导技巧,我就不做了。