John
John

Reputation: 55

C++ code breaks into line and for the love of me I can't fix it

Basically my code is supposed to take input from the user about the runners of a marathon, and then display the 3 best times and have the ability to search and display any runner. Right now the code is still pretty bare-bones, but it should at least take the input it, organize the info in ascending order (winners function) and I was in the middle of writing the display function when I decided to compile the code to test, and then it all goes down hill. Basically when I have to input the name of the first runner, the code breaks into this line:

static void __CLRCALL_OR_CDECL assign(_Elem& _Left, const _Elem& _Right) _NOEXCEPT
{   // assign an element
    _Left = _Right;
}

First of all I have no idea what that means or why left = right, and second a friend ran my code on his machine and told me he encountered no errors.

Here is my code:

#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>

using namespace std;

#pragma once
#include <string>
using namespace std;
class race
{
private:

public:
    int timeH, timeM, timeS, timeT;
    int number;
    string name;

    void input();
};

int size;
race *runner = new race[size];
void input();
void winners();
void display();
int main(){


    input();
    winners();
    display();

    system("pause");
}
void input(){

    cout << "Indique la cantidad de corredores: ";
    cin >> size;

    for (int i = 0; i < size; i++){
        cout << "Nombre del corredor ";
        cin >> runner[i].name;
        cout << "Numero del corredor # "<<i;
        cin >> runner[i].number;
        cout << "Tiempo del corredor # " << i << endl << "Indique cuantas horas tardo :";
        cin >> runner[i].timeH;
        runner[i].timeH = runner[i].timeH * 3600;
        cout << "Cuantos minutos: ";
        cin >> runner[i].timeM;
        runner[i].timeM = runner[i].timeM * 60;
        cout << "Cuantos segundos: ";
        cin >> runner[i].timeS;

        runner[i].timeT = runner[i].timeH + runner[i].timeM + runner[i].timeS;
    }

}
void winners(){

    race temp;
    int flag = 1;
    for (int j = 1; (j <= size) && flag; j++){

        flag = 0;
        for (int i = 0; i < size-1; i++){

            if (runner[i + 1].timeT < runner[i].timeT)
            {
                temp = runner[i];
                runner[i] = runner[i + 1];
                runner[i + 1] = temp;


                flag = 1;
            }
        }
    }
}

Thanks in advance, any help is appreciated.

Upvotes: 0

Views: 93

Answers (2)

Cornstalks
Cornstalks

Reputation: 38238

int size;
race *runner = new race[size];

That's a problem right there. You're allocating memory, but you haven't set size yet (that allocation runs before main() is invoked). That means you are doing new race[0] (because size is initialized to zero, since it's a global int). Ergo, the rest of your program is invoking undefined behavior, because runner is an empty array, which you're trying to (illegally) access.

You're also leaking memory, as you never delete [] the memory you allocate with new []. I suggest heading Joachim's advice and using std::vector.

Upvotes: 3

Some programmer dude
Some programmer dude

Reputation: 409482

The problem is this:

int size;
race *runner = new race[size];

Uninitialized global variables are zero initialized, so size will be initialized to zero, so you're allocating a zero-sized array which means any access to the array will be out of bounds and lead to undefined behavior.

There are two ways of solving this:

  1. Wait with the allocation until you know the size. (Not the solution I recommend.)

  2. Use std::vector which is a dynamically sized "array" type (the solution I do recommend).

Upvotes: 2

Related Questions