这是我的整个代码(在C ++中),以备必要时使用。我正在使用Eclipse Kepler。
//Tutorials from www.cplusplus.com/doc/tutorial combined
#include <iostream>
using namespace std;
//tutorial #1.1 Function with type -- Addition
int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}
//tutorial #1.2 Function with type -- Subtraction
int subtraction (int a, int b){
int r;
r=a-b;
return (r);
}
//tutorial #1.3 Function with NO type
void printmessage (void)
{
cout << "It's good to be the king." << '\n';
}
//tutorial #2.1 Arguments passed by reference
void duplicate (int& a, int& b, int& c)
{
a*=2;
b*=2;
c*=2;
}
//tutorial #2.2 and 2.5 Arguments passed by value and Declaration of preferred inline function
inline void prevnext (int x, int& prev, int& next)
{
prev = x-1;
next = x+1;
}
//tutorial #2.3 Default values in parameters (functions)
int divide (int a, int b=2)
{
int r;
r=a/b;
return (r);
}
//tutorial #2.4.1 Overloaded functions -- int cannot handle decimals
int operate (int a, int b)
{
return (a*b);
}
//tutorial #2.4.2 Overloaded functions -- float is used with decimals
float operate (float a, float b)
{
return (a/b);
}
//tutorial #2.6 Recursivity -- factorial calculator
long factorial (long a)
{
if (a > 1)
return (a * factorial (a-1));
else
return (1);
}
//tutorial #2.7 Declaring function prototypes
void odd (int a); //declared
void even (int a); //declared
void odd (int a) //defined
{
if ((a%2)!=0) cout << "Number is odd.\n";
else even (a);
}
void even (int a) //defined
{
if ((a%2)==0) cout << "Number is even.\n";
else odd (a);
}
//tutorial #3.1 Initializing and accessing the values of an array
float billy [] = {16, 2, 77, 40, 12071};
float k, result=0;
int main ()
{
cout << "Name: My Name \n";
cout << "Homework: 1";
cout << endl;
cout << endl;
int x=8, y=1, z, aa;
float n=3.0,m=9.0;
long number;
z = addition (4,2);
aa = subtraction (6,3);
cout << "The sum is " << z << '\n';
cout << "The subtraction is " << aa << '\n';
cout << "The second subtraction is " << subtraction(x,y) << '\n';
z = addition (x,y) - 3;
cout << "The new sum is " << z << '\n';
printmessage();
duplicate (x,y,z);
cout << "x=" << x << ", y=" << y << ", z=" << z << '\n';
prevnext (x, y, z);
cout << "Previous=" << y << ", Next=" << z;
cout << endl;
cout << "Division = " << divide (14);
cout << endl;
cout << "Second Division = " << divide (10,8);
cout << endl;
cout << "Operate Multiply: " << operate(x,y);
cout << '\n';
cout << "Operate Divide: " << operate(n,m);
cout << endl;
cout << "Please type a number: ";
cin >> number;
cout << number << "! = " << factorial (number);
cout << endl;
int i;
do {
cout << "Type a number (type 0 to exit): ";
cin >> i;
odd (i);
} while (i!=0);
for ( k=0 ; k<5 ; k++ )
{
result += billy[k];
}
cout << result;
return 0;
}
这是我在主要编写的代码中遇到问题的部分。更具体地说,结果+ = billy [k]行抛出错误:
for ( k=0 ; k<5 ; k++ )
{
result += billy[k];
}
cout << result;
这对应于:
//tutorial #3.1 Initializing and accessing the values of an array
float billy [] = {16, 2, 77, 40, 12071};
float k, result=0;
我需要帮助修复此代码,但最重要的是我想知道为什么我的代码把这个错误告诉了我以及如何让它变得更好(在初级阶段 - 显然,我不是在这里实施先进的概念)。
编辑11:10 PM我忘记将其切换回int。我最初有int但是由于错误,我想我会尝试浮动,看看它是否有所作为。
答案 0 :(得分:2)
您不能将浮点类型用作数组的索引。 k
应为整数。
答案 1 :(得分:1)
您应该使用整数作为数组的索引。也就是说,将k
声明为整数。
考虑这一点的一种方法是,如果您尝试billy[1.5]
怎么办?这是否有意义,如果它返回第一个元素或第二个元素,或者介于两者之间,或者什么都没有?访问billy[1.5]
没有意义,您应该将数组视为一组离散值,索引应该只指向其中一个值。
float billy[] = {1.0, 2.0, 3.0, 4.0, 5.0};
float result = 0.0;
int k;
for (k = 0; k < 5; k++)
{
result += billy[k];
}
cout << result << endl;
此外,除非您确实需要在程序中的任何位置访问result
,billy
和k
,否则应考虑将其移至main
函数内。正如Neil Kirk在其中一条评论中所说,如果你在循环中只需要k
,你可以写下以下内容:
for (int k = 0; k < 5; k++)
{
// Do Something
}
答案 2 :(得分:1)
根据C ++标准,数组索引必须是整数类型,您需要将变量k
更改为整数类型。
§8.3.4数组
在声明T D中,D的格式为
D1 [ constant-expressionopt] attribute-specifier-seqopt
并且声明T D1中的标识符类型是“derived-declarator-type-list T”,那么D的标识符的类型是数组类型;
如果存在常量表达式(5.19),则它应为积分常量表达式,其值应大于零。常量表达式指定数组中的(元素数)的边界。如果常量表达式的值为N,则数组具有编号为0到N-1的N个元素,并且D的标识符的类型是“N T的derived-declarator-type-list array”。
现在看§5.19常量表达式
§5.19.4 由于本国际标准对此没有任何限制 浮点运算的准确性,未指明在翻译期间对浮点表达式的评估是否产生与评估相同表达式相同的结果