Need help: cin contents to a vector

Hey guys!

So I'm trying to write a basic program that would allow the user to input a set of numbers (student grades) and get an average of the grades.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  int main()
{
  vector<int> gradeVector;
  int myint;
  
  cout << "Please enter the grades for the student: ";
  
  do{
      cin >> myint;
      gradeVector.push_back (myint);
  } while (myint != '\n');
  
  cout << "gradeVector stores " << int(gradeVector.size()) << " numbers.\n";
  
  return 0;
}


The problem is, I the user input ends when the user presses enter.

As you can see, the do while loop ends when the user goes to the next line. This is causing the vector to only store the first number...

Please help! I'm not sure what to do!
Last edited on
closed account (E0p9LyTq)
Consider making the while test condition something like while(myInt != -1) or some other numeric value that is not a valid test score.

Sample output after making the change:

Please enter the grades for the student: 12
15
25
50
75
-1

gradeVector stores 6 numbers.
I just wanted to add a few notes:

variable myInt is of type int, which means it can contain only numbers. It can't contain characters or newlines.

You should not compare numbers to characters (as they are not equal).

Due to some pecularities of the C and C++ languages, the value '\n' will likely be converted to number 10. And that is not really what you want.

Thanks Kevin C!

FurryGuy, I'm having a problem with that condition.

Let's say I just want to enter 5 different grades. I enter the 5 different grades: (100 75 78 88 94), then I enter the -1 to end the loop. This tells my vector that there are 6 values instead of the 5 I want to store.
I finally figured out how to get what I wanted with this:

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
int main()
{
  string studentFirst;
  string studentLast;
  cout << "Please enter the name of the student (FirstName LastName): ";
  
  cin >> studentFirst >> studentLast;
  
  vector<double> gradeVector;
  double mydouble;
  
  cout << "\n\nPlease enter the grades for " << studentFirst << ' ' << studentLast << ": ";
  
  
        string input;
        getline(cin, input);
        istringstream iss(input);
        while (iss >> mydouble)
        {
              gradeVector.push_back(mydouble);
        } 
    
        
        cout << "gradeVector stores " << double(gradeVector.size()) << " numbers.\n";
        
        
  
  return 0;
}


Now I've come to another problem...After entering the name of the student, no integers are able to be assigned to the vector.

Now what am I doing wrong?
add after reading the student name:

1
2
  string ignore;
  getline(cin, ignore);


... because you need to ignore the remainder of the input line containing the student's name. Otherwise, the getline in your program just reads the remainder of the line containg the student's name, and there is nothing after the student's name on that line.
closed account (E0p9LyTq)
This tells my vector that there are 6 values instead of the 5 I want to store.

Then you can either subtract 1 from the size, or remove the last entry (the -1).

Or rewrite your loop:

1
2
3
4
5
6
7
8
9
while (true)
{
   std::cin >> myint;
   if (myint == -1)
   {
      break;
   }
   gradeVector.push_back (myint);
}


There are usually more than just one way to do something.
Thank you so much Kevin C!

A lot of help! AND fast replies!
I really appreciate it!
Topic archived. No new replies allowed.