0

Just started out on codeforces a few days ago. My solution code (in C++) to question 71A https://codeforces.com/problemset/problem/71/A isn't compiling on their server and I haven't got a clue. Need help - What have I done wrong and what can I do to fix this? The solution code that I'd submitted -

#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    string a[n];
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++)
    {
        if (a[i].length() > 10)
        {
            cout << a[i][0] << a[i].length() - 2 << a[i][a[i].length() - 1] << endl;
        }
        else
        {
            cout << a[i] << endl;
        }
    }
    return 0;
}

This was working just fine on my local compiler, but threw errors on their server -

Cannot compile file:
program.cpp
program.cpp(7) : error C2057: expected constant expression
program.cpp(7) : error C2466: cannot allocate an array of constant size 0
program.cpp(7) : error C2133: 'a' : unknown size
program.cpp(9) : error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)
  • "Just started out on codeforces" -- that's the main issue. The shown code is already not standard C++. "`string a[n];`" is not standard C++, and relies on a compiler-specific extension. Not to mention that "`using namespace std;`" [is bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice), and will eventually lead you into a world of hurt. You won't learn this from some web site, but only from a textbook. The only way to learn the most complicated language in use today is with the help of a good textbook, and not some web site, or Youtube. – Sam Varshavchik Sep 06 '20 at 14:38
  • Please recommend a good textbook. It would be of great help. Thanks a lot! – the_antagonist9509 Sep 06 '20 at 15:28
  • See [stackoverflow's list of C++ textbooks](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik Sep 06 '20 at 15:29
  • From the perspective of competitive programming, how much of the language do I need to be comfortable with before I can feasibly start? Because as a highschooler, a 1000 page book is kinda intimidating. – the_antagonist9509 Sep 07 '20 at 06:04
  • Feel free to start any time. Competitive programming is a useless waste of time that doesn't teach anything useful. If someone wants to waste their time on it, they can start any time they wish. When applying for a job, nobody will care about anyone's competitive programming score, on some web site nobody ever heard of, they'll only want to know how much one knows C++; and, yes, that takes many years of rigorous, methodical study. There's no instant gratification here, or quickie shortcuts to learning C++, the most complicated general purpose language in use today. It takes a lot of time. – Sam Varshavchik Sep 07 '20 at 13:18

0 Answers0