[...] I only recently started with C.
Actually, your program is written in C++. C is a completely different language, which is why it has its own forum. Try to keep your languages straight. Unless your statement is unrelated to the rest of your post, in which case it's out of place.
How about a semicolon here?
Why do you keep opening and then closing the file? This is much more efficient, and won't cause the file to be clobbered with every character that you write to it.
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
int n;
int m;
cout << "Enter the max number: ";
cin >> m;
cout << "Enter the starting number: ";
cin >> n;
ofstream myfile;
myfile.open ("Combinations.txt");
while (n>=0, n<m)
{
cout << n << " ";
myfile << n;
++n;
}
myfile.close();
system("PAUSE");
return EXIT_SUCCESS;
}
Also, you should include <cstdlib> for system() if you must use system() (it's unportable, slow, and insecure; see the FAQ for alternatives), and the same header file anyway for EXIT_SUCCESS.
And an alternative to this
Code:
ofstream myfile;
myfile.open ("Combinations.txt");
is
Code:
ofstream myfile("Combinations.txt");
Note that you don't check to make sure that the file could be opened, which is a very good idea.
[edit] And the loop thing that anon mentioned. [/edit]