How to write to a txt file

Jan 25, 2019 at 11:58am
How to write to a txt file this program

#include<stdio.h>
main()
{
int i = 1;
int a=0,b=0,c=0;
for(a=1;a<=4;a++)
{
for(b=1;b<=5;b++)
{
for(c=1;c<=6;c++)
{
if(a!=b && b!=c && c!=a)
if(c>b && b>a)
{

printf("%d %d %d %d\n",i,a,b,c);
i = i + 1;

}
}
}
}
i = i + 1;
}
Jan 25, 2019 at 12:05pm
Jan 25, 2019 at 3:19pm
Please always use code tags when posting some code (the <> icon on the right of the text box) it makes your code readable to people.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<stdio.h>

int main() //in c++ main returns an int
{
    int i = 1;
    int a=0,
        b=0,
        c=0;

    for ( a = 1; a <= 4; a++ )
    {
        for ( b = 1; b <= 5; b++ )
        {
            for ( c = 1; c <= 6; c++ )
            {
                if ( a != b && b != c && c != a )
                {    //Missing brackets
                    if ( c > b && b > a )
                    {
                        printf("%d %d %d %d\n", i, a, b, c);
                        i = i + 1;
                    }
                }
            }
        }

        i = i + 1;    //what is the purpose of this?
    }

    return (0);    //missing return statement
}


Well a couple of things, first you're writing in C, to use C++ you #include <iostream> and use std::cout instead of pringf(). Also, the main function returns an int.

Second I have no Idea what you're trying to do with those for loops. About those, why initialise your a, b and c ints and then change their values in your for statement? You could as well have left them at 0 for (a; a <5; a++){}

I don't know if it's the result of you not using code tags, if it is ignore my following remark. Your code could use some spacing, especially between your brackets
for ( a = 1; a <= 4; a++ ) reads a lot better than for(a=1;a<=4;a++) for instance :)
Last edited on Jan 25, 2019 at 3:27pm
Jan 25, 2019 at 3:25pm
is this C or C++?
if its C, you need to add a file*, open it, and write to it. you can use fprintf instead of printf, it has an extra arg for the file pointer.. my memory is rusty you can google it but roughly
FILE * fp;
fp = fopen("filename", options);

fprintf(fp, … etc);

you can also redirect this kind of program to a file without the effort:
in your console where you run the program, and it works on dos and unix the same way, just run the program and add >filename to the end of the console command.

Jan 25, 2019 at 4:18pm
C++
Jan 25, 2019 at 4:24pm
İt is working Well but can not write as a txt file
Jan 25, 2019 at 4:34pm
İt is about conbination
Jan 25, 2019 at 4:57pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
   const int NMAX = 6;
   int i = 0;

   // Choose ONE of the following
// ofstream out( "output.txt" );       // for file
   ostream &out = cout;                // for screen

   for ( int a = 1; a <= NMAX - 2; a++ )
   {
      for ( int b = a + 1; b <= NMAX - 1; b++ )
      {
         for ( int c = b + 1; c <= NMAX; c++ )
         {
            int n = 100 * a + 10 * b + c;
            i++;                      // Largest i should be 6C3 = 20
            out << i << ": " << n << '\n';
         }
      }
   }
}
Last edited on Jan 25, 2019 at 4:59pm
Jan 26, 2019 at 6:59am
When I const int NMAX = 12 the result is wrong
after 1 2 9 it sould be 1 2 10 but it print 1 3 0


#include <iostream>
#include <fstream>
using namespace std;

int main()
{
const int NMAX = 6;
int i = 0;

// Choose ONE of the following
// ofstream out( "output.txt" ); // for file
ostream &out = cout; // for screen

for ( int a = 1; a <= NMAX - 2; a++ )
{
for ( int b = a + 1; b <= NMAX - 1; b++ )
{
for ( int c = b + 1; c <= NMAX; c++ )
{
int n = 100 * a + 10 * b + c;
i++; // Largest i should be 6C3 = 20
out << i << ": " << n << '\n';
}
}
}
}
Jan 26, 2019 at 7:07am
Well, I answered your original question, not the one I didn't know you were going to change it to.

If you want the upper limit to be arbitrary then just output a, b, c as separate variables rather than combining them into n.
Jan 26, 2019 at 9:04am
Thank you for your help I want Upper limit but I cant understand what you mean (just out put a,b,c as sepaerate variables rather than combining them into n.)
I am not good in c++

For example
const int NMAX = 6; or 7 or 12 or 20
can you do instead of const int NMAX = 6 to const int NMAX = 12 or a bigger number
thanks

Jan 26, 2019 at 10:08am
Just
out << i << ": " << a << " " << b << " " << c << " " << '\n';

If you are actually after combinations, i.e. nCr, then there are alternative ways of calculating them. Here, you have restricted yourself to r=3.
Jan 26, 2019 at 1:06pm
Thank You Very Much
Thats it
Topic archived. No new replies allowed.