harel avv
harel avv

Reputation: 45

C++ program doesn't output when I run on cmd or VScode (but on repl.it it does)

I have this C++ program I wrote that sums vectors:

#include <cmath>
#include <iostream>

using namespace std;

const float half_Pi = acos(0.0);

double *vectorSum(double *lengths, double *angels, int size, bool cartazian) {
  double Cx, Cy, *res;
  for (int i = 0; i < size; i++) {
    Cx += cos(angels[i] / 90 * half_Pi) * lengths[i];
    Cy += sin(angels[i] / 90 * half_Pi) * lengths[i];
  }
  if (cartazian) {
    res[0] = Cx;
    res[1] = Cy;
  } else {
    res[0] = sqrt(Cx * Cx + Cy * Cy);
    res[1] = atan(Cy / Cx) * 90 / half_Pi;
  }
  return res;
}

int main() {
  int numVectors, i = 0, carta;
  bool cartazian;
  cout << "enter number of vectors to sum: ";
  cin >> numVectors;
  double *lengths = new double[numVectors];
  double *angels = new double[numVectors];
  while (i < numVectors) {
    cout << "enter length " << i + 1 << ": ";
    cin >> lengths[i];
    cout << "enter angel " << i + 1 << ": ";
    cin >> angels[i];
    i++;
  }
  cout << "would you like the sum presented in x and y? enter 1 for yes and 0 "
          "for no: ";
  cin >> carta;
  if (carta == 0)
    cartazian = false;
  else if (carta == 1)
    cartazian = true;
  else
    throw("must enter either 0 or 1");
  double *totalVector = vectorSum(lengths, angels, numVectors, cartazian);
  if (cartazian)
    cout << "Vx = " << totalVector[0] << endl
         << "Vy = " << totalVector[1] << endl;
  else
    cout << "length = " << totalVector[0] << endl
         << "angle = " << totalVector[1] << "\u00B0" << endl;
  return 0;
}

I've been able to run it completely fine on repl.it but when I try to run it on VScode (with minGW) or cmd it runs fine until I'm done with the input (doesn't show the result). Why doesn't it show the result? Is it because the throw (tried without and still not)? I don't think it's because the math functions because I ran them fine on another test file. How do I fix this?

Upvotes: 2

Views: 214

Answers (1)

paddy
paddy

Reputation: 63481

The function vectorSum has undefined behavior due to the uninitialized pointer res which you are assigning data to as if it points to valid memory.

Note that you also have undefined behavior because you don't initialize the values Cx and Cy, but then start adding to them.

A naive fix for the first issue would be to allocate memory and then make the caller responsible for freeing it, or use smart pointers or a std::vector<double>, but really all you need is something like std::pair<double, double> instead. As for the second issue, it's as simple as initializing your values to zero.

Note that std::pair is defined in <utility> so you will need to include that.

std::pair<double, double> vectorSum(double *lengths, double *angels, int size, bool cartazian)
{
  std::pair<double, double> res;
  double Cx = 0, Cy = 0;
  for (int i = 0; i < size; i++) {
    Cx += cos(angels[i] / 90 * half_Pi) * lengths[i];
    Cy += sin(angels[i] / 90 * half_Pi) * lengths[i];
  }
  if (cartazian) {
    res.first = Cx;
    res.second = Cy;
  } else {
    res.first = sqrt(Cx * Cx + Cy * Cy);
    res.second = atan(Cy / Cx) * 90 / half_Pi;
  }
  return res;
}

The call:

std::pair<double, double> totalVector = vectorSum(lengths, angels, numVectors, cartazian);
if (cartazian)
  cout << "Vx = " << totalVector.first << endl
       << "Vy = " << totalVector.second << endl;
else
  cout << "length = " << totalVector.first << endl
       << "angle = " << totalVector.second << "\u00B0" << endl;

One last point is to take care of spelling things correctly in code. For instance, the correct spellings are:

  • angles
  • cartesian

Upvotes: 1

Related Questions