1

This is my entire code (in C++) in case it's necessary. I'm using 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;
}

This is the part of my code written in the main that is having the issue. More specifically, the result += billy[k] line is throwing the error:

      for ( k=0 ; k<5 ; k++ )
       {
          result += billy[k];
       }
       cout << result;

This corresponds with:

//tutorial #3.1 Initializing and accessing the values of an array
float billy [] = {16, 2, 77, 40, 12071};
float k, result=0;

I need help fixing this code but most importantly I want to know why my code threw me this error and how I could make it better (at an elementary level--obviously, I'm not implementing advanced concepts here).

EDIT 11:10PM I forgot to switch it back to int. I had int initially but due to the error I figured I would try float to see if it made a difference.

4
  • 3
    Why are you using a floating-point data type to iterate from 0 to 5? Commented Aug 31, 2013 at 2:36
  • 2
    Try to declare variables closer to where they are used. In particular, declare the counting variable inside for loops. For example, for (int k = 0; k < 5; k++) That would make it easier to spot mistakes such as the wrong types used. Commented Aug 31, 2013 at 2:42
  • Chris, I added a note to my original post about why I used float. Neil Kirk, I tried as you suggested and I'm still getting the same error. Thanks for the advice though because I am definitely going to continue doing that from now on. Commented Aug 31, 2013 at 3:14
  • Try to cut out as much as you can from your code while still getting the error, and post a new question. Commented Aug 31, 2013 at 3:49

3 Answers 3

2

You cannot use floating point types as indexes for arrays. k should be an integer.

Sign up to request clarification or add additional context in comments.

Comments

1

You should use integers for indices to arrays. That is, declare k as an integer.

One way to think about this is, what if you tried billy[1.5]? Does this make sense, should this return the first element or the second element, or something in between, or nothing at all? Accessing billy[1.5] doesn't make sense, you should think about arrays as a discrete set of values and the index should only ever point to one of these values.

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;

Also, unless you really need to access result, billy and k everywhere in your program, you should consider moving them to inside your main function. As Neil Kirk said in one of the comments, you can write the following if you only need k inside your loop:

for (int k = 0; k < 5; k++)
{
    // Do Something
}

1 Comment

Thanks! I liked the way you explained it. It doesn't make sense for elements in an array to be less than or more than whole. I'm still struggling with with I'm encountering the error message though. I think that with the few corrections I made, my program should compile. I'm beginning to wonder if my main is so big that maybe something in the lines above the function is causing it to not work. Edit: I just moved it and it appears that the same error is on the line that this was on but it is unrelated to my code. Thanks again for the help!
1

as per C++ standard, array index must be integer type, you need to change your variable k to integral type.

§8.3.4 Arrays

In a declaration T D where D has the form

D1 [ constant-expressionopt] attribute-specifier-seqopt

and the type of the identifier in the declaration T D1 is “derived-declarator-type-list T”, then the type of the identifier of D is an array type;

If the constant-expression (5.19) is present, it shall be an integral constant expression and its value shall be greater than zero. The constant expression specifies the bound of (number of elements in) the array. If the value of the constant expression is N, the array has N elements numbered 0 to N-1, and the type of the identifier of D is “derived-declarator-type-list array of N T”.

Now look at § 5.19 Constant expressions

§ 5.19.4 Since this International Standard imposes no restrictions on the accuracy of floating-point operations, it is unspecified whether the evaluation of a floating-point expression during translation yields the same result as the evaluation of the same expression

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.