Program Crashes Towards End.

closed account (Dj654iN6)
I am not getting any compiling errors.
The program crashes at the end of the first for loop.
I haven't put in any validation checks (will do it when I figure out what i'm doing wrong here), so data is assumed to be entered without human errors.
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
#include <iostream>
#include <string>
using namespace std;
int main()
{ 
int weightone[5], weighttwo[5], diffofweight[5]; ///string array declerations///
int count=0, count2=0, count3=0; ///counter declerations///
string names[5]; ///string array decleration///
for (count=0; count<7; count++) /// Name and First Weight Loop ///
{
cout << "Key In Pupil Name" << "\n"; ///Input Statement In String///
cin>> names[count]; ///Data Entry///
cout<< "Key In Pupil Weight In Kilograms" << "\n"; ///Weight Input Statement///
cin>> weightone[count]; /// Weight Data Entry///	
}
for (count2=0; count2<6; count2++) /// New Weight Loop///
{
cout<< "Key In New Weight Of Pupil \n"; /// New Weight Input Statement/// 
cin>> weighttwo[count2]; /// Weight Data Entry///
}
for (count3=0; count3<6; count3++) /// Difference of Weights Loop///
{ 
diffofweight[count3] = weightone[count3]-weighttwo[count3]; /// Difference Expression///
cout<< "The Difference Between The Weights is:" << " " << diffofweight[count3]; /// Output Difference///
}
cin.get();
}
What do you expect to happen? You defined your arrays with sizes of 5 yet you try to insert up to 7 names into the array.

closed account (SECMoG1T)
I agree with @jlb and that trend is evident in all your loops
Last edited on
closed account (Dj654iN6)
but dont we start counting elements from 0 in arrays? suppose x[5] = 6 array elements ?
Yes arrays start at zero, and stop at size - 1. So an array with a size of 5 has five elements, 0, 1, 2, 3, 4. With your first loop, you're trying to insert 7 elements into an array that can only hold 5 elements.

closed account (Dj654iN6)
Thankyou, got it working.
Topic archived. No new replies allowed.