[try Beta version]
Not logged in

 
Vector problem

Mar 26, 2016 at 7:35pm
When asked to enter students grades, it only output the first number the amount of times that's consistent with how many numbers the user inputted. Why is it doing so? I could've sworn I coded this correctly.

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
using namespace std;

void fillNames(vector<string>& n) {
    string a;
    cout << "Type in some names (typing o would indicate the breaking point): ";
    cin >> a;
    
    while(a != "o") {
        n.push_back(a);
        cin >> a;
    }
    cout << endl;
}

void printNames(const vector<string>& n) {
    cout << "Student Names: ";
    
    for(int i = 0; i < n.size(); i++) cout << n[i] << " ";
    
    cout << endl;
    cout << endl;
}

void studentGrades(vector<int>& g) {
    int a;
    string b;
    cout << "Enter student grades, type done whenever you're finished: ";
    cin >> a;
    
    while(b != "done") {
        g.push_back(a);
        cin >> b;
    }
}

void printGrades(vector<int> g) {
    cout << "Student Grades: ";
    
    int i = 0;
    
    while(i < g.size()) {
        cout << g[i] << " ";
        i++;
    }
}

int main() {
    vector<string> names;
    vector<int> grades;
    
    fillNames(names);
    printNames(names);
    studentGrades(grades);
    printGrades(grades);
    
    return 0;
}
Mar 26, 2016 at 7:41pm
1
2
3
4
5
6
7
8
9
10
11
void studentGrades(vector<int>& g) {
    int a;
    string b;
    cout << "Enter student grades, type done whenever you're finished: ";
    cin >> a;
    
    while(b != "done") {
        g.push_back(a);
        cin >> b;
    }
}


This function makes no sense. You input the grade (a) once only, before the loop, and then you add the same grade to the vector every time. Shouldn't you be entering the grade every time round the loop?
Last edited on Mar 26, 2016 at 7:43pm
Mar 26, 2016 at 8:46pm
Still can't figure out what the problem is =\
Mar 26, 2016 at 9:25pm
I'll break down the logic of this function for you, as you've written it.

1
2
3
4
5
Fetch a number from the user. NEVER CHANGE THIS NUMBER
WHILE ( user hasn't said "done")
{
   add the same number to the end of the vector
} 


Last edited on Mar 26, 2016 at 9:25pm
Mar 26, 2016 at 9:31pm
I changed it up a bit but the output is still giving me crazy numbers. I can't see how this is incorrect.

1
2
3
4
5
6
7
8
9
10
void studentGrades(vector<int>& g) {
    int a;
    string b;
    cout << "Enter student grades, type done whenever you're finished: ";
    
    while(b != "done") {
        g.push_back(a);
        cin >> b;
    }
}
Last edited on Mar 26, 2016 at 9:36pm
Mar 26, 2016 at 9:45pm
Is every student grade supposed to be the same? If not, show me the line where the user types in the student grade the second time.
Last edited on Mar 26, 2016 at 9:46pm
Mar 26, 2016 at 9:52pm
When the user types in the student grades, it's the grades of one student. I just want it to output the same numbers that have been inputted by the user but instead I'm getting ridiculous numbers.
Mar 26, 2016 at 9:53pm
show me the line of code where the user types in the student grade the second time, inside the function studentGrades.
Last edited on Mar 26, 2016 at 9:54pm
Mar 26, 2016 at 9:59pm
There aren't any lines where the user types in the student grade a second time. The basic idea it that it's typed all at once by the user & that's being done on line 4. For example:

Enter student grades, type done whenever you're finished: 83 72 79 94 done

I want my studentGrades function to mimic how my fillNames function works.
Last edited on Mar 26, 2016 at 10:04pm
Mar 26, 2016 at 10:07pm

I give up. I'll just do it for you. This relies on you have a C++11 compiler, because it has to convert a string to an int (which is something you'd missed entirely) using std::stoi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void studentGrades(vector<int>& g) {
    int a;
    string b;
    cout << "Enter student grades, type done whenever you're finished: ";
    
    while(true) 
    {
       cin >> b;  // GET NEXT VALUE FROM THE INPUT SET
       if (b == "done") 
       { break;}
       else
        {
            //  Not done, so assume this b is now a number, but of course it's a string,
           //    so we need to convert it into an int before we can store it
          a = stoi(b);   
          g.push_back(a);
       }
     }
}
Topic archived. No new replies allowed.