Thread: Pass values in functions and writing them to a txt file.

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    Pass values in functions and writing them to a txt file.

    I made several comments on this code, hope this helps. This code is for fun, and have it doing a lot of what I want it to, just not all. I want random generated to write to a txt file. I tried to use an array but that failed. I wanted to use an array because i am only passing one value. Which makes sense since the random generated function is an int.

    I made the fprintf as a comment but hopefully soon it will be able to send the values to the txt file. After that I will tackle the function.

    Code:
     #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    #define HIGH 49
    #define LOW 1
    
    int random_generated()
    {
        static int randValue = 1;
        static int randValue2= 0;
        static int numberout;
        
        int j=0,i=0;
        
        srand(time(NULL));
        
        while (i < 100) {
            
        for (j=0; j<6; j++) {       // loops 6 times resulting in the while loop producing 6 numbers
                                    // that were produced at the sametime
            do{
                randValue= (rand() %(HIGH-LOW+1))+LOW;          //generates random values
                randValue2= (rand() %(HIGH-LOW+1))+LOW;         //generates random values
            }
            
            while (randValue!=randValue2);
            
            randValue = numberout;
                return numberout; // will call this in datafile() and print its output to a .txt
        }                           //sends one value but I want 6, do I need an Array?
                                    // tried to use one but it failed, might have used it wrong
            i++;
      }
    }
    
    void datafile()
    {
        char file_name[81];
        char *p;
        int write_to_file;
        int i;
        FILE *fp;
        
        printf("Enter a file name:");
        fgets(file_name,sizeof(file_name),stdin);
        p=strchr(file_name, '\n');
        if ((p)) {
            *p='\0';
        }
        
        fp =fopen(file_name,"r+");
        if(fp==NULL)
        {
            printf("The file was not successfully opened\n");
            printf("Please check if the file exists\n");
            exit(1);
        }
        
        write_to_file = random_generated();
        
        printf("%d",write_to_file);
        
        //fprintf(fp, write_to_file); // this will hopefully soon write to a txt file with the output from
        //random_generated...
        
        fclose(fp);
    }
    
    int main(int argc, const char * argv[])
    {
        
        datafile();
        
        random_generated();
        
        return 0;
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    I would think - starting with fixing all warnings would be a good beginning

    Code:
    1>test.c(9): warning C4255: 'random_generated' : no function prototype given: converting '()' to '(void)'
    1>test.c(17): warning C4242: 'function' : conversion from 'time_t' to 'unsigned int', possible loss of data
    1>test.c(38): warning C4255: 'datafile' : no function prototype given: converting '()' to '(void)'
    1>test.c(43): warning C4101: 'i' : unreferenced local variable
    1>test.c(71): warning C4100: 'argv' : unreferenced formal parameter
    1>test.c(71): warning C4100: 'argc' : unreferenced formal parameter
    1>test.c(21): warning C4702: unreachable code
    1>test.c(36): warning C4715: 'random_generated' : not all control paths return a value
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    What IDE are using? I am using Xcode 4 and get non of the above...

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    Check your waring level - the above warnings are from VS2012 Express on W4 level

    Here is output from gcc

    Code:
    gcc -c -o obj/test.o src/test.c -Wall -pedantic -march=core2 -Iinclude
    src/test.c: In function ‘random_generated’:
    src/test.c:21:33: warning: C++ style comments are not allowed in ISO C90 [enabled by default]
    src/test.c:21:33: warning: (this will be reported only once per input file) [enabled by default]
    src/test.c: In function ‘datafile’:
    src/test.c:43:9: warning: unused variable ‘i’ [-Wunused-variable]
    src/test.c: In function ‘random_generated’:
    src/test.c:36:1: warning: control reaches end of non-void function [-Wreturn-type]
    So the most critical problem in line 36 of the file is reported by both compilers
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    OK!! Now we are getting somewhere. On lines 17, 36 and 43 I am getting warnings. Line 36 wants me to remove it, however I get warnings if I do and I making the function a void it resulting in an error because I am returning a value.

    I have a plan for line 36, and line 17 was explained to me and should be ignored. I code use some objective-c to make it go way.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    What exactly do you think the inner do/while loop in random_generated is doing? What is it's purpose?
    Also, you're returning numberout which has never been initialized. I assume you want randValue = numberout to be the other way around.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    I think the do loop is generating random values twice and passing them to two different variables. The while loop keeps going while randValue is not equal to randValue2 and return an int. I want 6 ints returned so I might need an array. I was running this program differently and had random_generated print 6 number with in the function but they were not being passed to next function. Which is why I changed it to its current code.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    When you return from a loop, the function shall end. There is no such thing as returning a value six times.

    I recommend modifying a data structure of some kind:
    Code:
    void random_generator(int hi, int low, int dice[6])
    {
      int i;
      if (dice == NULL)
         return;
      for (i = 0; i < 6; ++i)
           dice[i] = low + rand()%(hi - low);
    }
    Last edited by whiteflags; 08-24-2013 at 01:50 PM.

  9. #9
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    What do these warning mean. The more fun projects I do the more I find out I do not know. @whiteflags, thank you for the advice. I didn't you could use return the way you did.

    Why am I getting all these warnings when I simply call random_generator into data_files using an array. I want to fprintf the array to the txt file. I called it in main and the array was working fine. Something is not clicking over here.

    Code:
     #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define HIGH 49
    #define LOW 1
    
    void number_generator(numbers[6]) //warning specifier missing, defaults to 'int'
    {
        int i;
        srand(time(NULL));
        
        for (i=0; i<6; i++) {
            
    numbers[i]= LOW + rand()%(HIGH - LOW); //this works
            
    }
    }
    
    void datafile()
    {
        char file_name[81];
        char *p;
        int receiveNumber[6];
        int i;
        FILE *fp;
        
        printf("Enter a file name:");
        fgets(file_name,sizeof(file_name),stdin);
        p=strchr(file_name, '\n');
        if ((p)) {
            *p='\0';
        }
        
        fp =fopen(file_name,"r+");
        if(fp==NULL)
        {
            printf("The file was not successfully opened\n");
            printf("Please check if the file exists\n");
            exit(1);
        }
        
       number_generator(receiveNumber);
        
        for (i=0; i<6; i++) {
        
        fprintf(fp,receiveNumber[i]); //format string is not a string literal (potentially insecure)
                                        //incompatable integer to pointer conversion passing 'int' to parameter of type of const char *
            
        }
        
        fclose(fp);
    }
    
    int main(int argc, const char * argv[])
    {
        int receiveNumber[6];
        int i;
        
        datafile();
        
        number_generator(receiveNumber);
        
        for (i=0; i<6; i++) {
        
        printf("%d\n",receiveNumber[i]); //this is just confirm values are being passed
            
        }
        return 0;
    }

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    void number_generator(numbers[6]) //warning specifier missing, defaults to 'int'
    as the error says there is no type specifier
    try
    Code:
    void number_generator(int numbers[6])
    Code:
    fprintf(fp,receiveNumber[i]);
    Where is the format string ?
    Kurt

  11. #11
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    fprintf, puts and gets both require a char. I just want to pass an int array. I have only passed variable to a data file with strings. I suppose that is my hang up.

  12. #12
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    OMG I feel like a retard!!!!!

    Disregard, thank you for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-14-2012, 07:45 PM
  2. How to pass enum values
    By Bargi in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2007, 02:55 AM
  3. Writing to single file in multiple functions
    By alvision in forum C Programming
    Replies: 12
    Last Post: 08-22-2004, 08:15 PM
  4. howto pass values to & from
    By merlyn2000 in forum C Programming
    Replies: 6
    Last Post: 06-13-2002, 06:31 PM