Home > Software design >  Unsure why code is repeating certain outputs
Unsure why code is repeating certain outputs

Time:12-18

I am doing this for a lab at school, however, in my code i get the correct outputs, but for some reason my inputs are repeating themselves. I am unsure why they are doing this, and have tried editing my code in several different ways in order to fix the problem, but to no avail.

here is my original code:

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

const double PI = acos(-1); // arccos(-1) produces the value pi
double DirectTime (double rDestination) {
return 2.0 * sqrt(fabs(rDestination - 1.496e11) / 10) / 86400.0;
}

const double MU = 1.3274745e20;
const int SEC_IN_DAY = 86400;
const double R_EARTH = 1.496e11;
const double R_VENUS = 1.08e11;
const double R_JUPITER = 7.778e11;
const double R_PLUTO = 5.91e12;


double ConvertSecondsToDays(double seconds);
double CubedSum(double v1, double v2);
double HohmannTime(double r1, double r2);

double ConvertSecondsToDays(double seconds) {
double days = 0;
days = seconds / SEC_IN_DAY;
cout << days;
return days;
}

double CubedSum(double v1, double v2) {
    return pow(v1 v2,3);
}

double HohmannTime(double r1, double r2 = R_EARTH) {
return ConvertSecondsToDays( PI * sqrt(CubedSum(r1, r2)/ (8 * MU)));
}


int main() {
  
  printf("%-10s%-15s%-s\n", "Planet", "Hohmann Time", "Direct Time");
  printf("%-10s%-15.2f%-.2f\n", "Venus", HohmannTime(R_VENUS, R_EARTH), DirectTime(R_VENUS));
  printf("%-10s%-15.2f%-.2f\n", "Jupiter", HohmannTime(R_JUPITER, R_EARTH), DirectTime(R_JUPITER));
  printf("%-10s%-15.2f%-.2f\n", "Pluto", HohmannTime(R_PLUTO, R_EARTH), DirectTime(R_PLUTO));


return 0;
}

When all is said and done, it is supposed to output the following:

Planet    Hohmann Time   Direct Time
Venus     145.88         1.49
Jupiter   996.83         5.80
Pluto     16643.47       17.57

However, mine keeps outputting this:

Planet    Hohmann Time   Direct Time
145.88Venus     145.88         1.49
996.503Jupiter   996.50         5.80
16643.5Pluto     16643.47       17.57

I'm thinking that the issue is either with what im printing for the functions, or i have them repeating the output somewhere without realizing it.

P.S

This is my first question on stack. I have heard that alot of people ask bad question, so if i am doing something wrong, please let me know so i can do better!!

Thanks for your help!!

CodePudding user response:

The problem is that your ConvertSecondsToDays function is inadvertently printing its result to stdout via:

cout << days;

That is intermingled with the real output done in main. Remove the cout call and you should be good.

  • Related