我试图让用户输入放入向量中的数字,然后使用函数调用来输入数字,为什么这不起作用?我只能说出第一个号码.
template <typename T>
void write_vector(const vector<T>& V)
{
cout << "The numbers in the vector are: " << endl;
for(int i=0; i < V.size(); i++)
cout << V[i] << " ";
}
int main()
{
int input;
vector<int> V;
cout << "Enter your numbers to be evaluated: " << endl;
cin >> input;
V.push_back(input);
write_vector(V);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我无法弄清楚为什么我一直得到结果1.#INF从my_exp()我给它1作为输入时.这是代码:
double factorial(const int k)
{
int prod = 1;
for(int i=1; i<=k; i++)
prod = i * prod;
return prod;
}
double power(const double base, const int exponent)
{
double result = 1;
for(int i=1; i<=exponent; i++)
result = result * base;
return result;
}
double my_exp(double x)
{
double sum = 1 + x;
for(int k=2; k<50; k++)
sum = sum + power(x,k) / factorial(k);
return sum;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试从文件中读取数字并将它们放入数组中.现在,当我运行该程序时,它打印8个数字,然后该行结束并打印相同的8个数字.这是一个永无止境的循环.我究竟做错了什么?
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int num;
ifstream infile;
infile.open("euler8Nums.txt");
infile >> num;//must attempt to read info prior to an eof() test
while(!infile.eof()){
cout << num << endl;
infile >> num;
}
infile.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Linux Ubuntu 10.10中的g ++编译.cpp文件,当我尝试编译此代码时
#include <iostream>
#include <vector>
#include <"writeVector.h"
#include <"insertionSort.h">
using namespace std;
int main()
{
int n;
int i;
vector<int> V;
cout << "Enter the amount of numbers you want to evaluate: ";
cin >> n;
cout << "Enter your numbers to be evaluated: " << endl;
while (V.size() < n && cin >> i){
V.push_back(i);
}
InsertionSort(V);
write_vector(V);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在同一个文件夹中有两个.h文件,但它一直说我的writeVector.h文件或文件夹不存在.
这就是我的writeVector.h文件的样子
#include <iostream>
#include <vector>
using namespace std;
template <typename …Run Code Online (Sandbox Code Playgroud) c++ ×4
function ×2
exp ×1
g++ ×1
linux ×1
loops ×1
templates ×1
ubuntu-10.10 ×1
vector ×1
while-loop ×1