0

I want to generate two random chars (Or a string of two random chars), concatenate them into a string and store it in a 2D array.

Does anyone have any idea why this is not working? (I know the code isn't particularly good but I guess you have to start somewhere). Thanks.

int TimeTracking(void) { // Function with the purpose of counting how many 
    // times it has been invoked and return it
    static unsigned int call_count = -1;
    call_count++;
    return call_count;
}

int PrinterFunction (void) {
    int LocalCallCount = TimeTracking();
    char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int randomvar = 0;
    fileObject = fopen("Default.txt", "a");

    char MasterArray[1440][10];


    char AircraftRandomLetter1[10]; // Store random letter
    char AircraftRandomLetter2[10]; // Store second random letter


    randomvar = rand() % 26; // Gen random num for gen random letter
    AircraftRandomLetter1[LocalCallCount] = alphabet[randomvar]; // Access 
    // Violation here???
    randomvar = rand() % 26; // Gen random num for gen random letter
    AircraftRandomLetter2[LocalCallCount] = alphabet[randomvar];


    strcat(AircraftRandomLetter1[LocalCallCount], 
    AircraftRandomLetter2[LocalCallCount]);


    MasterArray[LocalCallCount] = AircraftRandomLetter1[LocalCallCount];  


    fputs(AircraftRegisterRequestingTakeOffIdentifer[LocalCallCount],
    fileObject);
    // Print result to file

    fputs("\n", fileObject);
    fclose(fileObject);
    return 0;
}

In reply to chux, is this somewhat in the right direction? :

randomvar = rand() % 26;
strncpy(AircraftRandomLetter1[LocalCallCount], alphabet, randomvar);
AircraftRandomLetter1[LocalCallCount + 1] = '\0';
randomvar = rand() % 26;
strncpy(AircraftRandomLetter2[LocalCallCount], alphabet, randomvar);
AircraftRandomLetter1[LocalCallCount + 1] = '\0';
strncat(AircraftRandomLetter2[LocalCallCount], 
AircraftRandomLetter1[LocalCallCount], sizeof(AircraftRandomLetter1));


fputs(AircraftRegisterRequestingTakeOffIdentifer[LocalCallCount], 
fileObject);
// Print result to file
  • Please indent your code properly and check the comments – Deepesh Choudhary Apr 25 '17 at 16:17
  • Yeah that was a mistake with pasting it into stackoverflow – Johnny Balboa Apr 25 '17 at 16:18
  • 1
    This isn't working because the code doesn't compile, much less run. I think you need to review how character arrays and the `string.h` library work together. – WhozCraig Apr 25 '17 at 16:20
  • @WhozCraig I literally have no idea – Johnny Balboa Apr 25 '17 at 16:22
  • "Does anyone have any idea why this is not working?" Certainly your compiler gave warnings. Add them to the post. – chux - Reinstate Monica Apr 25 '17 at 16:22
  • @CreamofSomYungGuy I suspected. That's why i said you need to review your materials. if you don't have a good book in C, [get one](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – WhozCraig Apr 25 '17 at 16:24
  • @chux I updated the comments to show where the error occured – Johnny Balboa Apr 25 '17 at 16:36
  • `strcat(AircraftRandomLetter1[LocalCallCount], ...` expect pointers to 2 _strings_. Instead, it is given pointers to 2 `char` arrays that may lack the needed null character - which would make them a _string_. Code needs to insure the arrays each have a _null character_. ` MasterArray[LocalCallCount] = AircraftRandomLetter1[LocalCallCount];` is insufficient too. GTG – chux - Reinstate Monica Apr 25 '17 at 16:41
  • @chux I updated the post, I'm not quite sure what to do I'm still getting errors, I think I understand the docs, must be getting something wrong though clearly – Johnny Balboa Apr 25 '17 at 17:23
  • If all you're trying to do is append a two-character sequence randomly selected from the 26 upper-case characters to a text file, followed by a newline, [you're making this *much* harder than needed](https://pastebin.com/PhaETKSR). In fact, you need no character array *at all*. [It can be done without one](https://pastebin.com/A9vNV4Lv). – WhozCraig Apr 25 '17 at 18:30
  • Your call counter is off-by-one; after the first call, it says 'zero calls'. Initialize to 0, not -1. – Jonathan Leffler Apr 25 '17 at 18:51

1 Answers1

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

int TimeTracking(void);
int PrinterFunction (void);

int main() {

  PrinterFunction ();

  return EXIT_SUCCESS;
}

int TimeTracking(void) {
  static unsigned int call_count = 0; // At first it has never been called
  call_count++;

  return call_count;
}

int PrinterFunction (void) {
  int LocalCallCount = TimeTracking();
  int randomvar = 0;

  short i;

  char MasterArray[1440][10];
  /*
    +1 because we have to reserve a cell for «\0».
    The arrays must be initialized in order to have «\0» so we will
    not have to deal with it later.

    Find out more about strings here https://www.tutorialspoint.com/cprogramming/c_strings.htm
  */
  char AircraftRandomLetter1[11] = {0};
  char AircraftRandomLetter2[11] = {0};

  /*
     You did not specify a type...
     Just in case : https://linux.die.net/man/3/fopen
  */
  FILE * fileObject = fopen("Default.txt", "a");

  /*****
        I want to generate two random chars (Or a string of two random chars),
        concatenate them into a string and store it in a 2D array.
  *****/

  /*
    You have to specify a «seed» or else you will generat the same pseudo-random
    number.
    RTM : https://linux.die.net/man/3/srand
  */
  srand(time(NULL)); // You only need to call it once.
  for (i = 0; i < 10; ++i) { // Genarate 10 random numbers
    randomvar = rand() % 26;
    AircraftRandomLetter1[i] = 'A' + randomvar;

    randomvar = rand() % 26;
    AircraftRandomLetter2[i] = 'A' + randomvar;
  }

  /*
    Add the randomly ganerated string to MasterArray at position LocalCallCount.

    I'm not sure that's what you intend to do...
    If that's not the case you should be able to take it from here :)
   */
  strcat(MasterArray[LocalCallCount], AircraftRandomLetter1);
  strcat(MasterArray[LocalCallCount], AircraftRandomLetter2);
  printf("%s", MasterArray[LocalCallCount]);

  /*
    Write the results...

    Read about fprintf here -> https://linux.die.net/man/3/fprintf
  */
  fprintf(fileObject, "%s\n", MasterArray[LocalCallCount]);

  fclose(fileObject);

  return EXIT_SUCCESS;
}
Remy J
  • 709
  • 1
  • 7
  • 18
  • `char AircraftRandomLetter1[11] = ""` is equivalent initialization, (and simply `0` is equivalent to `'\0'`.), so using the *Universal Initializer* `{0}` works as well -- your choice. – David C. Rankin Apr 26 '17 at 02:17
  • Thanks, that I did not know. I'll update my answer, I'll go with `{0}`. – Remy J Apr 26 '17 at 02:24
  • Please excuse me if I'm bordering but I never came across this before. May I ask where did you encounter it maybe was it in a book? If so which one as it. – Remy J Apr 26 '17 at 03:50
  • It is how initialization works in the C standard. When initializing any object, the elements (beginning with the first) are explicitly initialized to the *initializer list* specified, all additional elements not explicitly initialized are set to `0` or `null` (depending on type) [**C11 Standard (draft n1570) - Sec. 6.7.9 (10)**](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) When you initialize a *character array* with the `""` initializer (empty string), the first element is the *nul-terminating* character (all others are set to `0`), `{0}` explicitly sets the first to `0`. – David C. Rankin Apr 26 '17 at 06:29
  • To me there was a difference between an array of integers and an array of characters (strings) during initialization. I've always made that distinction up to now thanks for pointing that out. Thanks again and have a good day/night. – Remy J Apr 26 '17 at 06:37