Thread: Saving number Output

  1. #31
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    Yeah. Well, I didn't read rule one, so I didn't realise the whole thing about blocks. Here's my new code:

    Code:
    #include <stdio.h>
    #include <fstream>
    #include <iostream>
    
      using namespace std;
    void myflush(std::istream& in)
    {
      in.clear();
      in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    void mypause()
    {  
      myflush(std::cin);
      std::cin.get();
    }
    int main(void)   
    {
       char str[10];
       srand((unsigned)time(0)); 
       ofstream a_file ( "example.txt" );
       int random_integer; 
       for(int index=0; index<7; index++)
          {
             random_integer = (rand()%99)+1;
             a_file << random_integer;
       if (index) cout << ", ";
       { 
             cout << random_integer;
       }   
          }
         cout << endl;
         a_file.close();
         mypause();
    }

  2. #32
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    OK, so the using keyword should be at level 0 since it's outside any function.
    But also do tell me why you put the { for the for loop at level 1 and the code inside at level 2 and why the if is still at level 1?
    I need to understand your mindset.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #33
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    Oh. I knew I forgot something. I just did, I guess I made a mistake.

    Here's the improved version:

    Code:
    #include <stdio.h>
    #include <fstream>
    #include <iostream>
    
      using namespace std;
    void myflush(std::istream& in)
    {
      in.clear();
      in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    void mypause()
    {  
      myflush(std::cin);
      std::cin.get();
    }
    int main(void)   
    {
       char str[10];
       srand((unsigned)time(0)); 
       ofstream a_file ( "example.txt" );
       int random_integer; 
       for(int index=0; index<7; index++)
             {
             random_integer = (rand()&#37;99)+1;
             a_file << random_integer;
       if (index) cout << ", ";
             { 
             cout << random_integer;
             }   
             }
         cout << endl;
         a_file.close();
         mypause();
    }

  4. #34
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You're not doing it right. Explain to me why you're doing as you do, so I can understand how you're thinking.
    Don't try to fix it. Not yet. Just explain why you write the indentation as you do. It will be much easier to fix your indentation if I understand how you think and what you're confused about.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #35
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    Ok, for the "For" loop you asked why it isn't 2 leveled, I presume by level you 2 you mean 6 spaces after the "F" in for, since each level is 3 spaces. I did this aswell for the "If" statement.

  6. #36
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    OK, so you understand that all code inside a block gets +1 indentation right?
    So shouldn't that means that all the code inside the for should be at level 2 (since the loop is at level 1)?
    And the code inside the if (or the local block to be precise, but more on that later), should be level 3?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #37
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by Apocalyptic_end View Post
    Oh well I'm using Dev-C++ and it says it says 13:3 and I know the (Well I thought) the 3 meant 3 spaces. So I guess I was wrong. Thanks for the help.
    13:3 would mean the cursor is in the 3rd column, and if that's the first character, you only have two spaces to the left.

  8. #38
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Assuming that you consistently use braces for all control statements, and you don't break long lines, there's a simple guideline: if any line that is not a closing brace starts further to the left than the line above, you've got something wrong.
    If any line that is just a closing brace doesn't start further to the left than the line above, you've got something wrong, too.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. counting number of lines in my output file
    By zesty in forum C Programming
    Replies: 34
    Last Post: 12-25-2007, 09:15 PM
  2. 2d game
    By JordanCason in forum Game Programming
    Replies: 5
    Last Post: 12-08-2007, 10:08 PM
  3. I am lost on how to read from file and output to file?
    By vicvic2477 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 11:52 AM
  4. Funny output on hex value of number
    By shav in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 08:19 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM