int isaMUstring(char* sequence, int size)                         //function isaMUstring(char* sequence, int size)        
{
    int i,j;
    for(i=0; i<size; i++)
    {
        if(sequence[i]=='M'||sequence[i]=='U'||sequence[i]=='I')
        j=1;
        else 
        j=0;
    }
        return j;
}

int canApplyRule1(char* sequence, int size)                      //function canApplyRule1(char* sequence, int size) 
{
    if(sequence[size-1]=='I')
    return 1;
    else 
    return 0;
}
int canApplyRule2(char* sequence, int size)                      //function canApplyRule2(char* sequence, int size) 
{
    if(sequence[0]=='M')
    return 1;
    else 
    return 0;
}


int main()                                                    
{
    int i,size,num;
    char*sequence;
    string a;
    cout<<"Enter the size you want:"<<endl;                  //ask for a size of the sequence
    cin>>size;
    sequence=new char[size];
ENTER:
    cout<<"Please enter the sequence:"<<endl;                //ask for the sequence
    for(i=0; i<size; i++)
    {
        cin>>sequence[i];
    }
    if(!isaMUstring(sequence,size) || i > size )             //test if it is a MU string
    {
        cout<<"Invaild sequence"<<endl;
        goto ENTER;
    }
    else                                                     //test whether Rule1 and Rule2 can be applied to it and notify the user
    {
        if(canApplyRule1(sequence,size))
        cout<<"Can apply to rule1"<<endl;
        if(canApplyRule2(sequence,size))
        cout<<"Can apply to rule2"<<endl;
        if(!canApplyRule1(sequence,size)&&!canApplyRule2(sequence,size))
        cout<<"Both do not apply"<<endl;
    }
    if(canApplyRule1(sequence,size)||canApplyRule2(sequence,size))  //ask the user to choose a rule to apply 
    {
        cout<<"Please choose the rule which can apply"<<endl;
        cin>>num;
        if(num==1)                                         
        {
            char* arr=sequence[i];
            int size=strlen(arr)+1;
            char* old_ptr=arr;
            arr=new char[size];
            strcpy(arr,old_ptr);
            strcat(arr, "U");
            cout<<arr;
            delete[] arr;
                                                          //print out the new sequence
        }
        if(num==2)
        {
            char*a=new char[size-1];
            for(i=0;i<=size-1;i++)
            {
                a[i]=sequence[i+1];
            }
            for(i=0;i<=size;i++)
            {
                cout<<sequence[i];
            }
            for(i=0;i<=size-1;i++)
            {
                cout<<a[i];                                        //print out the new sequence
            }
        cout<<endl;
        delete []a;
        }
    }
    delete []sequence;
    return 0;
}

this is a MU game, however there is an error when i try to compile it,
error C2440: 'initializing' : cannot convert from 'char' to 'char *'

the output is supposed to be when the user input a sequence ending with I the programs adds a U. However, the program must del the old sequence & generate a new sequence with a +1 size.
I cant seem to figure out whats wrong.
Hope someone could give me soem tips

Recommended Answers

All 6 Replies

There problem is exactly as stated. sequence is a pointer to char (a dynamic array), so sequence[i] produces a single character, not a pointer to a character. I can't say how to fix it because I'm not sure what you're trying to accomplish with that line, but on the assumption that you're trying to get the length of the substring starting at i, you can do this:

char* arr = &sequence[i];

Taking the address of the character produced by sequence[i] will give you a slice of the string. Alternatively you can use this syntax if it's more intuitive:

char* arr = sequence + i;

basically im trying to store the user's input sequence of characters into a dynamic array, e.g MUI.
then increasing the size by 1
and output a new sequence with U, e.g MUIU

I'd recommend dropping dynamic arrays entirely and letting the string class do the heavy lifting. It'll save you all kinds of complexity and potential bugs:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string input;

    cout << "Enter a string: ";

    if (getline(cin, input))
    {
        cout << "Before: " << input << endl;

        // Append 'U'
        input += 'U';

        cout << "After: " << input << endl;
    }
}

Thanks for the recommendation, but unfortunately, this assignment must be done with dynamic array. If i could I would definately switch.

There problem is exactly as stated. sequence is a pointer to char (a dynamic array), so sequence[i] produces a single character, not a pointer to a character. I can't say how to fix it because I'm not sure what you're trying to accomplish with that line, but on the assumption that you're trying to get the length of the substring starting at i, you can do this:
char* arr = &sequence[i];
Taking the address of the character produced by sequence[i] will give you a slice of the string. Alternatively you can use this syntax if it's more intuitive:

char* arr = sequence + i;

I managed to get the program to run, However, whenever i select 1, the output is weird.

Probably because the arr array points to the same array as sequence, and there is no null value at the end of the intended sequence array? That is probably why it outputs beyond the intended sequence, including gibberish until it finds a null value. Just my two cents.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.