views:

258

answers:

2

Hi,

I want to add a new (fstream) function in a program that already uses char arrays to process strings. The problem is that the below code yields strings, and the only way i can think of getting this to work would be to have an intermediary function that would copy the strings, char by char, into a new char array, pass these on to the functions in the program, get back the results and then copy the results char by char back into the string.

Surely (hopefully) there must be a better way?

Thanks!

void translateStream(ifstream &input, ostream& cout) {   
  string inputStr;
  string translated;

  getline(input, inputStr, ' ');

  while (!input.eof()) {
    translateWord(inputStr, translated);
    cout << translated;
    getline(input, inputStr, ' ');
  }

    cout << inputStr;

the translateWord func:

void translateWord(char orig[], char pig[]) {
  bool dropCap = false;
  int len = strlen(orig)-1;
  int firstVowel = findFirstVowel(orig);
  char tempStr[len];

  strcpy(pig, orig);


  if (isdigit(orig[0])) return;


  //remember if dropped cap
  if (isupper(orig[0])) dropCap = true;        

  if (firstVowel == -1) {
    strcat(pig, "ay");
    //    return;
  }

  if (isVowel(orig[0], 0, len)) {
    strcat(pig, "way");
    //    return;
  } else {

    splitString(pig,tempStr,firstVowel);

    strcat(tempStr, pig);
    strcat(tempStr, "ay");

    strcpy(pig,tempStr);
  }


  if (dropCap) {
    pig[0] = toupper(pig[0]);
  }

}
A: 

You can use the member function ifstream::getline. It takes a char* buffer as the first parameter, and a size argument as the second.

Charles Salvia
not sure I follow, im already using getline, though this appears to be outputting a string?
v_a_bhatia
There are two getline functions.
anon
Bhatia, the suggestion is that you should use a *different* getline function. Stream objects have their own getline method that fills a character array. Use that instead of the standalone getline function from the string header. That *directly* addresses the question you asked, whereas Neil's answer addresses the underlying problem of your program — if you find yourself needing to convert to an array and then convert back to a string, then maybe you should just use strings exclusively so you can skip all the conversions.
Rob Kennedy
i see what you guys mean, i think the design is in some ways fundamentally flawed - ive been able to sort of get Neil's solution working but i think the pointers might make things slightly easier, i think i'll give that a try too.thank you all for pointing it out!
v_a_bhatia
I'm not sure if this is a homework assignment, (although I'm inclined to think it is given the general usefulness of translating strings into pig latin), but unless you have some kind of imposed constraint that forces you to use C strings here, you can make your life a lot easier by just using `std::string`. As Neil pointed out above, that will significantly simplify and improve your translateWord function
Charles Salvia
+2  A: 

You can pass a string as the first parameter to translateWord by making the first parameter a const char *. Then you call the function with inputStr.c_str() as the first parameter. Do deal with the second (output) parameter though, you need to either completely re-write translateWord to use std::string (the best solution, IMHO), or pass a suitably sized array of char as the second parameter.

Also, what you have posted is not actually C++ - for example:

char tempStr[len];

is not supported by C++ - it is an extension of g++, taken from C99.

anon