• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

having trouble writing a program that generates source code

Onceler

Golden Member
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 skips the printf's so I can't tell it's progress and I am not sure I am doing it right in the first place. 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:
Why is there a ; on the while? Its only going to run that block once and independent of the while condition.
 
Why is there a ; on the while? Its only going to run that block once and independent of the while condition.

Yup, that's the OP's bug.

Code:
# these two are the same

while (counter2 !=0);

while (counter2 !=0) {
    // empty
}

This means that the while loop in the OP's code doesn't do anything. It's equivalent to the following
Code:
while (counter2 !=0) {
    // do nothing
}
{
    // code the OP meant to be in the loop
}
 
Now my code looks like this:
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.
Thanks
 
Use the syntax highlighting!

PHP:
#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;
}

Now you can see that your strings are arranged very confusingly. You should be using a syntax-highlighting editor such as Notepad++ (or GVim, but I don't want to confuse you further 😛). And why didn't you use [post=35865342]this really nice macro[/post] I told you about, to put code in a string? It could help you a lot.
 
Code:
if counter3==200;
That does not look like C syntax. Perhaps:
Code:
if (counter3==200)
{
}
But it is still an empty body, just like the while was.
 
Use the syntax highlighting!

PHP:
#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;
}

Now you can see that your strings are arranged very confusingly. You should be using a syntax-highlighting editor such as Notepad++ (or GVim, but I don't want to confuse you further 😛). And why didn't you use [post=35865342]this really nice macro[/post] I told you about, to put code in a string? It could help you a lot.

because my char is not a constant and I will not be working with ints other than reading them in
 
I cleaned a few things up based on what I thought you were going for. I'm assuming that you're just starting out and trying to learn how to output to a file? Because you're not currently outputting anything meaningful but it seems like you're trying to print the countdown of counter2 since you subtract 2 until its 0 and that sounds like something I'd do for practice.


Code:
#include<stdio.h> 

int main() 
{ 
    unsigned long long counter1 = 18446744073709551615; 
    unsigned long long counter2 = counter1; 
    char output[8]={0,0,0,0,0,0,0,1}; 
    int counter3 = 0; 
 
    FILE *fp; 
    fp=fopen("crypt.txt", "w");  // We're writing stuff to a file...

   while (counter2 != 0) // This will run forever since counter1 is odd.....
   { 
    	if (counter3 <= 200) // This didn't make any sense the that way it was... I assume you wanted <=200 since counter started at 0
        { 
            if (counter2 <= counter1) 
            { 
               fprintf(fp,"if (counter2<=STUCK"); // Syntax error was here, you forgot a quotation mark
               fprintf(fp,"if counter2<=counter1%s\n",output); 
               counter2 = counter2 - 2; 
               //(output+1); // This wasn't doing anything so I commented it out....
               printf("%s\n",output); 
               printf("%llu\n",counter2); 
            }
            counter3++; 

            /*  
            // I think you were doing some sort of debugging here... so I just commented it out
            { 
            fprintf(fp,"if (counter2<=STUCK) 
            } 
            */
        }
    } 
    fclose(fp);  // Need to close these :P
    return 0; 
}
 
Last edited:
It's supposed to be(warning pseudo code):


if (counter3 <= 200) // This didn't make any sense the that way it was... I assume you wanted <=200 since counter started at 0
The way I wanted it was to fprintf nested if's in if's. Every 200 count make it use a master if for all of them under the if so that when the program is compiled and run the computer doesn't have to go through as many if's making the program much faster.
 
The way I wanted it was to fprintf nested if's in if's. Every 200 count make it use a master if for all of them under the if so that when the program is compiled and run the computer doesn't have to go through as many if's making the program much faster.
"Premature optimization is the root of all evil (or at least most of it) in programming." - Donald Knuth

I'm not saying don't do that optimization. But I am suggesting you might want to hold off doing that optimization until the program works without it.

Speaking of optimizations you could do later, are you sure some of this couldn't go in a loop in the resulting program? With data held in a data structure like an array perhaps? But, again, you might want to get the program working first.
 
Back
Top