0

So I got my code to successfully convert english to pig latin, but when I pass the variable back to main{}, I can only somehow return an address location or some hex or char number. I've tried to use different conversion specifiers and everything, but for some reason, I can't get it to output the string. This program reads from an input txt file, computes the translation, then prints to an output txt file. Let me know what you may think is the issue. Thanks!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STR_SIZE 50

char * convertToPigLatin (char * strPtr, char * pLatinStr);

int main(int argc, char *argv[])
{
   char str[MAX_STR_SIZE];
   char pStr[MAX_STR_SIZE];
   char *pStrPtr;
   FILE *fileInPtr;                                     //Create file name
   FILE *fileOutPtr;    

   fileInPtr = fopen("pigLatinIn.txt", "r");    //Assign text to file
   fileOutPtr = fopen("pigLatinOut.txt", "w");

   if(fileInPtr == NULL)                                //Check if file exists
   {
      printf("Failed");
      exit(-1); 
   }
   fprintf(fileOutPtr, "English Word\t\t\t\tPig Latin Word\n", pStr);
   fprintf(fileOutPtr, "---------------\t\t\t\t----------------\n", pStr);

   do                   //Cycles until end of text
   {
      fscanf(fileInPtr, "%29s", str);               //Assigns word to *char

      str[29] = '\0';                           //Optional: Whole line

      pStrPtr = convertToPigLatin(str, pStr); 

      fprintf(fileOutPtr, "%15s\t\t\t\t%15p\n", str, *pStr); 

   }  while(!feof(fileInPtr));   

   system("pause"); 
}

char * convertToPigLatin (const char * strPtr, char * pStrPtr)
{
   int VowelDetect = 0; 
   int LoopCounter = 0; 
   int consonantCounter = 0; 
   char pStr[MAX_STR_SIZE] = {'\0'};
   char cStr[MAX_STR_SIZE] = {'\0'};
   char dStr[] = {'-','\0'}; 
   char ayStr[] = {'a','y','\0'};
   char wayStr[] = {'w','a','y','\0'};

   pStrPtr = pStr; 

   while (*strPtr != '\0')
   {
      if (*strPtr == 'a' || *strPtr == 'e' || *strPtr == 'i' || *strPtr == 'o' || *strPtr == 'u' || VowelDetect ==1)
      {
         strncat(pStr, strPtr, 1); 
         VowelDetect = 1; 
      }
      else
      {
         strncat(cStr, strPtr, 1); 
         consonantCounter++; 
      }
      *strPtr++;
   }
   strcat(pStr, dStr); 
   if (consonantCounter == 0)  
   {
      strcat(pStr, wayStr);
   }
   else
   {
      strcat(cStr,ayStr);
      strcat(pStr, cStr);
   }  
 //printf("%s\n", pStr);                         

   return pStr; 
}
user2525288
  • 87
  • 1
  • 6
  • 1
    Please update your original question http://stackoverflow.com/questions/17847090/issues-with-string-processing-in-c instead of asking new ones. These are unlikely to help future visitors. – jman Jul 25 '13 at 03:34

1 Answers1

5

You're returning the local variable pStr from the convertToPigLatin function. The storage used by this variable is deallocated upon function return, and is no longer valid (and is liable to be overwritten by the next function call) after the function returns. That doesn't really matter, however, because you're not actually using it at any point. In main(), you're doing pStrPtr = convertToPigLatin(str, pStr); to assign to pStrPtr the (now invalid) address of the converted string, but then when you call printf(), you pass it str (the original string) and *pStr (the first character of a char[] array that's never been initialized or used). On top of that, the format specifiers you're passing to printf() are %15s and %15p, so that single character you're passing as the second variable to printf()? You're telling printf() that it's a pointer you want displayed.

Quick list of issues I see from a read-through:

  • The two calls to fprintf() that display the table header each have an unused extra argument (harmless).
  • Pointless str[29] = '\0', fscanf() will have null-terminated the string it got for you, and your buffer is larger than that anyway.
  • In convertToPigLatin(), you immediately discard the passed-in pointer pStrPtr, then never even touch that variable again.
  • Seriously? Using strncat() to copy one byte?
  • Your method of initializing the the string constants dStr, ayStr, and wayStr, while valid, is odd. (Why not just char *dStr="-", *ayStr="ay", *wayStr="way"?
  • You're returning a local automatic variable from convertToPigLatin()
  • you're never using the value returned from convertToPigLatin()
This isn't my real name
  • 4,869
  • 3
  • 17
  • 30
  • What confuses me is that the prototype "char * convertToPigLatin (const char * strPtr, char * pStrPtr);" was given as part of the assignment. But why do I need to both return a value and have a pointer value as a parameter? Don't those two pieces of information give me back the same thing? These two things sorta made me really confused with which pointers were going where. Which you pointed out. – user2525288 Jul 25 '13 at 03:21
  • 1
    You're probably supposed to allocate memory for `pStrPtr` within `main()`, pass that to `convertToPigLatin()`, and have it drop the converted string into there. Sometimes it can be useful to return the same pointer so you can use it in an expression, e.g. `char pStrPtr[100]; puts(convertToPigLatin("nix on the pointer", pStrPtr));` but who knows what your instructor had in mind. – Crowman Jul 25 '13 at 03:26
  • Take a look at the specification of `extern char *strcpy(char restrict *dst, const char restrict *src);` — it takes the space to be copied to as its destination (first) argument, and returns that value. You could, therefore, write: `printf("%s\n", strcpy(dst, src));` and what's in `src` would be copied to `dst` and also printed. You could also get rebuked firmly for presenting such code for review, but it would actually work. There's a good chance that's the general design that was intended. – Jonathan Leffler Jul 25 '13 at 03:31
  • Hm... its a little bit over my head. I think I might just get rid of the function because I can't figure out the pointers. – user2525288 Jul 25 '13 at 03:42
  • I advise against giving up: There are a number of questions/answers on this site that have fairly good explanations of pointers and pointer-handling, ([here](http://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome) and [here](http://stackoverflow.com/questions/4025768/what-do-people-find-difficult-about-c-pointers), for example), so I suggest that you read up on them a bit, and ask questions for anything you still don't understand... – This isn't my real name Jul 25 '13 at 04:19