Hello, i am messing around with operations on strings using pointers (i am not sure about translation. I mean char*). I recreated strcpy without problem. It works great, without any bugs, The "form" asking about strings looks like this:
1 2 3 4 5 6 7 8
|
char s1[10], s2[40];
cout << "First string ";
cin.getline(s1,10);
cout << "Second string ";
cin.getline(s2,40);
m_strcpy(s2,s1) // my strcpy, it works great
cout<<"New string: " << s2;
getch();
|
First string First
Second string Second
New string Firstd |
It is actually a function implemented in bigger program, which let me choose (using arrow keys and enter), what operation i want to do at the moment. I can enter this function as many times as i want, it works perfectly. However, there is other function, that works terrible. Firstly, this is how it looks:
1 2 3 4 5 6 7 8 9 10 11
|
char s1[10]={' '}, s2[40]={' '}; //this initializing prevents errors occuring, when new string is longer than second
unsigned bit;
cout << "First string ";
cin.getline(s1,10);
cout << "Second string ";
cin.getline(s2,40);
cout << "From which char should i start? "
cin << bit;
m_strpcpy(s2,ls1,bit) // my strpcpy, it starts copying after (p-1)th char
cout<<"New string: " << s2;
getch();
|
Output after
first running:
First string First
Second string Second
From which char should i start? 3
New string SeFirst |
And then, if i run this function another time (simply by pressing enter once, becouse this option is highlighted in menu after function ends) output looks like:
First string
Second string Whatever
From which char should i start? 20
New string Whatever |
It's like there are leftovers in buffer. This damn "\n" char, which automatically confirm
cin.getline. I'm going to add, that, when i launch first function after launching second, it also crashes. On the other hand, if i launch second, after i had launch first, it works great. I was trying to add
cin.ignore(),
cin.ignore(INT_MAX,'\n') in few different places, but it ain't the way. In described situations, program works normally, so
cin.ignore is not the anwser...