当选择一个选项时,如何执行另一个C++程序?

当选择一个选项时,如何执行另一个C++程序?,c++,C++,我有这个代码执行时,它将让用户选择一个选项。一旦用户输入一个选项,程序将被清除并执行另一个程序。下面是示例。底部是选项1的另一个程序 #include <iostream> using namespace std; int main() { int a; cout<<"Please choose an option below: \n"; cout<<"1. Area of Shapes\n"; cout<<"2.

我有这个代码执行时,它将让用户选择一个选项。一旦用户输入一个选项,程序将被清除并执行另一个程序。下面是示例。底部是选项1的另一个程序

#include <iostream>
using namespace std;
int main()
{
    int a;
    cout<<"Please choose an option below: \n";
    cout<<"1. Area of Shapes\n";
    cout<<"2. Cost of your items\n";
    cout<<"3. Flood Control\n";
    cout<<"4. Fibonacci Numbers\n";
    cout<<"5. Addition Table\n";
    cout<<"6. Exit\n";
    cin>> a;

system("pause");
return 0;    
}
以下是选项1的程序:

#include <iostream>
using namespace std;
float circle (float a)
{
      float z;
      z = 3.141593*(a*a);
      return (z);
}
float square (float b)
{
      float y;
      y = b * b;
      return (y);
}
float rectangle (float c, float d)
{
      float x;
      x = c * d;
      return (x);
}
float triangle (float e, float f)
{
      float w;
      w = (e * f) / 2;
      return (w);
}
void exit ()
{
     cout << "THANK YOU! GOODBYE!" <<endl;
}            
int main()
{
      float n;
      float l;
      float m;
      float radius;
      float side;
      float length;
      float width;
      float base;
      float height;

      do
      {
            cout << "1 => Area of Circle" <<endl;
            cout << "2 => Area of Square" <<endl;
            cout << "3 => Area of Rectangle" <<endl;
            cout << "4 => Area of Triangle" <<endl;
            cout << "0 => Exit" <<endl;
            cout << "Please enter number of your choice: ";
            cin >> n;
            {
                if (n==0)
                {
                         exit ();
                         system("pause");
                         return 0;
                }
                else if (n==1)
                {
                     cout << "Enter radius of the circle: ";
                     cin >> radius;
                     l = circle (radius);
                     cout << "Area of the circle is: " <<l <<endl;
                }
                else if (n==2)
                {
                     cout << "Enter side of the square: ";
                     cin >> side;
                     cout << "Area of the square is: " <<square (side) <<endl;
                }
                else if (n==3)
                {
                     cout << "Enter length of the rectangle: ";
                     cin >> length;
                     cout << "Enter width of the rectangle: ";
                     cin >> width;
                     m = rectangle (length, width);
                     cout << "Area of the rectangle is: " <<m <<endl;
                }
                else if (n==4)
                {
                     cout << "Enter base of the triangle: ";
                     cin >> base;
                     cout << "Enter height of the triangle: ";
                     cin >> height;
                     cout << "Area of the triangle is: " <<triangle (base, height) <<endl;
                }
                else
                cout << "Invalid number. Please enter a valid number below" <<endl;
                }
            }
            while (n!=0);
            cout <<endl <<endl;
            system("pause");
            return 0;
}
如果您确实想用另一个程序替换当前程序,请查看exec系列的系统调用

是否将每个程序放在自己的文件中,但最好将它们分开。 将每个程序的main重命名为有意义的内容,如区域。 在标题中声明该函数。 在控制器程序中包括该标题。 根据读取的输入从控制器调用相应的程序函数。
你的问题到底是什么?你告诉我们的只是你所拥有的,而不是你想要完成的。我只是一名编程专业的学生。我需要做的就是当用户想要输入选项1时,它将直接进入该选项的程序。我不知道是否所有的程序都放在一个文件中。我不知道如何使用它。我只是刚开始编程,只知道它的基本知识。请帮忙,然后我会说,你可能不想用另一个程序取代当前的程序,因为这很少是一个初学者的话题。如果你真的这么做了,那么听起来你需要的帮助比这样的网站所能提供的要多。这并不完全是取代当前的程序。只是如果用户输入选项1,它将运行与选项2相同的程序,因此我很抱歉,但我不知道如何在主文件中启动我的程序