-1
#include <iostream>
using namespace std;
int main(){
int N,unit_distance,total_distance,gas_number,sum_of_distance,giant_number,temp;
bool reach;
cin >> N;

for (int all=0;all<N;all++)
{

    reach =true;
    sum_of_distance=0;
    cin >> unit_distance >> total_distance >> gas_number;

    int gas[gas_number+1];
    for (int i=0;i<gas_number;i++)
        cin >> gas[i];
    gas[gas_number] = total_distance;


    // cin >> giant_number;


}

return 0;
}

This is part of my code for a question. The problem is that the line "cin >> giant_number" is causing runtime error on the Online Judge, as long as this line is removed the code can run successfully without runtime error. I just really cannot understand why a single "cin" code can cause runtime error. Meanwhile, the problem doesn't exist when I run it in Xcode.

Frozen
  • 1
  • 4

2 Answers2

1

You never check that your input was successful. If a non-digit is entered, attempting to read an integer will fail and you'll get some strange values. Make sure you verify that the input is success and, depending on how the values are used, are in a sane range, e.g.:

if (std::cin >> gas_number && gas_number < max_gas_number) {
    std::vector<int> gas(gas_number + 1);
    // ...
}

Also note that the code you posted is not C++: there are no dynamically sized arrays in C++.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • There are! Since C++11 standard, you can dynamically allocate arrays without 'new' operator. The other matter is that MS VC compiler doesn't keep the standard. – enedil Jun 01 '14 at 12:14
  • @enedil [No, there really aren't, in C++11.](http://stackoverflow.com/questions/8593643/does-c-support-variable-length-arrays) –  Jun 01 '14 at 12:15
  • @hvd I guess there is? I got "Accepted" on the Online Judge using dynamical arrays without 'new' operator for some other questions. – Frozen Jun 01 '14 at 12:19
  • @enedil: can you please provide a quote from the C++ standard? Feel free to use the latest working draft (which is actually for C++14 and I think is [n3936](http://www.open-std.org/JTC1/SC22/WG21/prot/14882fdis/n3936.pdf)). Since I'm regularly attending the committee meetings and following this particular change at some level, I'm pretty sure you won't find any hint of dynamically sized arrays, though. It is an extension for some compilers which may explain why the code compiled. – Dietmar Kühl Jun 01 '14 at 12:21
  • @Frozen Compilers are permitted to allow it as an extension, and your Online Judge may allow the use of compiler extensions, but that doesn't make it valid C++. –  Jun 01 '14 at 12:21
  • Ok, my bad. I was convinced they are. – enedil Jun 01 '14 at 12:26
0

Variables of number types are stored in your memory. For instance, int is stored in 4 bytes and has a range of possible values of:

–2,147,483,648 to 2,147,483,647

I believe you have typed a number which is too large and overflows the type. If the range is not enough for you, you can use __int64, for instance, which has a range of possible values as follows:

–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

as it is stored in 8 bytes.

__int64 is not standard, therefore I recommend reading this post: Fixed-width integers in C++

Read more here: http://msdn.microsoft.com/en-us/library/s3f49ktz.aspx

Community
  • 1
  • 1
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • `__int64` is a non-standard type defined by Microsoft's compiler (a valid extension). The OP mentions Xcode, so how sure are you that the OP's compiler supports `__int64`? –  Jun 01 '14 at 12:20