我将如何选择进行多个数学问题,而无需每次复制和粘贴代码
How would I give the choice to do multiple math problems without having to copy and paste the code each time?
我的代码允许用户输入一些数字或从文档中读取它们。我还将如何允许用户从选择数学问题的选择中挑选这些数字,而无需在我的主体中有一堆代码。到目前为止,这是我的代码:
#include <iostream>
#include <cmath>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
int main()
{
//ask for file input or manual input
cout << "Press 1 to enter numbers or 2 to read them from a list." << "n";
int choice;
cin >> choice;
if (choice == 1)
{
int numberswanted;
cout << "How many numbers would you like to enter?" << "n";
cin >> numberswanted;
vector<double> list;
for (int i = 0; i < numberswanted; i++)
{
cout << "Enter your numbers: " << "n";
double x;
cin >> x;
list.push_back(x);
}
}
else if (choice == 2)
{
ifstream doc;
float output;
doc.open("input.txt");
while (!doc.eof())
{
vector<double> list;
double x;
doc >> x;
list.push_back(x);
}
doc.close();
}
return 0;
}
可能是您可以尝试这样的事情:
int main() { cout << "Press 1 to enter numbers or 2 to read them from a list." << "n";
int choice;
cin >> choice;
if (choice == 1)
{
choice1();
}
else if (choice == 2)
{
choice2();
}
return 0;}
当choice1
包含int [...] list.push_back(x);}
时等...
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在java中解决这段代码时面临循环中的问题
- 将鼠标悬停在问题上时与预期">"相关的代码错误
- 代码在我的计算机上运行良好,但是在将其提交给coursera时遇到未知的信号11问题
- 读取最后一行代码算法 - c++ 时出现问题
- 这个带有模板<类 Vector 的C++代码片段有什么问题>
- Xcode 语义问题引用或以前定义的代码
- 需要以下代码的帮助,下面的代码有什么问题
- 我无法在Visual Studio代码中使用CIN输入答案,它说输入您的年龄,但它说只读文本编辑器如何解决这个问题?
- 如何在代码问题中解决此问题 代码强制
- C 代码问题?编译器问题?代码块
- C - 如果语句问题.代码将不会运行其他,否则
- 简单的NOOB C 输入/输出问题:代码错误
- NDK:0xdeadd00d时虚拟机中止和致命信号 11 (SIGSEGV) 的问题(代码 = 1)
- 编译问题 - 代码 C2129
- c++问题代码段
- 模板作用域问题代码无法编译
- 基本 CUDA 程序的问题.代码或编译器