改变子功能c ++

时间:2013-12-10 23:08:07

标签: c++ function

我对c ++很陌生, 在该程序结束时,我希望它能够跳回一个早期的子功能,允许用户在以后的子功能中提供新输出之前输入新值。 此刻它将成功返回到所需的子功能。然而,该程序退出后,而不是继续下一个子功能。

这里是int main;

int main ()
{
welcome ();
input_dimensions ();
input_loading ();
transition ();
twodarray ();
initial_calculations ();
bending_stress ();
shearing_stress ();
bearing_stress ();
conclusion ();
cin.get();
cin.get();
return 0;
}

并且继承了最后一个子功能

// C O N C L U S I O N
void conclusion ()
{
// End Results
cout << "\n\n\n____________ C O N C L U S I O N_____________ \n\n" << endl;

if ((bendingstress < designbending) && (shearstress < designshear) && ((  bearingstress <= (designbearing * kc90))))
{
    cout << "Beam is adequate for the input loads \n\n\n" << endl;
    cout << "\n\n\nPlease scroll up for more information \n\n\n" <<endl;
    cout << "\nPlease press ENTER to design a new beam or close this screen to quit" <<endl;
    system ("pause > nul");     
    system ("CLS");
    return input_dimensions (); 
}

else
{
    cout << "\n\n          W A R N I N G\n THIS BEAM IS AT RISK OF FAILURE ! \n" << endl;
    cout << "\nPlease scroll up for more information before trying new values \n" <<endl;
    cout << "\nOnce ready, please press ENTER to try new beam member properties \n" <<endl;
    system ("pause > nul");     
    system ("CLS");
    return input_dimensions (); 
}
}

感谢您的时间

1 个答案:

答案 0 :(得分:0)

  

然而,该程序退出后,而不是继续下一个   子功能。

  • 您在input_dimensions()内拨打conclusion()
  • input_dimensions()返回时,执行将继续执行conclusion()内的下一个语句。
  • conclusion()返回时, 执行将在调用main()后的conclusion()语句中继续执行。
  • 在那里,你需要在main()内再增加三个语句,然后退出你的程序。

除非你告诉它,否则C ++不会多次运行语句,所以main()中的函数调用只运行一次(你没有任何循环)。

以下是您问题的可能解决方案:

而不是在main()中进行所有函数调用,您可以使函数相互调用,如下所示:

void welcome();
void input_dimenstions();
void conclusion ();
void initial_calculations ();

void welcome () {
     input_dimensions();
}
void input_dimensions () {
     initial_calculations();
}
void initial_calculations () {
     conclusion();
}
void conclusion () {
    if (...) {
              input_dimensions();
    }
    else {
        ...
    }
}

int main(int argc, char **argv) {
    welcome();
}

通过这种方式,您可以控制程序在下一个函数中的位置,而不是让main()这样做,这通常很快就会变得混乱。

通过在顶部声明所有功能,您不必在以后担心它们的顺序。