繁体   English   中英

C ++ cout不打印所有参数

[英]C++ cout not printing all args

我有以下代码来打印出Car对象。 所有字段均可公开访问。

void print_cars_array(Car cars[]) {
/**
 * Prints all cars in the given car array.
 */
  for(int i = 0; i < NUM_CARS; i++) {
      std::cout << "Car #" << i + 1 << std::endl;
      std::cout << cars[i].year << ' ' << cars[i].color << ' ' << cars[i].make << ' ' << cars[i].model << std::endl;
  }
}

但是,这给了我以下输出:

Car #1
 Subaru Outback
Car #2
 Toyota Corolla
...

起初我以为前两个字段搞砸了,但是修改了这个循环:

void print_cars_array(Car cars[]) {
/**
 * Prints all cars in the given car array.
 */
  for(int i = 0; i < NUM_CARS; i++) {
      std::cout << "Car #" << i + 1 << std::endl;
      std::cout << cars[i].year << std::endl;
      std::cout << cars[i].color << std::endl;
      std::cout << cars[i].year << ' ' << cars[i].color << ' ' << cars[i].make << ' ' << cars[i].model << std::endl;
  }
}

产生以下内容:

Car #1
2016
green
 Subaru Outback
Car #2
2006
white
 Toyota Corolla

我是否想知道为什么不打印这些内容? year外的所有字段均为字符串, year为int。

尝试to_string()因为问题可能与int和字符串类型串联有关

参考: http : //www.cplusplus.com/reference/string/to_string/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM