Hi! I'm having some trouble with char pointers, arrays and all that stuff.
I wrote a simple programme which should let me know every time it finds a letter 'a' in an array. But it outputs nothing even if I write only 'a's.
Here's my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
char * input;
void Check();
void Get();
int main(){
Get();
Check();
}
void Get(){
string aux;
cin>>aux;
input=(char*)aux.c_str();
cout<<input;}
void Check(){
for(int i=0;i<(int)strlen(input);i++){
if(input[i]=='a')cout<<"There's an 'a' within the array"<<endl;}
}
#Moschops, I compiled it several times, even added a cout<<whatever, changed the variable names, rebooted my PC, but still doesn't work!
How is this possible?
Here's the complete code with some cout<<...'s I put to see if anything changed. I am using Visual Studio C++ 2010 Express.
As soon as you exit the Get function, aux is no longer in scope. i.e. its storage is no longer allocated and the char * input pointer is no longer valid.