[C++] Fatal Errors (Strcpy/Strcat)

First off, here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int answer;
char amount[6]={'\0'}
char letter[10]={'\0'};
if(i==32){
  strcpy(letter, "[Space]");
}
else{
  letter[0]=char(i);
}
	
if(amount[0]=='\0'){

  char message[]=" doesn't have an amount.\nUse Default?";
  char *temp=strcat(letter,message);
			
  answer=MessageBoxA(glbWin.vScrollContent, (LPCSTR)temp, "Error", 
    MB_YESNO|MB_ICONEXCLAMATION|MB_APPLMODAL);
		
  if(answer==IDYES){ //If OK is pressed, then use the default value
    char string[6]={'\0'};
    LoadString(GetModuleHandle(0), j+1000, (LPWSTR)string, 
      sizeof(string)/sizeof(*string));
    strcpy(amount,string);
  }
  else //Else exit the loop.
    return 1; //Return error*/
  }


I have two different errors, depending on whether I press Yes or No.
If I press no, I get:

Microsoft Visual C++ Debug Error

Dubug Error!

Program: [Path here]
Module: [Path here]
File:

Run-Time Check Failure #2 - Stack around the variable 'letter' was corrupted.

(Press Retry to debug the application)


If I press yes, it just flat out crashes.

Can anybody shed some light on why this is happening?

Thanks!
1
2
char message[]=" doesn't have an amount.\nUse Default?";
  char *temp=strcat(letter,message);


This does not work. Letter does not have enough space to store itself + message. Also, temp is pointing at letter now, I hope you realize that (it is not a separate copy). Just use std::strings, they'll make your life a lot easier.
Excuse me... Bangs Head*

For some strange reason, I was thinking that *temp received the combined strings, and since I was getting the correct message... let's just go with I need to bang my head on a C++ book. Maybe it'll knock some knowledge into it. :)

Thanks for the suggestion to use strings, it works quite well.
Topic archived. No new replies allowed.