-1

New to C++, I'm trying to create a fibonacci calculator where it will list all the steps up to a user given point and using a user given starting point.

#include <iostream>

using namespace std;

int main()
{
    int initial;
    int steps;
    int values[] = {};
    int x = 0;
    char hold;
    cout << "Start at: "; cin >> initial;
    values[0] = initial;
    cout << "Steps: "; cin >> steps;
    while(x < steps){
        if(x == 0){
            values[(1)] = values[0] + 1;
        }else {
            values[(x+1)] = values[x] + values[(x-1)];
        }
        x++;
        cout << values[x] << endl;
    }
}

I'm not getting the output i'm expecting at all :P it looks like it has something to do with the values[(x+1)] = values[x] + values[(x-1)]; line. Thanks for any help :)

MisterQuacker
  • 35
  • 1
  • 6
  • 2
    What size do you think is `values` array? What is the range of valid indexes by which it can be accessed? Discuss. – Igor Tandetnik Oct 03 '15 at 22:21
  • This construct is not valid, in either standard C or standard C++: `http://stackoverflow.com/questions/295027/array-of-zero-length`. Look here for a discussion: http://stackoverflow.com/questions/295027/array-of-zero-length – paulsm4 Oct 03 '15 at 22:27

2 Answers2

1

You are not getting any output at all. Compiling this program (gcc) gives you an error:

error: zero-size array 'values' int values[] = {};

You cannot have such a zero-sized array defined in C++. You need to specify the number of elements, like

int values[256]{}; // allocate 256 ints and initialize them to zero

If you want dynamic arrays, consider using std::vector.

vsoftco
  • 55,410
  • 12
  • 139
  • 252
0

The values array it is a fixed size of zero. You cannot set values to it without resizing. Think whether you really need an array.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
steviekm3
  • 905
  • 1
  • 8
  • 19