简体   繁体   English

存储值Turbo C ++

[英]storing value Turbo C++

I am currently trying to get my program to store a value so that it can display that value whenever there is an error. 我目前正在尝试让我的程序存储一个值,以便在出现错误时它可以显示该值。 Here is my program : 这是我的计划:

void function()
{
    float abc[2];
    int i = 0;
    if ( i/2 != 0 ) 
    { 
      i++; 
      abc[0] = 1; 
    }
    abc[1] = abc[0];
    cout << abc[1];
}

Practically, my program keeps calling this function until I exit it, so it will keep updating my program and show me abc[1] . 实际上,我的程序一直调用这个函数,直到我退出它,所以它会不断更新我的程序并向我显示abc[1] What I want is when the condition of the if statement is not met, abc[1] will display the previously known value of itself. 我想要的是当if语句的条件不满足时, abc[1]将显示以前已知的自身值。 How do I do it?. 我该怎么做?。

I'm not sure exactly what result you are trying for, but you need to make abc and i static, thusly: 我不确定你想要的结果是什么,但你需要让abc和我静态,因此:

void function()
{
    static float abc[2];
    static int i = 0;
    if ( i/2 != 0 ) { i++; abc[0] = 1; }
    abc[1] = abc[0];
    cout<<abc[1];
}

This will allow them to retain their value between function calls. 这将允许他们在函数调用之间保留它们的值。 Right now that function is pretty silly, but I don't know what to recommend because I don't know what you are trying to do. 现在这个功能非常愚蠢,但我不知道该推荐什么,因为我不知道你想要做什么。

Not sure i fully understand what you are meaning but if you want to store the previous value for printing why not just have an abcPrevious variable? 不确定我完全理解你的意思,但如果你想存储以前的打印值,为什么不只是有一个abcPrevious变量? After each loop (at the end) update it with the new value. 在每个循环(最后)之后用新值更新它。

void function()
{
    static float abcPrevious;
    static float abc[2];
    static int i = 0;
    if ( i/2 != 0 ) 
    { 
      i++; 
      abc[0] = 1; 
    }
    abcPrevious = abc[1];
    abc[1] = abc[0];
    cout << "abc[1] is: " << abc[1] << "\nabcPrevious is: " << abcPrevious << endl;
}

Hopefully this helps :D 希望这会有所帮助:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM