请各位专家有空看一下小弟我的小程序,小弟我的学习书不能解决这个有关问题
请各位专家有空看一下我的小程序,我的学习书不能解决这个问题。
我在学习essential c++这本书的中文版,这个程序是课后的作业,编译环境是vc++6,编译后出现错误。程序在下面,请各位看一看,如果可以的话,请指出错误。谢谢。
程序的功能:按用户输入的个数算出pentagonal的各个元素。举例输入为5,就计算出前5个元素,8个,就算出前8个,
vector <float> addelem(int,vector <float> &);
函数用来计算元素。
void display(vector <float> );
用来显示计算出的元素。
变量pos用来存储用户输入的元素数。
向量pent用来存储计算出的pentagonal数列元素。
程序很简单,我刚刚学习c++,essential c++是我的第一本书。
vector <float> addelem(int,vector <float> &);
void display(vector <float> );
#include <vector>
#include <iostream>
using namespace std;
void main()
{
int pos;
vector <float> pent;
cout < < "Please enter the element number of the pent: "
< < " " < <endl;
cin> > pos;
addelem(pos,pent);
display(pent);
}
vector <float> addelem(int pos,vector <float> & pent)
{
if(pos <=0||pos> 256)
{
cout < < "The number # " < <pos < < "couldn 't be supported " < <endl;
return pent;
}
if(pos> pent.size())
for(ix=pent.size();ix <pos;ix++)
pent[ix]=(ix+1)*(3*(ix+1)-1)/2;
return pent;
}
void display(vector <float> pent)
{
for(ix=0;ix <=(pent.size()-1);ix++)
{
cout < < "The pent is " < <pent[ix] < < " " < <endl;
}
}
------解决方案--------------------
vector <float> addelem(int pos,vector <float> & pent);
void display(vector <float> pent);
这两句话放到main函数前,这个叫声明,不然你函数实现放到main后面,main中认为那两函数没有定义
还有你ix没有定义
------解决方案--------------------
换C++ Primer吧,essential c++是给java等转C++时了解概念用的。
------解决方案--------------------
#include <vector>
#include <iostream>
using namespace std;
vector <float> addelem(int,vector <float> &);
void display(vector <float> );
void main()
{
int pos;
vector <float> pent;
cout < < "Please enter the element number of the pent: "
< < " " < <endl;
cin> > pos;
addelem(pos,pent);
display(pent);
}
vector <float> addelem(int pos,vector <float> & pent)
{
if(pos <=0||pos> 256)
{
cout < < "The number # " < <pos < < "couldn 't be supported " < <endl;
return pent;
}
if(pos> pent.size())
for(int ix=pent.size();ix <pos;ix++)
pent[ix]=(ix+1)*(3*(ix+1)-1)/2;
return pent;
}
void display(vector <float> pent)
{
for(int ix=0;ix <=(pent.size()-1);ix++)
{
cout < < "The pent is " < <pent[ix] < < " " < <endl;
}
}
我在学习essential c++这本书的中文版,这个程序是课后的作业,编译环境是vc++6,编译后出现错误。程序在下面,请各位看一看,如果可以的话,请指出错误。谢谢。
程序的功能:按用户输入的个数算出pentagonal的各个元素。举例输入为5,就计算出前5个元素,8个,就算出前8个,
vector <float> addelem(int,vector <float> &);
函数用来计算元素。
void display(vector <float> );
用来显示计算出的元素。
变量pos用来存储用户输入的元素数。
向量pent用来存储计算出的pentagonal数列元素。
程序很简单,我刚刚学习c++,essential c++是我的第一本书。
vector <float> addelem(int,vector <float> &);
void display(vector <float> );
#include <vector>
#include <iostream>
using namespace std;
void main()
{
int pos;
vector <float> pent;
cout < < "Please enter the element number of the pent: "
< < " " < <endl;
cin> > pos;
addelem(pos,pent);
display(pent);
}
vector <float> addelem(int pos,vector <float> & pent)
{
if(pos <=0||pos> 256)
{
cout < < "The number # " < <pos < < "couldn 't be supported " < <endl;
return pent;
}
if(pos> pent.size())
for(ix=pent.size();ix <pos;ix++)
pent[ix]=(ix+1)*(3*(ix+1)-1)/2;
return pent;
}
void display(vector <float> pent)
{
for(ix=0;ix <=(pent.size()-1);ix++)
{
cout < < "The pent is " < <pent[ix] < < " " < <endl;
}
}
------解决方案--------------------
vector <float> addelem(int pos,vector <float> & pent);
void display(vector <float> pent);
这两句话放到main函数前,这个叫声明,不然你函数实现放到main后面,main中认为那两函数没有定义
还有你ix没有定义
------解决方案--------------------
换C++ Primer吧,essential c++是给java等转C++时了解概念用的。
------解决方案--------------------
#include <vector>
#include <iostream>
using namespace std;
vector <float> addelem(int,vector <float> &);
void display(vector <float> );
void main()
{
int pos;
vector <float> pent;
cout < < "Please enter the element number of the pent: "
< < " " < <endl;
cin> > pos;
addelem(pos,pent);
display(pent);
}
vector <float> addelem(int pos,vector <float> & pent)
{
if(pos <=0||pos> 256)
{
cout < < "The number # " < <pos < < "couldn 't be supported " < <endl;
return pent;
}
if(pos> pent.size())
for(int ix=pent.size();ix <pos;ix++)
pent[ix]=(ix+1)*(3*(ix+1)-1)/2;
return pent;
}
void display(vector <float> pent)
{
for(int ix=0;ix <=(pent.size()-1);ix++)
{
cout < < "The pent is " < <pent[ix] < < " " < <endl;
}
}