簡體   English   中英

當我在 cmd 或 VScode 上運行時,C++ 程序不輸出(但在 repl.it 上確實如此)

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

我有我寫的這個 C++ 程序,它對向量求和:

#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;
}

我已經能夠在 repl.it 上完全運行它,但是當我嘗試在 VScode(使用 minGW)或 cmd 上運行它時,它運行良好,直到我完成輸入(不顯示結果)。 為什么不顯示結果? 是不是因為throw (嘗試過但仍然沒有)? 我不認為這是因為數學函數,因為我在另一個測試文件上運行得很好。 我該如何解決?

函數vectorSum具有未定義的行為,因為您將數據分配給未初始化的指針 res,就好像它指向有效內存一樣。

請注意,您還有未定義的行為,因為您沒有初始化值CxCy ,而是開始添加它們。

第一個問題的一個天真的解決方法是分配內存,然后讓調用者負責釋放它,或者使用智能指針或std::vector<double> ,但實際上你所需要的只是像std::pair<double, double>代替。 至於第二個問題,就像將您的值初始化為零一樣簡單。

請注意, std::pair是在<utility>定義的,因此您需要包含它。

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;
}

電話:

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;

最后一點是注意在代碼中正確拼寫。 例如,正確的拼寫是:

  • 角度
  • 笛卡爾

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM