Saving number Output

This is a discussion on Saving number Output within the C++ Programming forums, part of the General Programming Boards category; Hello. Let's start with my code: Code: #include <stdio.h> #include <fstream> #include <iostream> using namespace std; void myflush(std::istream& in) { ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    32

    Saving number Output

    Hello. Let's start with my 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)); 
       int random_integer; 
       for(int index=0; index<7; index++){
       random_integer = (rand()%99)+1;
       if (index) cout << ", ";
       cout << random_integer;
      }
       cout << endl;
       ofstream a_file ( "example.txt" );
       a_file << random_integer;
       a_file.close();
       mypause();
      }
    When writing my "Example.txt" I want to make ALL seven numbers appear. But with this code only the last random number appears.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    That's because the write to example.txt is outside of the loop that generates the random numbers, so it only gets what is left in random_integer.

    Fix your indentation and you will see that.

    Todd

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    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.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    32

    Fixed my indentation

    Well... I've fixed my indentation and Moved some of my Text Writing 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();
             
      }
    It's still not working. Why is that? I put it in both the For loop and If statement.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    What does it do now?
    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

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    32

    Still only one number

    In Example.txt it only repeats the last of my 7 Random_integer numbers. I want all of them to be shown.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Looking at the code, I have a hard time believing that. Are you sure you've compiled and run the changes correctly?
    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

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    Yes. I'm sure. I've Compiled it multiple times. Ran it many times with the same affect.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Nope, I just compiled and ran it, and it worked exactly as I expected.

    Perhaps you're fooled by the fact that there are no spaces between the number in the output file, so it looks like one big number?
    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

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Indentation is far from fixed.
    Main function is horribly broken.
    Try reading http://cpwiki.sf.net/User:Elysia/Indentation
    And see if it helps you understand how to indent better.
    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.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    Ok. I see we are talking about talking to different things, I think. I want the 7 numbers to show up in the Example.txt file and not just in the program when I run it.

    When I run the program I have all 7 numbers but I don't have all 7 numbers show up in my example.txt file.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    32

    How's this indenting?

    Is this better indenting?

    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;
            if (index) cout << ", ";
            {
               a_file << random_integer;
               cout << random_integer;
            }   
       }
       cout << endl;
       a_file.close();
       mypause();
    }

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Program output is:
    Code:
    63, 90, 58, 93, 13, 99, 36
    example.txt contains:
    Code:
    63905893139936
    As you can see, the program works correctly.

    By the way, you should include <cstdlib> instead of <stdio.h>, and you shoud include <limits>.
    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

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    Hooray! I figured it out! I opened the Dev file instead of the CPP File.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Quote Originally Posted by Apocalyptic_end View Post
    Is this better indenting?
    Nope. Indentation levels are inconsistent.
    Have you tried reading the indentation articles? I just want to know if you're still confused after reading them.
    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.

Page 1 of 3 123 LastLast
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, 08:15 PM
  2. 2d game
    By JordanCason in forum Game Programming
    Replies: 5
    Last Post: 12-08-2007, 09: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, 10:52 AM
  4. Funny output on hex value of number
    By shav in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 07:19 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21