Problem with vectors?

Jul 11, 2013 at 2:21am
Hello, I am running into a problem with my program, for some reason I am only getting the vector entered being factored into the weighted_total and I am completely lost as to why.
(The goal here is for a user to enter as many Exam, Self, Guided, and Assignment scores as they wish and have it all totaled and averaged to their final grade)

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>
#include <fstream>
#include <numeric>
#include <vector>


using namespace std;

int main() {
  string gradetype;
  vector<double> assign_vec;
  double assign_score=0.0;
  vector<double> self_vec;
  double self_score=0.0;
  vector<double> guided_vec;
  double guided_score=0.0;
  vector<double> exam_vec;
  double exam_score=0.0;
  double weighted_total=0.0;
  double assign_average=0.0;
  double self_average=0.0;
  double guided_average=0.0;
  double exam_average=0.0;

  exam_vec.clear();
  assign_vec.clear();
  self_vec.clear();
  guided_vec.clear();

  do {
    //clear out cin
    cin.clear(); cin.ignore(1000, '\n');

    cout << "Grade Type? Please Enter Self, Guided, Exam, or Assignment (blank to end): ";
    getline(cin, gradetype);
    if(!gradetype.length()) continue;

    if(gradetype == "Assignment" || "assignment"){

      cout << "Score: ";
      cin >> assign_score;
      assign_vec.push_back(assign_score);


    } else if(gradetype == "Self" || "self"){

      cout << "Score: ";
      cin >> self_score;
      self_vec.push_back(self_score);


    } else if(gradetype == "Guided" || "guided"){

      cout << "Score: ";
      cin >> guided_score;
      guided_vec.push_back(guided_score);
  

    } else if(gradetype == "Exam" || "exam"){

      cout << "Score: ";
      cin >> exam_score;
      exam_vec.push_back(exam_score);
     

        }

  } while(gradetype.length());

  if(!assign_vec.empty()) {
    assign_average = std::accumulate(assign_vec.begin(), assign_vec.end(), 0.0) / assign_vec.size();
  }

  if(!self_vec.empty()) {
    self_average = std::accumulate(self_vec.begin(), self_vec.end(), 0.0) / self_vec.size();
  }

  if(!guided_vec.empty()) {
    guided_average = std::accumulate(guided_vec.begin(), guided_vec.end(), 0.0) / guided_vec.size();
  }

  if(!exam_vec.empty()) {
    exam_average = std::accumulate(exam_vec.begin(), exam_vec.end(), 0.0) / exam_vec.size();
  }



  weighted_total = ((exam_average * 0.40) + (assign_average * 0.20) + (self_average * 0.30) + (guided_average * 0.10));

  cout << weighted_total;

return 0;
}
Last edited on Jul 11, 2013 at 4:09am
Jul 11, 2013 at 1:17pm
Still not sure why it's returning an int of the last vector entered.
Jul 11, 2013 at 3:04pm
Found the problem. In the code the 'if' checks are not correct. Consider following 'if' statement:

if(gradetype == "Assignment" || "assignment"){

Here, "assignment" will always evaluate to true and thus any value you enter will be inserted into the "assign_vec" vector. The remaining vectors always remain empty.

Change all the checks similar to the following:

if(gradetype == "Assignment" || gradetype == "assignment"){

- Kau
Last edited on Jul 11, 2013 at 3:05pm
Topic archived. No new replies allowed.