Trying to get this code to print 90, 92, 94, 95 hence no comma after the 95. Min
ID: 674146 • Letter: T
Question
Trying to get this code to print 90, 92, 94, 95 hence no comma after the 95. Mine is printing a comma after 95. How do i get it not to do this
#include <iostream>
#include <vector>
using namespace std;
int main() {
const int NUM_VALS = 4;
vector<int> hourlyTemp(NUM_VALS);
int i = 0;
hourlyTemp.at(0) = 90;
hourlyTemp.at(1) = 92;
hourlyTemp.at(2) = 94;
hourlyTemp.at(3) = 95;
for(i=0; i<NUM_VALS; ++i){
cin >> hourlyTemp.at(i);
}
for(i=0;i<NUM_VALS; ++i){
cout << hourlyTemp.at(i) << ", ";} /*Trying to get this to print 90, 92, 94, 95 */
cout << endl;
return 0;
}
Explanation / Answer
#include <iostream>
#include <vector>
using namespace std;
int main() {
const int NUM_VALS = 4;
vector<int> hourlyTemp(NUM_VALS);
int i = 0;
hourlyTemp.at(0) = 90;
hourlyTemp.at(1) = 92;
hourlyTemp.at(2) = 94;
hourlyTemp.at(3) = 95;
for(i=0; i<NUM_VALS; ++i){
cin >> hourlyTemp.at(i);
}
for(i=0;i<NUM_VALS; ++i){
cout << hourlyTemp.at(i) << (i == NUM_VALS-1 ? "" : ", ");} /*Trying to get this to print 90, 92, 94, 95 */
cout << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.