0

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.

3
Contributors
4
Replies
30
Views
1 Year
Discussion Span
Last Post by Mayukh_1
Featured Replies
  • 3

    When you don't specify the size of an array, the size is inferred from the initializer. So when you write `int arr[] = {1,2,3}`, that's the same as `int arr[3] = {1,2,3}`. Likewise `char formula[] = {'\0'}` is the same as `char formula[1] = {'\0'}` and equivalently `char fomula[] = … Read More

3

When you don't specify the size of an array, the size is inferred from the initializer. So when you write int arr[] = {1,2,3}, that's the same as int arr[3] = {1,2,3}. Likewise char formula[] = {'\0'} is the same as char formula[1] = {'\0'} and equivalently char fomula[] = "" is the same as char formaula[1] = "".

So you're defining an array of size one. You're then writing into that array using cin >> formula. This will write more than one character into the array, meaning you're writing out of bounds. This invokes undefined behavior, possibly overwriting other variables' memory and/or causing a crash.

0

thanks for prompt reply sepp2k. Please tell me, we can assign string to char pointer like :

 char *str = "This is a string.";

but we can't access any character from char pointer with loop like str[0]. str[0] prints the whole string till last character.

How can i apply strrchr() function to find sub-string "OH" from end of string.

1

but we can't access any character from char pointer with loop like str[0]. str[0] prints the whole string till last character.

That's not true. str[0] will give you the character 'T' in your example, str[1] will give you 'h' and so on.

0

for C++ use std::string a instead. And instead of strlen use a.size(), to parse the data from the string,
use a.substr(int index, int noOfBytes)

Edited by Mayukh_1: minor changes

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.