Need help ASAP on outputting a string from a function and accessing it in main!

In the code below, when I try to output out_phrase_char, I get a message that says "The Debugger has exited due to signal 10 (SIGBUS).The Debugger has exited due to signal 10 (SIGBUS)." Please help me as soon as possible. This is due at 11:59pm.

// classes example
#include <iostream>
#include <cstdlib>
using namespace std;

class MyClass {

public:

//
int *getNiCount(const char *phrase_char, const wchar_t *phrase_wchar)
{
int index = 0;
int *num_occurences = new int[2];

while(phrase_char[index] != '\0'){
if(phrase_char[index] == 'N' && phrase_char[index+1] == 'i')
{
num_occurences[0] = num_occurences[0] + 1;
}
index++;
}
index = 0;
while(phrase_wchar[index] != '\0'){
if(phrase_wchar[index] == 'N' && phrase_wchar[index+1] == 'i')
{
num_occurences[1] = num_occurences[1] + 1;
}
index++;
}

return num_occurences;
}

//
//The code btwn the //'s is irrelevant to my problem.


//The function below is supposed to replace the occurences of "Ni" with "NI" in a string of ascii characters and //a string of unicode characters

char *replaceNiWithNI(const char *phrase_char, const wchar_t *phrase_wchar)
{
size_t temp;
char *out_phrase_char = (char*)phrase_char;
char *out_phrase_wchar;
temp = wcstombs(out_phrase_wchar, phrase_wchar, 14);
char *output_phrases = new char[2];
int index = 0;

while(phrase_char[index] != '\0'){
if(phrase_char[index] == 'N' && phrase_char[index+1] == 'i')
{
out_phrase_char[index+1] = 'I';
}
index++;
}
index = 0;
while(phrase_wchar[index] != '\0'){
if(phrase_wchar[index] == 'N' && phrase_wchar[index+1] == 'i')
{
out_phrase_wchar[index+1] = 'I';
}
index++;
}
output_phrases[0] = out_phrase_char;
output_phrases[1] = out_phrase_wchar;

return *out_phrase_char;
}
};


int main () {
MyClass phrase1;
const char *szTestString1 = "Ni nI NI nI Ni";
const wchar_t *szTestString2 = L"Ni nI NI nI Ni";
char *out_phrase_char = new char[2];


//int *num_occurences = phrase1.getNiCount(szTestString1,szTestString2);

// out_phrase_char takes in the edited versions of szTestString1 and
// szTestString2.
char *out_phrase_char = phrase1.replaceNiWithNI(szTestString1,szTestString2);
cout << out_phrase_char[0] << endl;
cout << out_phrase_char[1] << endl;

return 0;
}
Last edited on

1
2
3
4
output_phrases[0] = out_phrase_char;
output_phrases[1] = out_phrase_wchar;

return *out_phrase_char;


those 2 line: convert from char * to char
return: convert char to char *


main function's variable out_phrase_char is redefinition.
Basically, I am trying to create a function that takes in a const char pointer and a const wchar_t pointer. Then I want to manipulate the const char and const wchar_t strings, either by copying them and manipulating them, or by some other method, and then I want this function to return the new manipulated strings. When trying to do that I keep running into problems with casting, and outputting char pointers versus character strings. Any advice, on how to do this correctly would be greatly appreciated.
Topic archived. No new replies allowed.