我试图在代码中找到车辆的平均价格和里程数。我正在使用指针通过函数运行值,但是,函数的输出不正确。
public function send_email()
{
$email = $this->session->userdata('email');
echo $email;
/*
$this->email->to($email);
do whatever u want
*/
}
我知道我不能直接添加指针,所以我将它们添加到变量来存储值。但是,它似乎不存储值。 avgprice 的值为0, avgmiles 的值为12000。
答案 0 :(得分:3)
你在这里犯了三个错误:
=+
而不是+=
,因此每次都会重新分配值,而不是添加它。%d
(十进制)printf说明符,而不是浮点说明符:%f
。avgmile
为int
,而实际上是float
。#include <stdio.h>
#include <string.h>
#define SIZE 4
typedef struct {
int year;
char model[11];
} Model;
typedef struct {
float price;
} Price;
typedef struct {
int miles;
} Miles;
typedef struct {
Model year, model;
Price price;
Miles miles;
struct Car *next;
} Car;
float averagePrice(Car *ptr_car, Car *ptr_car2, Car *ptr_car3, Car* ptr_car4);
int averageMiles(Car *ptr_car, Car *ptr_car2, Car *ptr_car3, Car *ptr_car4);
int main(void) {
Car f1, im, f2, g;
int i, cnt;
float avgprice, avgmile;
f1.year.year = 56;
strcpy(f1.model.model, "Ford");
f1.price.price = 500.00;
f1.miles.miles = 23000;
im.year.year = 64;
strcpy(im.model.model, "Impala");
im.price.price = 1800.00;
im.miles.miles = 12000;
f2.year.year = 57;
strcpy(f2.model.model, "Ford");
f2.price.price = 1400.00;
f2.miles.miles = 22000;
g.year.year = 65;
strcpy(g.model.model, "Galaxy");
g.price.price = 2600.00;
g.miles.miles = 48000;
avgprice = averagePrice(&f1, &im, &f2, &g);
avgmile = averageMiles(&f1, &im, &f2, &g);
printf("The average price of the vehicle is: %f.\n", avgprice);
printf("The average miles of the vehicle is: %f.\n", avgmile);
getchar();
return 0;
}
float averagePrice(Car *ptr_car, Car *ptr_car2, Car *ptr_car3, Car *ptr_car4) {
float total;
total = ptr_car->price.price;
total += ptr_car2->price.price;
total += ptr_car3->price.price;
total += ptr_car4->price.price;
total = total / SIZE;
return total;
}
int averageMiles(Car *ptr_car, Car *ptr_car2, Car *ptr_car3, Car* ptr_car4) {
float total;
total = ptr_car->miles.miles;
total += ptr_car2->miles.miles;
total += ptr_car3->miles.miles;
total += ptr_car4->miles.miles;
total = total / SIZE;
return total;
}