小编Sea*_*ean的帖子

如何cin到矢量

我试图让用户输入放入向量中的数字,然后使用函数调用来输入数字,为什么这不起作用?我只能说出第一个号码.

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)

c++ templates function vector

35
推荐指数
8
解决办法
14万
查看次数

exp函数使用c ++

我无法弄清楚为什么我一直得到结果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)

c++ function exp

4
推荐指数
2
解决办法
2641
查看次数

.eof()循环不起作用

我正在尝试从文件中读取数字并将它们放入数组中.现在,当我运行该程序时,它打印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)

c++ loops while-loop

2
推荐指数
2
解决办法
2万
查看次数

g ++编译错误.h文件

我正在尝试使用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++ linux g++ ubuntu-10.10

0
推荐指数
1
解决办法
1084
查看次数

标签 统计

c++ ×4

function ×2

exp ×1

g++ ×1

linux ×1

loops ×1

templates ×1

ubuntu-10.10 ×1

vector ×1

while-loop ×1