Thread: Greetings

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    16

    Greetings

    Sorry for being a complete noob in advance, but I only recently started with C.

    I'm trying to make a program that'll output all numbers based upon your inputs.

    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;
    
      while (n>=0, n<m)
      {
        cout << n << " ";
          ofstream myfile;
          myfile.open ("Combinations.txt");
          myfile << n;
          myfile.close();
        ++n;
      }
    
    
      system("PAUSE");
      return EXIT_SUCCESS;
    }
    The program works and it outputs the numbers, but only in the command prompt and I want to write it to a file (text file). I realise the error is right at the myfile portion, but I do not know what else I should put there. Any help would be greatly appreciated. Basically how to print a changing variable to a text file.
    Last edited by Nightwarrior; 10-06-2007 at 05:48 PM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Open the file before the loop. If you open it each time the previous contents will be overwritten (unless you use appending mode).
    You don't need to close an ofstream explicitly (unless you want to be sure it is closed at some particular point before the stream object goes out of scope).

    And the loop condition is wrong.
    Code:
      while (n>=0, n<m)
    The first part (n>=0) will be simply ignored. If you want multiple conditions to be true, use the "logical and" - && operator:
    Code:
      while (n >= 0 && n < m)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    [...] 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?
    Code:
    myfile << n ;
    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]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    16
    Thank you very much!

    Now is there any way for it to convert 0, 1, 2, 3, etc to 000, 001, 002, 003, etc? I know it's silly, but it'd look much better.

    *EDIT: How would one make it check to see if the file is open?
    Last edited by Nightwarrior; 10-06-2007 at 05:57 PM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Are you going to delete this post, too?

    Now is there any way for it to convert 0, 1, 2, 3, etc to 000, 001, 002, 003, etc? I know it's silly, but it'd look much better.
    Sure.
    Code:
    #include <iomanip>
    
    std::cout << std::setw(3) << 1;
    You can use any stream, not just cout. And you can leave off the std:: if you use using namespace std.

    *EDIT: How would one make it check to see if the file is open?
    With the is_open() member function. http://www.cprogramming.com/tutorial/lesson10.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    16
    Thank you for taking your time to help me.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    16
    Sorry for bugging you again, but the

    Code:
    std::cout << std::setw(3) << 1;
    is messing my output up. Where should it be added?

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You need to call setw(3) (or cout.width(3) btw) before you print out each integer. The width is reset to zero each time anything is printed.

    Here you will also have to use cout << setfill('0') or cout.fill('0'). Unlike the width, the fill will not be reset on each print, and thus needs to only be called once. This can lead to problems if other parts of your code rely on fill to be the default ' ', so beware.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Dwks's statement is inaccurate. setw() will only pad the following value with spaces, you have to call a setfill() before it in order to pad it with zeros (or whatever you might like).
    Code:
    // Be sure to include <iomanip>
    while (n>=0, n<m)
    {
        cout << setfill('0') << setw(3) << n << " ";
        myfile << setfill('0') << setw(3) << n; // Note that you aren't putting a space between values in the file
        ++n;
    }
    Lastly, don't make unsecure calls to the shell. That is to say, don't use system(). It is a bad habit that you should never get used to. There are much better ways to pause your program.
    Last edited by SlyMaelstrom; 10-06-2007 at 09:52 PM.
    Sent from my iPadŽ

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by SlyMaelstrom View Post
    Dwks's statement is inaccurate. setw() will only pad the following value with spaces, you have to call a setfill() before it in order to pad it with zeros (or whatever you might like).
    Code:
    // Be sure to include <iomanip>
    while (n>=0, n<m)
    {
        cout << setfill('0') << setw(3) << n << " ";
        myfile << setfill('0') << setw(3) << n; 
        ++n;
    }
    except that cout << setfill('0') and myfile << setfill('0') need only be called once. Like this:

    Code:
    cout.fill('0');
    myfile.fill('0');
    while (n>=0 && n<m)
    {
        cout << setw(3) << n << " ";
        myfile << setw(3) << n; // Note that you aren't putting a space between values in the file
        ++n;
    }
    //Set fill back to space.
    //It is also possible to set it back to the previous value, but that requires you to store
    // the previous value in a variable yourself.
    cout.fill(' ');
    myfile.fill(' ');
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    The results remain the same regardless... and besides, I did only call it once for each stream, which I believe is required. So really, what's the difference whether it's at the top or in the statement it's first used?
    Sent from my iPadŽ

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by SlyMaelstrom View Post
    The results remain the same regardless... and besides, I did only call it once for each stream, which I believe is required. So really, what's the difference whether it's at the top or in the statement it's first used?
    You call it n-m times for each steam since it's called inside a while loop. There is no real problem in doing this except that there is no need. Just call it once at the beginning.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed