简体   繁体   中英

C++ cout not printing all args

I have the following code to print out a Car object. All the fields are publicly accessible.

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

However, this gives me the following output:

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

At first I thought the first two fields were messed up, but modifying the loop to this:

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

Produces the following:

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

Am I missing something as to why those won't print? All fields except year are strings, and year is an int.

Try to_string() as the issue could be with int and string type concatenation

Ref: http://www.cplusplus.com/reference/string/to_string/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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