当选择一个选项时,如何执行另一个c++程序
How to execute a another c++ program when an option is chosen?
我有这段代码,当执行时,它会让用户选择一个选项。一旦用户输入一个选项,程序将被清除并执行另一个程序。下面是示例。底部是选项1 的另一个程序
#include <iostream>
using namespace std;
int main()
{
int a;
cout<<"Please choose an option below: n";
cout<<"1. Area of Shapesn";
cout<<"2. Cost of your itemsn";
cout<<"3. Flood Controln";
cout<<"4. Fibonacci Numbersn";
cout<<"5. Addition Tablen";
cout<<"6. Exitn";
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
重命名为有意义的内容,如"区域" - 在标头中声明该函数
- 在您的"控制器"程序中包含该标头
- 根据您读取的输入,从"控制器"调用相应的"程序函数"
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- C++,系统无法执行指定的程序
- 使用C++中的模板和运算符重载执行矩阵运算
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 执行函数时导致崩溃的变量
- 无论条件是否为true,if总是在c++中执行
- 当函数模板参数是具有默认参数的类模板时,函数模板参数的推导如何执行
- 在C++中对T*类型执行std::move的意外行为
- 使用QProcess执行命令,并将结果存储在QStringList中
- 如何在没有信号的情况下从C++执行QML插槽
- 如何确认我的constexpr表达式实际上已经在编译时执行
- 如何在C++中执行以前执行的代码行
- 为什么我的虚拟破坏者多次执行这些执行
- 如何标识其功能当前正由可执行文件执行的共享库
- 打印C 代码执行逐步执行
- 从编译的可执行文件执行函数
- 当程序执行取决于执行顺序时,是否是未定义的行为
- 在.so内部定义的extern变量和可执行引线执行未定义的行为
- 等待执行不可执行脚本的子进程
- 调试与发布可执行文件有何不同?