Hey Guys,
So, I will give a snip of code from one of my much larger programs that I am working on. what I am trying to do is change a global variable with a method that is called. It works and changes that global variable, but for some reason under certain circumstances that variable appears to get changed again without the function being called. I don't understand why. So here it is:
[tag]
Code:
//Love me some global variables
int num;
char *cmds[10];
char *typed, *prompt;
void perror(string s)
{
cout << s << endl;
}
char* promptCNG(char *line)
{
string n;
line = strchr(line, '"');-> Find the first parenthesis set string to start from that point
if(line == NULL)->If there is no parenthesis yell+quit
{
perror("Use the right syntax!\n");
return NULL;
}
else
{
line = line+1;->cut the initial parenthesis found
line[strlen(line)-1] = NULL;->cut last parenthesis of the pair
n = line;->strings are easier to append
n = n + ": "; return (char*)n.c_str();->convert back to char* and return it
}
}
int main()
{
prompt = "I will do your bidding master: ";
while(1 == 1)
{
typed = NULL;
typed = readline(prompt);
add_history(typed);
if(typed[0] == 'P' && typed[1] == 'S' && typed[2] == '1')
prompt = promptCNG(typed);
else
cout << endl;
}
return 0;
}
[/tag]
What I would expect to see happen is my prompt will pop up and wait for input. If my input starts with the characters "PS1" then it should call my function to change my prompt which should then change my global variable and then forever more my prompt should be whatever was returned from my promptCNG function.
And, for the most part is works except for if I do PS1 = " " which would make my prompt a spot of white space followed by the colon ( : ). It will change my prompt successfully to that but then the next time my mains while loop comes round it gets changed again without the function call and I have no prompt at all? I don't understand how that is happening. Any suggestions?