Vectors

I need to get rid of the space after it outputs the last weight.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  #include <iostream>
#include <vector>// FIXME include vector library
using namespace std;



int main() {
   const int NEW_WEIGHT = 5;
   vector <double> inputWeights;
   int i = 0;
   double sumWeight = 0.0;
   double AverageWeight = 1.0;
   double temp = 0.0;

   for (i = 0; i < NEW_WEIGHT; i++){
      cout << "Enter weight "<< i+1<< ": "<< endl;
      cin>> temp;
      inputWeights.push_back (temp);

      }


      cout << "\nYou entered: ";

   for (i =0; i < NEW_WEIGHT- 1; i++){
         if (NEW_WEIGHT == 4) {
			cout << inputWeights.at(i);
		   }
		   else {
		      cout << inputWeights.at(i) << " ";
		   }
   }
      cout<< inputWeights.at(inputWeights.size() - 1) << endl;

    for (i =0; i < NEW_WEIGHT; i++){

       sumWeight += inputWeights.at(i); 

      }
   cout <<"Total weight: "<< sumWeight<< endl;

       AverageWeight = sumWeight / inputWeights.size();

   cout <<"Average weight: "<< AverageWeight<< endl;

    double maxWeight = inputWeights.at(0);
    for ( i = 0; i < inputWeights.size(); ++i ) {
        if ( inputWeights.at(i) > maxWeight ) {
           maxWeight = inputWeights.at(i);
        }
    }

   cout<< "Max weight: "<< maxWeight << endl;

   return 0;
}
1
2
3
4
5
6
const int NEW_WEIGHT = 5;

 for (i =0; i < NEW_WEIGHT- 1; i++){
         if (NEW_WEIGHT == 4) {
			cout << inputWeights.at(i);
		   }

NEW_WEIGHT can never be 4, as it's declared with a constant value of 5.

I think you mean something like this:
1
2
3
4
5
6
7
for( int i = 0; i < NEW_WEIGHT; i++ ) {
    std::cout << inputWeights[i];
    // NEW_WEIGHT - 1 is the last element
    // if it's not that,
    // print a space
    if( i != NEW_WEIGHT - 1 ) std::cout << ' ';
}
Last edited on
Got it! Thanks!
Topic archived. No new replies allowed.