As the title suggests, I am having an issue with passing a char array to a function and then working on the passed array. It has been a while since I have used c++ and most of the relevant knowledge has escaped me.
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
[b]char* processNibble(char *nibble)[/b]{
char *modded = new char[6];
//modded[6] = '\n';
if (strcmp(nibble, "0000") == 0){
strcpy(modded, "100000");
cout << modded;
return modded;
}else if (strcmp(nibble, "0001") == 0){
strcpy(modded, "010001");
cout << modded;
return modded;
}else if (strcmp(nibble, "0010") == 0){
strcpy(modded, "110010");
cout << modded;
return modded;
}else if (strcmp(nibble, "0011") == 0){
strcpy(modded, "100011");
cout << modded;
return modded;
}else if (strcmp(nibble, "0100") == 0){
strcpy(modded, "100100");
cout << modded;
return modded;
}else if (strcmp(nibble, "0101") == 0){
strcpy(modded, "010101");
cout << modded;
return modded;
}else if (strcmp(nibble, "0110") == 0){
strcpy(modded, "110110");
cout << modded;
return modded;
}else if (strcmp(nibble, "0111") == 0){
strcpy(modded, "100111");
cout << modded;
return modded;
}else if (strcmp(nibble, "1000") == 0){
strcpy(modded, "101000");
cout << modded;
return modded;
}else if (strcmp(nibble, "1001") == 0){
strcpy(modded, "011001");
cout << modded;
return modded;
}else if (strcmp(nibble, "1010") == 0){
strcpy(modded, "111010");
cout << modded;
return modded;
}else if (strcmp(nibble, "1011") == 0){
strcpy(modded, "101011");
cout << modded;
return modded;
}else if (strcmp(nibble, "1100") == 0){
strcpy(modded, "101100");
cout << modded;
return modded;
}else if (strcmp(nibble, "1101") == 0){
strcpy(modded, "011101");
cout << modded;
return modded;
}else if (strcmp(nibble, "1110") == 0){
strcpy(modded, "111110");
cout << modded;
return modded;
}else if (strcmp(nibble, "1111") == 0){
strcpy(modded, "101111");
cout << modded;
return modded;
}
}
[b]
char* processNibble(char*);[/b]
int main(){
ifstream inFile;
char c;
char ch[4];// = new char[4];
int i = 0;
//ch[5] = '\n';
inFile.open("in.txt");
if (!inFile.good()){
cerr << "Unable to open file! Dunno why" << endl;
return 0;
}else{
while (!inFile.eof()){
inFile >> c;
if (c == '-'){
break;
}else if (c == '0' || c == '1'){
if (i < 4){
ch[i] = c;
i++;
}else if (i == 4){
[b]processNibble(ch);[/b]
i = 0;
ch[i] = c;
i++;
}
}
}
}
}
After the array is passed and I look at its value in a debugger, it looks like only the first character made it to the function. This causes problems later during some compares. I bolded the function, its prototype, and where I am calling it from. This code seems to compile with no problems under VS 2008. Any help in solving this problem would be greatly appreciated!

New Topic/Question
Reply




MultiQuote





|