-1

So I suck with functions and need to debug this. Im pretty sure the function ToPigLating does its job well at converting. However I just need help calling the function ToPigLatin inside of my main function. But when I try doing that I just get a bunch of error codes.

#include <stdlib.h>
#include <string.h>

#define LEN 32

char* ToPigLatin(char* word[LEN]){
    char word[LEN];
    char translation [LEN];
    char temp [LEN];
    
    int i, j;
    
    
    while ((scanf ("%s", word)) != '\0') {
        strcpy (translation, word);
        
        //just pretend I have all the work to convert it in here. 
    } // while
}

int main(){
    printf("Enter 5 words: ");
    scanf("%s", word);
    ToPigLatin();
    
    
}```
  • Please take the time to read the [help] pages. The titles needs to be relevant to your question, i.e. other people searching for the same problem must find this question by its title. "I need help" is not an acceptable question title here. "I just get a bunch of error codes." Please post the errors (copy/paste) – bolov Apr 15 '21 at 03:24
  • 1
    You don't pass `word` as a parameter when calling `ToPigLatin`. In fact, you don't even define `word` before using it. (Also, I think you want `char *word` or `char word[LEN]` as the parameter, not `char *word[LEN]`, which is basically an array of strings.) – mediocrevegetable1 Apr 15 '21 at 03:33
  • 1
    im trying to understand what youre saying but im not sure if im following. i removed the ```scanf ``` in ```main``` and am getting an error saying "too few arguments to function 'ToPigLatin'. Not sure what to do really – icecoldash Apr 15 '21 at 03:50
  • 1
    As I said in my previous comment, by writing `char* ToPigLatin(char* word[LEN]){` you expect an array of strings as an argument to be passed into the function in the brackets when you call it, but you leave that empty. I'd suggest you take another look at whatever tutorial or book you're using about what function parameters/arguments are. – mediocrevegetable1 Apr 15 '21 at 03:53
  • 1
    You should get a large number of error messages for this code . If not then adjust your compiler settings. – M.M Apr 15 '21 at 04:15

4 Answers4

1

Roughly, variables only exist within the function they're declared in. The word in ToPigLatin exists only within ToPigLatin. It is not available in main. This lets us write functions without worrying about all the rest of the code.

You need to declare a different variable in main, it can also be called word, to store the input and then pass that into ToPigLatin.

Let's illustrate with something simpler, a function which doubles its input.

int times_two(int number) {
  return number * 2;
}

We need to give times_two a number.

int main() {
  // This is different from "number" in times_two.
  int number = 42;

  // We have to pass its value into time_two.
  int doubled = times_two(number);

  printf("%d doubled is %d\n", number, doubled);
}

Your case is a bit more complicated because you're working with input and memory allocation and arrays. I'd suggest just focusing on arrays and function calls for now. No scanf. No strcpy.

For example, here's a function to print an array of words.

#include <stdio.h>

// Arrays in C don't store their size, the size must be given.
void printWords(const char *words[], size_t num_words) {
    for( int i = 0; i < num_words; i++ ) {
        printf("word[%d] is %s.\n", i, words[i]);
    }
}

int main(){
    // This "words" variable is distinct from the one in printWords.
    const char *words[] = {"up", "down", "left", "right"};
    
    // It must be passed into printWords along with its size.
    printWords(words, 4);
}
Schwern
  • 153,029
  • 25
  • 195
  • 336
0

ToPigLatingToPigLating function expects to have a parameter like ToPigLating("MyParameter");

lewis machilika
  • 819
  • 2
  • 11
  • 25
  • `"MyParameter"`'s type is `char *` but the function expects `char *[]` or `char **`. I suspect OP is not actually even using the parameter since they define another variable in their function with the same name as the parameter, so if that's true the parameter seems useless anyway and the function definition should be changed to require no arguments. – mediocrevegetable1 Apr 15 '21 at 03:56
0

Hello there icecolddash.

First things first, there are some concepts missing. In main section:

scanf("%s", word);

You're probably trying to read a string format and store in word variable. In this case, you should have it on your declaration scope. After some adjustment, it will look like this:

int main(){
char word[LEN];

As you defined LEN with 32 bytes maximum, your program will not be allowed to read bigger strings. You're also using standard input and output funcitions as printf, and so you should ever include stdio.h, thats the header which cointains those prototypes already declared, avoiding 'implicit declaration' compiling warnings.

Next issue is how you're declaring your translation function, so we have to think about it:

char* ToPigLatin(char* word[LEN])

In this case, what you wrote: ToPigLatin is a funcion that returns a char pointer, which means you want your function to probably return a string. If it makes sense to you, no problem at all. Although we got some real problem with the parameter char* word[LEN].

Declaring your variable like this, assume that you're passing an array of strings as a parameter. If I got it right, you want to read all five words in main section and translate each one of them. In this case I suggest some changes in main function, for example :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LEN 32
#define MAX_WORDS 5

char *globalname = "translated";

char* ToPigLatin(char* word){
  char *translation = NULL;
  //Translation work here.
  if ( !strcmp(word, "icecolddash") ){
    return NULL;
  }
  
  translation = globalname;

  return translation;
}

int main(){
  char word[LEN];
  char *translatedword;
  int i;
  printf("Enter 5 words: \n");
  for ( i=0; i < MAX_WORDS; i++ ){
    fgets(word, sizeof(word), stdin);
    strtok(word, "\n"); // Just in case you're using a keyboard as input.
    translatedword = ToPigLatin(word);  
    if ( translatedword != NULL ){
      //Do something with your translation
      //I'll just print it out as an example
      printf("%s\n", translatedword);
      continue;
    }
    
    // Generic couldn't translate message
    printf("Sorry, I know nothing about %s\n", word);
    
  }
  return 0;
}

The above code translate every word in a fixed word "translated". In case of reading the exact input "icecolddash", the program will output a generic error message, simulating some problem on translation process.

I hope this help you out with your studies.

rfermi
  • 179
  • 11
-3

There are a few things that I see.

#include <stdio.h>
#include <stdlib.h>

char* ToPigLatin(char* word){
    printf(word);
    return word;
}

int main(){
    printf("Enter 5 words: ");
    
    // declare the variable read into by scanf
    char * word = NULL;
    
    scanf("%s", word);
    
    //Pass the variable into the function
    ToPigLatin(word);
    
    // Make sure you return an int from main()
    return 0;
}

I left some comments in the code with some specific details.

However, the main thing that I would like to call out is the style of the way you're writing your code. Always try to write small, testable chunks and build your way up slowly. Try to get your code to compile. ALWAYS. If you can't run your code, you can't test it to figure out what you need to do.

As for the char ** comment you left on lewis's post, here is some reading you may find useful in building up your intuition:

https://www.tutorialspoint.com/what-does-dereferencing-a-pointer-mean-in-c-cplusplus

Happy coding!

ianarko
  • 402
  • 3
  • 12
  • 1
    You can't input a string into the `word` you created, it'll try to write to `NULL`. You need to use an array. Also, Tutorialspoint... isn't the best site. An example is [Are the "C mock tests" at tutorialspoint correct?](https://stackoverflow.com/questions/62816217/are-the-c-mock-tests-at-tutorialspoint-correct) – mediocrevegetable1 Apr 15 '21 at 04:13
  • You're right. I'm not trying to give you the answer. This feels too much like homework for that. I'm giving you some reminders, like don't forget to declare word. – ianarko Apr 15 '21 at 04:19