Thread: having trouble writing a program that generates source code

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    150

    having trouble writing a program that generates source code

    Code:
    #include<stdio.h>
    int main()
    {
        unsigned long long counter1 =18446744073709551615;
        char output[8]={0,0,0,0,0,0,0,0};
        FILE *fp;
             fp=fopen("d:\\crypt.h", "w");
            unsigned long long counter2 =counter1;
            while (counter2 !=0);
            {
                if (counter2&1);
                fprintf(fp,"if (counter2<=counter1);");
                fprintf(fp,"%s",output);
                (counter2=counter2-2);
                (output+1);
                printf("%s",output);
                printf("%llu",counter2);
                }
             return 0;
    }
    it isn't printing any output to the screen or the file.
    It is supposed to generate an if statement using the odd numbers backwards and transposing their values into the char array I have forwards.
    Thanks
    Last edited by Once-ler; 01-03-2014 at 09:26 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Do you even have a compiler?
    C:\Users\Josh2>gcc once-ler.c -ansi -pedantic -Wall -o once-ler.exe
    once-ler.c: In function 'main':
    once-ler.c:4:19: warning: ISO C90 does not support 'long long' [-Wlong-long]
    once-ler.c:4:34: warning: integer constant is so large that it is unsigned [enab
    led by default]
    once-ler.c:4:5: warning: this decimal constant is unsigned only in ISO C90 [enab
    led by default]
    once-ler.c:4:5: warning: integer constant is too large for 'long' type [-Wlong-long]
    once-ler.c:8:23: warning: ISO C90 does not support 'long long' [-Wlong-long]
    once-ler.c:8:9: warning: ISO C90 forbids mixed declarations and code [-pedantic]

    once-ler.c:14:13: warning: statement with no effect [-Wunused-value]
    Start with these.

    Code:
     FILE *fp;
             fp=fopen("d:\\crypt.h", "w");
    Always check the file handle:
    Code:
    if (fp == NULL) {
      perror("d:\\crypt.h");
      return -1;
    }
    Code:
    fprintf(fp,"%s",output);
    I doubt output is going to be a zero-terminated C-string. Crypto rarely results in human readable text, too, so you might as well fwrite it to a binary file.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    I am using mingw with code::blocks.
    The code compiles without complaining but it does nothing when I run it except show the console.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm also using mingw. Better fix those warnings then.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    the only warnings I am getting are:
    C:\Users\Ben\Documents\newheathe3.c||In function 'main':|
    C:\Users\Ben\Documents\newheathe3.c|4|warning: integer constant is so large that it is unsigned [enabled by default]|
    C:\Users\Ben\Documents\newheathe3.c|4|warning: this decimal constant is unsigned only in ISO C90 [enabled by default]|
    ||=== Build finished: 0 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    @Once-ler:
    Crank up your warning level all the way. Read the compiler documentation, and take a close look at the compile command in whiteflag's post #2. I wonder what -Wall does?


    Next, get the auto-indent feature of your editor working correctly. Then, auto-indent your code. And notice how the following doesn't line up they way you would expect:
    Code:
    if (counter2&1);
    fprintf(fp,"if (counter2<=counter1);");
    fprintf(fp,"%s",output);
    (counter2=counter2-2);
    Why aren't any of the fprintf or following statements indented as part of the if?
    I also recommend using explicit curly braces for if/else and loops, even if there's only one statement you want in there. It helps avoid silly mistakes.
    Then, tell me what you think if (counter2&1) is checking. If that's what it checks, what is the point of (counter2=counter2-2)?
    Tell me, what is the point of this statement: (output+1);?
    Also, tell me why the if statement doesn't match the if statement in the fprintf right below it.


    @whiteflags:
    > I doubt output is going to be a zero-terminated C-string.
    Either Once-ler edited the code on you, or you totally missed the intializer.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    your right I don't need counter2&1. output+1 is supposed to be incrementing the char array by one.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    Code:
    #include<stdio.h>
    int main()
    {
        unsigned long long counter1 =18446744073709551615;
        char output[8]={0,0,0,0,0,0,0,1};
        FILE *fp;
             fp=fopen("d:\\crypt.h", "w");
            unsigned long long counter2 =counter1;
            int counter3=0;
            while (counter2 !=0)
            {
                if counter3==200;
             {
                if (counter2<=counter1)
                {
                  fprintf(fp,"if (counter2<=STUCK);
                fprintf(fp,"if counter2<=counter1%s\n",output);
                (counter2=counter2-2);
                (output+1);
                printf("%s\n",output);
                printf("%llu\n",counter2);
                counter3++;
                {
                 fprintf(fp,"if (counter2<=STUCK)
                }
                }}
             return 0;
    }
    as you can see I am stuck.
    I am trying to make it so that every time that counter3==200 it will output in the file an if statement, the part I am hung on is how to make it put out as if statement every 200 passes and contain the other if's in it and have it reset back to 0 after it reaches 200 and keep it going.
    I also can't figure out why the output text file is showing "if counter2<=counter1" and that's all it shows over and over. How do I fix this? It is suppose to transpose all the odd numbers converting them into the char array and fprint them into the file to serve as a test condition.
    As for C books I have been reading them, I've read three so far but still am having trouble with it.
    Thanks

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    @whiteflags:
    > I doubt output is going to be a zero-terminated C-string.
    Either Once-ler edited the code on you, or you totally missed the intializer.
    He literally changes what output looks like every time he posts. Right now it's:
    Code:
    char output[8]={0,0,0,0,0,0,0,1};

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    This warning is pointing out a quite critical error with regards to your program's correctness: "integer constant is so large that it is unsigned"
    It means that counter1 does not get given the value you are trying to give it.
    The fix is to create a literal of the right type, by adding ULL onto the end:
    Code:
    unsigned long long counter1 = 18446744073709551615ULL;
    Please fix your indentation and formatting. E.g. It is never acceptable to have two closing brackets on the same line.

    Why do you have statements that do nothing such as?:
    Code:
    (output+1);
    Last edited by iMalc; 01-04-2014 at 01:58 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    Thank you.
    output +1 is supposed to advance to the next char in output it should be output=output+1
    on the line right before it ,output is printed or supposed to be
    so after it prints output then output plus one for the next step

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If you don't know how to write your own source code, you can't hope to write source code for a program that generates source code.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  13. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    I changed it
    Code:
    #include<stdio.h>
    int main()
    {
        unsigned long long counter1 =18446744073709551615ULL;
        char output[8]={0,0,0,0,0,0,0,1};
        FILE *fp;
             fp=fopen("d:\\crypt.h", "w");
            unsigned long long counter2 =counter1;
            int counter3=0;
            while (counter2 !=0)
            {
                if (counter2<=counter1)
                {
                fprintf(fp,"if counter2<=counter1%s\n",output);
                (counter2=counter2-2);
                (output=output+1);
                printf("%s\n",output);
                printf("%llu\n",counter2);
                counter3++;
                }}
             return 0;
    }
    C:\Users\Ben\Documents\bestyet.c||In function 'main':|
    C:\Users\Ben\Documents\bestyet.c|16|error: incompatible types when assigning to type 'char[8]' from type 'char *'|
    ||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

  14. #14
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    Why is it giving me this error, all I am doing is changing output by adding one to whats already in there hoping that it would advance it.

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The address of a array in C is NOT a variable!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble writing code for project
    By keith1994 in forum C++ Programming
    Replies: 3
    Last Post: 05-11-2012, 03:56 PM
  2. Paint Program Example Source Code??
    By Welder in forum Windows Programming
    Replies: 9
    Last Post: 11-08-2007, 06:23 PM
  3. How Can I compile 3 source code in 1 program ?
    By lord_cedrich in forum C Programming
    Replies: 8
    Last Post: 12-10-2006, 05:10 AM
  4. My first program source code
    By Musicdip in forum C++ Programming
    Replies: 5
    Last Post: 09-28-2002, 11:55 PM