help with strcpy()
Oct 2, 2012 at 3:02pm UTC
Hi, i am trying to copy a peice of string into a string array for a project but it seems that something is wrong with strcpy() when i use it with an array.
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 28 29 30
#include <iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
char messages[100][10] = {"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" };
void insertMessage(char * mes)
{
for (int i = 9; i > 0; i--)
{
strcpy( ::messages[i], ::messages[i-1]);
}
strcpy( ::messages[0],mes);
}
int main()
{
system("cls" );
insertMessage("saasasasasasasasasasasasasasasa" );
for (int k = 0; k < 10 ; k++ )
cout << ::messages[k] << endl;
cin.get();
return 0;
}
saasasasasasasasasasasasasasasa
asasasasasasasasasasa
asasasasasa
a
I am only allowed to use C type strings for the project. Thanks for the help.
Oct 2, 2012 at 3:09pm UTC
You have your array of ten items specified backwards. It should be char messages[10][100] = {...};
.
Good luck!
Oct 3, 2012 at 12:27pm UTC
thanks!! that helped. I must have been really very stupid.
Oct 3, 2012 at 3:07pm UTC
Only for the quarter-second it took you to mistype that one spot. It happens to everyone.
Topic archived. No new replies allowed.