gpt4 book ai didi

c++ - 我的程序将运行,但它会打印出所有应该完全不同的值的 100

转载 作者:搜寻专家 更新时间:2023-10-31 01:42:18 27 4
gpt4 key购买 nike

我的程序会运行,但是当它打印出一些 vector 值时,它们都变成了 100,就我的编程技能而言(相当低),我认为没有理由它应该创造值(value)都是 100。我认为这可能与 input_seconds 有关,但我不确定。有什么问题的建议吗?

#include<iostream>
#include<cmath>
#include<vector>
using namespace std;

double altitude(){
double alti;
cout << "Please input altitude in meters:";
cin >> alti;
return alti;
}

double RoC()
{
double climbR;
cout << "Please input climb rate in m/s:";
cin >> climbR;
return climbR;
}

double speed(){
double v;
cout << "Please input your current speed over ground in m/s" << endl;
cin >> v;
return v;
}

// Gives you the time it will take to reach desired altitude
double time(double A, double R){
double t;
t = A / R;
return t;
}

//Distance travelled horizontally in given time
double distancetravelled(double Veloc, double Time){
double D;
D = Veloc*Time;
return D;
}

//This will convert time to days, hours, minutes, and seconds.
vector<double> converted_time(double input_seconds){
double days;
double hours;
double minutes;
double seconds;
days = input_seconds / 60 / 60 / 24;
hours = (input_seconds / 60 / 60) / 24;
minutes = (input_seconds / 60) / 60;
seconds = (input_seconds) / 60;
//puts values into a vector
vector<double>times;
times[0] = days;
times[1] = hours;
times[2] = minutes;
times[3] = seconds;

return times;
}

void print_vector(vector<double>converted_time){
cout << "The time it will take for the plane to reach its desired altitude is ";
cout << converted_time[0] << " days, " << endl;
cout << converted_time[1] << " hours, ";
cout << converted_time[2] << " minutes and ";
cout << converted_time[3] << " seconds" << endl;
}

void print_result (double V, double D){
cout << "The distance over ground you will travel will be ";
cout << D << " meters"<<endl;
}

int main(){
double A;
double R;
double T;
double V;
double D;
R = RoC();
A = altitude();
T = time(A, R);
vector<double>converted_time(4);

double days = converted_time[0];
double hours = converted_time[1];
double minutes = converted_time[2];
double seconds = converted_time[3];

V = speed();
D = distancetravelled(T,V);

print_result(V, D);
print_vector(converted_time);
return 0;
}

最佳答案

问题 1:您没有正确计算小时、分钟和秒。

 days = input_seconds / 60 / 60 / 24;

// hours = (input_seconds / 60 / 60) / 24;
// This is wrong since it is same as
// hours = input_seconds / 60 / 60 / 24;
// You need:
hours = (input_seconds / 60 / 60) % 24;

// Similarly, change how you compute minutes and seconds.
minutes = (input_seconds / 60) % 60;
seconds = (input_seconds) % 60;

问题 2:在为元素赋值之前,您没有创建足够大的 vector 。

 // vector<double> times;
// This creates an empty vector. Use:
vector<double> times(4);

times[0] = days;
times[1] = hours;
times[2] = minutes;
times[3] = seconds;

问题 3:您没有在 main 中调用函数 converted_time

vector<double> converted_time(4);

只是将 converted_time 定义为局部变量。你需要这样的东西:

vector<double> convertedTimes = converted_time(4);

关于c++ - 我的程序将运行,但它会打印出所有应该完全不同的值的 100,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27163531/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com