For Loop not allowing inputs after first loop??

Mar 1, 2013 at 8:31pm
Hi everyone,

I'm very new to programing. I'm trying to create a program that will allocate the cost of an environmental clean up over a number of insurance companies that insured the contaminated property during the time of contamination based both upon the time they insured the property and their annaul policy limits.

I created a vector to take the name of each insurance company entered by the user. I need dynamic memory allocation becuase sometimes there's two insurers, other times there are 20. I used a for loop similar to the loop below, but I"m at work and don't have access to the actual code I wrote, but it behaved exactly like the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <vector>
using namespace std;
 
int main()
{
    vector<double> student_marks;
    // no size specified: vector contains
    // no elements
 
int num_students;
cout << "Number of students: " << flush;
cin >> num_students;
 
student_marks.resize (num_students);
 
for (vector<double>::size_type i = 0; i < num_students; i++)
{
    cout << "Enter marks for student #" << i+1 
         << ": " << flush;
    cin >> student_marks[i];
}
return 0; 
} 


Again, I did not write this code, but I modeled mine off of it. The problem is, the loop runs correctly once, and takes the input, but then it just prints out:"Enter marks for student 1 Enter marks for student 2," etc. After the first input, it never asks for the input for Students 2, 3, 4, etc., it just prints out "Enter marks for student . . ." over and over until the loop ends.

Can anyone tell me why?

Thanks,

Kevin
Last edited on Mar 1, 2013 at 8:33pm
Mar 1, 2013 at 9:15pm
Did you enter non-numeric data?
Mar 1, 2013 at 9:21pm
Yes, I entered "A," for example. Is that the problem? I should note that in my program at home, I did not use an int variable, I used "char."
Mar 1, 2013 at 9:28pm
I could only judge based upon the code posted. The type of behaviour you described would fit what happens if the cin statement expects a number but gets something else.

This vector needs numeric values for example,
vector<double> student_marks;

If your actual code is substantially different, then you may need to wait until you have access in order to post that code here.
Mar 1, 2013 at 10:02pm
Yes, sorry about that... I'll post up my code when I get home. Thanks,

Kevin
Topic archived. No new replies allowed.