I've been making exercises as functions and some interface for them. Currently I've made just one exercise which is to erase the first occurrence of line 'we' in some bigger line. There's single guy with same problem and similar conditions (strcpy causing Thread 1: signal SIGABRT), but I get the fault not when function strcpy is used, but after, when we return to switch-case construction, though this is must be connected.
#include <iostream>
using namespace std;
void task1_C() {
const int N = 10;
char S[N];
char rem[3] = "we";
cout << "Input your line (should consist of 10 characters): ";
cin >> S;
char *p = strstr(S, rem);
if (p) {
strcpy(p, p + strlen(rem));
cout << S << endl;
}
}
void task1_Cpp() {
}
void task2_C() {
}
void task2_Cpp() {
}
void task3_C() {
}
void task3_Cpp() {
}
void task4_Cpp() {
}
void task5_Cpp() {
}
int main(){
int input;
cout << "Input 1-5 for C++ functions, 11-13 for C functions." << endl;
cin >> input;
switch(input){
case 1: task1_Cpp(); break;
case 2: task2_Cpp(); break;
case 3: task3_Cpp(); break;
case 4: task4_Cpp(); break;
case 5: task5_Cpp(); break;
case 11: task1_C(); break;
case 12: task2_C(); break;
case 13: task3_C(); break;
}
return 0;
}
Output:
Input 1-5 for C++ functions, 11-13 for C functions.
11
Input your line (should consist of 10 characters): sweweratwe
sweratwe
Program ended with exit code: 9