0

Hello to all the handsome people who want to save me from my pain,

I am new to c++, and after having done the usual intro stuff like strings, loops, arrays, pointers that w3schools tells you, I decided to write some code. I decided to start with a simple challenge that asks you to write some loops that will sort a few ints from highest to lowest or the other way around, without using the sort function(I was told there was something like this).

And the code is still not working. But this is normal for me as long I can understand why is it not working. (Pls don't answer why it doesn't work, just continue reading)

#include <iostream>


using namespace std;

int age[4] = { 26, 14, 6, 43};


int num = 4;

int a = 0;

double previous = 0;


int chosen = age[a];

void first() {



    while (result[0] || result[num] == 0){

        for (int i = 0; a != num; i++) {

            chosen = age[a];

            previous = age[i];

            if (chosen < (previous + 0.01) ) {

                chosen = result[a];

                a++;

                previous = age[0];

                i = 0;
            }
        }

    }

}

void last()
{


    for (int i = 0; i < num; i++) {

        cout << result[i] << "\n";

    }
}


int main() {
    first();
    last();

}

So I tried to see if my main "if statement" was properly working and I added an already assigned value of result[0] where the variables were introduced.

int a = 0;

double previous = 0;

result[0] = 20;

int chosen = age[a];

And then when I ran the program this errors occurred:

error C2466: cannot allocate an array of constant size 0                  
(18,14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
(18,16): error C2440: 'initializing': cannot convert from 'int' to 'int [0]'
(18,12): message : There are no conversions to array types, although there are conversions to references or pointers to arrays

(line 18 is where I assigned result[0])

When I saw this I was like "I guess I am stupid enough and I don't know how to assign arrays", but then I checked if this was the case and I think this is not the case.

And then I understood the first error because I tried changing result[0] to result[4] and it disappeared, but I had no idea what the second error was saying.

So, as one wise man probably said, "If you have coding problems that google cannot answer, go to stack overflow".

And so I did. But the article I found was with code that was equal to my knowledge of Chinese. And if you are still wondering, I do not know a single Chinese word. And I didn't find a better article that could explain how to fix the problem in pure coding for beginners. I just started coding c++ in the pandemic and I don't understand anything other than English yet.

And I would really appreciate if someone could tell me what does this error mean and how is it possible to disappear with his other error friends waiting below him.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • Stop using C-style arrays. Instead use `std::array` or `std::vector`. – Jesper Juhl May 31 '20 at 18:29
  • 2
    Maybe a wise man might have said "If you have coding problems that google cannot answer, go to stack overflow", but not a wise C++ programmer. They almost always recommend [getting a book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). C++ is too complicated a language to learn from the Internet in a reasonable amount of time. A good book will let you in on the terminology to use when discussing C++, making web searches much more effective, and teach you what good programs and good advice looks like so you can better tell a good tutorial from a bad one. – user4581301 May 31 '20 at 18:57
  • That is a very good idea. Thank you, I am going to buy one. Do you have any idea for something that for example is better in most cases than any other book, or just any book? – Victor Ivanov Jun 02 '20 at 17:42

1 Answers1

1

You haven't declared result. C has the idea that using an undeclared variable causes the compiler to assume that the variable is declared extern int. Some C++ compilers support this for backwards-compatibility, but it's not part of standard C++.

In this case, the compiler seems to support this so it assumes extern int result; since it hasn't seen a declaration for this name.

This results in two diagnostics:

  • The compiler is warning you about the "default int" declaration and that this is deprecated: "missing type specifier - int assumed"
  • Then the compiler is telling you that you can't subscript an int (result[0] makes no sense if result is an int): "cannot convert from 'int' to 'int [0]'"

Add an array declaration for result to fix this.

cdhowie
  • 158,093
  • 24
  • 286
  • 300