-
Arrays
why does my second loop (for a....) not return pupil[0] it will only start from pupil[1]
Code:
#include <iostream>
#include <string>
using namespace std;
int main(){
char *pupil[10]= {
"Ben",
"Graham",
"Joe",
"James",
"Tom",
"Tim"};
char house[]= {0,6};
char clss[]= {0,6};
char game1[]= {0,6};
char game2[]= {0,6};
char game3[]= {0,6};
char game4[]= {0,6};
char ingame[]= {0,6};
char x;
int b=1;
for(int i = 0;i <= 5; i++){
cout<<i;
cout<<"Enter house for : "<<pupil[i]<<endl;
cin>>x;
house[i]=x;
}
for(int a = 0;a <= 5;a++){
cout<<pupil[a]<<endl;
cout<<house[a]<<endl;
}
return 0;
}
-
The way you declared your house, clss, and game arrays, the size of the array is determined by the number of entries in the list. Each of those lists have two entries, so each of those arrays has two elements the first initialized to the character with the code 0, the second initialized with the character with the code 6. What you probably meant to do was:Later your first loop writes to house past its bounds which causes undefined behavior- meaning anything can happen like the error you experienced, a crash or some other problem.
-
aaah yes lol that would explain alot.....thats the narrowed down version of wat im doing and may explain the rest of the weird behaviour!
thank you!