Hi everyone,
My program is crashing on taking input of alpha-numeric and store it in char[]. I don't know why, please anyone help !
sample input : C9H8O4MgOH
#include <iostream>
#include <string.h>
using namespace std;
bool hydroxide(char[]);
int main()
{
char formula[] = "";
cout<<"Enter chemical formula : ";
cin >> formula;
if(hydroxide(formula))
{
cout<<"\nFormula ends with sub-string OH"<<endl;
}
else
{
cout<<"\nFormula does not ends with sub-string OH"<<endl;
}
cout<<"\nProgram terminated !"<<endl;
return 0;
}
bool hydroxide(char formula[])
{
char *ptr1, *ptr2;
ptr1 = strrchr(formula, 'O');
ptr2 = strrchr(formula, 'H');
cout<<"O index : "<<ptr1-formula <<endl;
cout<<"H index : "<<ptr2-formula <<endl;
//second last character is O and last character is H it returns true
if(ptr1-formula == strlen(formula)-2 && ptr2-formula == strlen(formula)-1)
{
return true;
}
return false;
} //end of hydroxide()
Everything works fine, but at the end program crashes and shows message that hyroxide.exe has stopped working.