1

I have a for loop that prints a comma and a space after each item in a list. It is also outputting a comma and space after the last item in the list. How do I get rid of this?

for (i = 0; i < NUM_VALS; ++i) {
   cin >> hourlyTemp[i];
}

for (i = 0; i < NUM_VALS; i++) {
   cout << hourlyTemp[i];
   cout << ", ";
}

My output is : 90, 92, 94, 95, I want it to be : 90, 92, 94, 95

Jarod42
  • 190,553
  • 13
  • 166
  • 271

1 Answers1

3

Since you're using a loop with incremented integer, this version of solution is simple and fast.

if(0 < NUM_VALS ) cout << hourlyTemp[0];

for (i = 1; i < NUM_VALS; i++) {
   cout << ", " << hourlyTemp[i];  
}
acegs
  • 2,453
  • 1
  • 19
  • 31