having trouble writing a program that generates source code

Onceler

Golden Member
Feb 28, 2008
1,262
0
71
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:

exdeath

Lifer
Jan 29, 2004
13,679
10
81
Why is there a ; on the while? Its only going to run that block once and independent of the while condition.
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
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
}
 

Onceler

Golden Member
Feb 28, 2008
1,262
0
71
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
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,698
4,658
75
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 :p). 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.
 

mv2devnull

Golden Member
Apr 13, 2010
1,526
160
106
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.
 

Onceler

Golden Member
Feb 28, 2008
1,262
0
71
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 :p). 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
 

xalos

Senior member
May 31, 2002
292
0
76
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:

Onceler

Golden Member
Feb 28, 2008
1,262
0
71
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.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,698
4,658
75
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.