编程新小白 2024-04-26 20:16 采纳率: 33.3%
浏览 1

C++中变量出现的一些问题

我想问问,如何使得在在条件语句中获取的值给予到主程序中
运用函数返回值的功能


int checkText(TextMath* abs, int year, int moon, int day)
{
    for (int i = 0; i < abs->m_Math; i++)
    {
        if (abs->InFormation[i].Year == year && abs->InFormation[i].Moon == moon && abs->InFormation[i].Day == day)
        {
            return i;
        }
    }
    return -1;
}

void modeText(TextMath* abs)
{
    int year = 0;
    int moon = 0;
    int day = 0;
    cout << "请填写年份" << endl;
    while (true)
    {
        cin >> year;
        if (year > 0)
        {
            cout << "请添写月份: " << endl;
            while (true)
            {
                cin >> moon;
                if (moon >= 1 && moon <= 12)
                {
                    cout << "请添写日数: " << endl;
                    while (moon == 1 || moon == 3 || moon == 5 || moon == 7 || moon == 8 || moon == 10 || moon == 12)
                    {
                        cin >> day;
                        if (day >= 1 && day <= 31)
                        {
                            int ret = checkText(abs, year, moon, day);
                            break;
                        }
                        cout << "请填写正确的日数" << endl;
                    }

                    while (moon == 4 || moon == 6 || moon == 9 || moon == 11)
                    {
                        cin >> day;
                        if (day >= 1 && day <= 30)
                        {
                            int ret = checkText(abs, year, moon, day);
                            break;
                        }
                        cout << "请填写正确的日数" << endl;
                    }

                    while (moon == 2)
                    {
                        cin >> day;
                        if (year % 4 == 0)
                        {
                            if (day >= 1 && day <= 29)
                            {
                                int ret = checkText(abs, year, moon, day);
                                break;
                            }
                        }
                        else
                        {
                            if (day >= 1 && day <= 28)
                            {
                                int ret = checkText(abs, year, moon, day);
                                break;
                            }
                        }
                        cout << "请填写正确的日数" << endl;
                    }
                }
                cout << "请填写有效月数" << endl;
            }
        }
        cout << "请输入有效年份" << endl;
    }
    if (ret == -1)        // 这里会出错,未识别标符
    {
        cout << "无可修改记录" << endl;
    }
    else
    {
        cout << "正在修改" << endl;
        cout << "请添写修改年份: " << endl;
        int year = 0;
        while (true)
        {
            cin >> year;
            if (year > 0)
            {
                abs->InFormation[abs->m_Math].Year = year;
                break;
            }
            cout << "请输入有效年份" << endl;
        }
        cout << "请添写修改月份: " << endl;
        int moon = 0;
        while (true)
        {
            cin >> moon;
            if (moon >= 1 && moon <= 12)
            {
                abs->InFormation[abs->m_Math].Moon = moon;
                break;
            }
            cout << "请填写有效月数" << endl;
        }

        cout << "请添写修改日数: " << endl;
        int day = 0;
        while (moon == 1 || moon == 3 || moon == 5 || moon == 7 || moon == 8 || moon == 10 || moon == 12)
        {
            cin >> day;
            if (day >= 1 && day <= 31)
            {
                abs->InFormation[abs->m_Math].Day = day;
                break;
            }
            cout << "请填写正确的日数" << endl;
        }

        while (moon == 4 || moon == 6 || moon == 9 || moon == 11)
        {
            cin >> day;
            if (day >= 1 && day <= 30)
            {
                abs->InFormation[abs->m_Math].Day = day;
                break;
            }
            cout << "请填写正确的日数" << endl;
        }

        while (moon == 2)
        {
            cin >> day;
            if (year % 4 == 0)
            {
                if (day >= 1 && day <= 29)
                {
                    abs->InFormation[abs->m_Math].Day = day;
                    break;
                }
            }
            else
            {
                if (day >= 1 && day <= 28)
                {
                    abs->InFormation[abs->m_Math].Day = day;
                    break;
                }
            }
            cout << "请填写正确的日数" << endl;
        }

        cout << "修改后的该天天气如何" << endl;
        string weather;
        cin >> weather;
        abs->InFormation[abs->m_Math].Weather = weather;

        cout << "布置完成,请输入修改正文 " << endl;
        cout << endl;
        string text;
        cin >> text;
        abs->InFormation[abs->m_Math].Text = text;
        cout << endl;
        cout << "记录完成" << endl;

        abs->m_Math++;

    }
    system("pause");
    system("cls");
}

请问怎么解决啊

  • 写回答

3条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-04-26 20:16
    关注

    引自免费微信小程序:皆我百晓生

    在你的代码中,checkText 函数的返回值没有正确地被处理。问题在于 modeText 函数内部定义了一个与外部同名的局部变量 int ret,这导致了在检查 checkText 返回值的地方出现了未识别标识符的问题。为了解决这个问题,你需要移除模式文本函数内部对 int ret 的重新定义,并确保在循环之外使用从 checkText 函数得到的结果。

    这里是修复后的代码:

    #include<iostream>
    using namespace std;
    
    // 假设 TextMath 和 InFormation 结构体已经定义好
    // struct TextMath;
    // struct InFormation;
    
    int checkText(TextMath* abs, int year, int moon, int day)
    {
        for (int i = 0; i < abs->m_Math; i++)
        {
            if (abs->InFormation[i].Year == year && abs->InFormation[i].Moon == moon && abs->InFormation[i].Day == day)
            {
                return i;
            }
        }
        return -1;
    }
    
    void modeText(TextMath* abs)
    {
        int year = 0;
        int moon = 0;
        int day = 0;
        cout << "请填写年份" << endl;
        while (true)
        {
            cin >> year;
            if (year > 0)
            {
                cout << "请添写月份: " << endl;
                while (true)
                {
                    cin >> moon;
                    if (moon >= 1 && moon <= 12)
                    {
                        cout << "请添写日数: " << endl;
                        while (moon == 1 || moon == 3 || moon == 5 || moon == 7 || moon == 8 || moon == 10 || moon == 12)
                        {
                            cin >> day;
                            if (day >= 1 && day <= 31)
                            {
                                int ret = checkText(abs, year, moon, day);
                                break;
                            }
                            cout << "请填写正确的日数" << endl;
                        }
    
                        while (moon == 4 || moon == 6 || moon == 9 || moon == 11)
                        {
                            cin >> day;
                            if (day >= 1 && day <= 30)
                            {
                                ret = checkText(abs, year, moon, day);
                                break;
                            }
                            cout << "请填写正确的日数" << endl;
                        }
    
                        while (moon == 2)
                        {
                            cin >> day;
                            if (year % 4 == 0)
                            {
                                if (day >= 1 && day <= 29)
                                {
                                    ret = checkText(abs, year, moon, day);
                                    break;
                                }
                            }
                            else
                            {
                                if (day >= 1 && day <= 28)
                                {
                                    ret = checkText(abs, year, moon, day);
                                    break;
                                }
                            }
                            cout << "请填写正确的日数" << endl;
                        }
                        break; // 移动这个break语句,以便退出所有嵌套循环
                    }
                    cout << "请填写有效月数" << endl;
                }
            }
            cout << "请输入有效年份" << endl;
        }
        if (ret == -1)
        {
            cout << "无可修改记录" << endl;
        }
        else
        {
            cout << "正在修改" << endl;
            cout << "请添写修改年份: " << endl;
            int year = 0;
            while (true)
            {
                cin >> year;
                if (year > 0)
                {
                    abs->InFormation[ret].Year = year;
                    break;
                }
                cout << "请输入有效年份" << endl;
            }
            cout << "请添写修改月份: " << endl;
            int moon = 0;
            while (true)
            {
                cin >> moon;
                if (moon >= 1 && moon <= 12)
                {
                    abs->InFormation[ret].Moon = moon;
                    break;
                }
                cout << "请填写有效月数" << endl;
            }
    
            cout << "请添写修改日数: " << endl;
            int day = 0;
            while (moon == 1 || moon == 3 || moon == 5 || moon == 7 || moon == 8 || moon == 10 || moon == 12)
            {
                cin >> day;
                if (day >= 1 && day <= 31)
                {
                    abs->InFormation[ret].Day = day;
                    break;
                }
                cout << "请填写正确的日数" << endl;
            }
    
            while (moon == 4 || moon == 6 || moon == 9 || moon == 11)
            {
                cin >> day;
                if (day >= 1 && day <= 30)
                {
                    abs->InFormation[ret].Day = day;
                    break;
                }
                cout << "请填写正确的日数" << endl;
            }
    
            while (moon == 2)
            {
                cin >> day;
                if (year % 4 == 0)
                {
                    if (day >= 1 && day <= 29)
                    {
                        abs->InFormation[ret].Day = day;
                        break;
                    }
                }
                else
                {
                    if (day >= 1 && day <= 28)
                    {
                        abs->InFormation[ret].Day = day;
                        break;
                    }
                }
                cout << "请填写正确的日数" << endl;
            }
    
            cout << "修改后的该天天气如何" << endl;
            string weather;
            cin >> weather;
            abs->InFormation[ret].Weather = weather;
    
            cout << "布置完成,请输入修改正文 " << endl;
            cout << endl;
            string text;
            cin >> text;
            abs->InFormation[ret].Text = text;
            cout << endl;
            cout << "记录完成" << endl;
    
            abs->m_Math++;
        }
        system("pause");
        system("cls");
    }
    

    在这个修复版本中,我删除了模式文本函数内部对 int ret 的重新定义,并在找到匹配项后立即跳出所有嵌套循环。这样,在判断 ret 是否等于 -1 时,它就会包含来自 checkText 函数的正确返回值。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 4月26日

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题