Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

CodeBlocks output window help in C++

  1. Nov 24, 2014 #1
    Hi everyone! Fresh and new around here.
    I just started learning C++ and wrote a program yesterday to list all primes up-to an input n.
    It works just fine except for one problem. I tried listing all the primes up-to 100,000. Program completed in about 20 seconds, I scrolled up to the top but the list was starting from some weird number, not 2. But my program is working just fine with smaller numbers like 10,000 etc. I suspected that the output window was not scrolling up to the top. My suspicions were right, I noticed that whilst the program is running the scroll bar stops getting smaller after some time. Any idea how to fix this, so that I can view the complete list.
    I'm using Code::Blocks 10.05 mingw.
     
  2. jcsd
  3. Nov 24, 2014 #2

    jedishrfu

    Staff: Mentor

    well you could put multiple numbers on one line

    or you could pause when x lines are displayed, clear the screen and show another x lines.

    or you could write your output to file and view it when the program is finished.
     
  4. Nov 24, 2014 #3
    ok. I went off to study i/o in c++ and read about the fstream header file and ofstream and ifstream. so I just included ofstream a_file ("example.txt"); inside the int main() and replaced cout with a_file and added a_file.close(); at the end just before return 0;. This created a .txt file alright, and it worked just fine until 1000, but when I entered 10,000 the resulting notepad file was some stuff written in Japanese or something. Help.
    P.S. the output is already displaying multiple numbers.
    BTW thanks @jedishrfu.
     
  5. Nov 24, 2014 #4

    Mark44

    Staff: Mentor

    A simpler approach is to redicrect the output to a text file. You can still use the standard output stream cout. Assuming the name of your executable is program.exe, you can redirect output to a text file named example.txt using this command: program >output.txt

    As far as getting Japanese text, I'm not sure what could be causing that without seeing your code. If you post your code I'm sure we can figure it out.
     
  6. Nov 24, 2014 #5
    Hey Mark!
    Does the "program>output.txt" come at the very beginning ?
    Thanks.
    P.S. I'm thinking that japanese stuff is something to do with the horizontal line limit in notepad (if there is any such thing) cause it seems to work just fine if I add "\n" before <<n. What do you think?
    This is my program so far:-

    #include <iostream>
    #include<math.h>
    using namespace std;

    int main()
    {
    unsigned long long int n,r,i,d;
    cout<<"enter d: ";
    cin>>d;
    for(n=2;n<=d;n++)
    {
    for(i=2;i<=sqrt(n);i++)
    {
    r=n%i;
    if (r==0)
    {
    break;
    }
    }
    if (r!=0)
    {
    cout<<n<<" ";
    }
    }
    return 0;
    }
     
  7. Nov 24, 2014 #6

    jedishrfu

    Staff: Mentor

    Did you remember to close the file you wrote to?
     
  8. Nov 24, 2014 #7
    I forgot to say this before but my OS is Windows 7.
     
  9. Nov 24, 2014 #8
    Yep.
     
  10. Nov 24, 2014 #9
    This is the program with the fstream header that will display Japanese stuff unless I add <<"\n"; after <<n
    #include <fstream>
    #include <iostream>
    #include<math.h>
    using namespace std;

    int main()
    {
    ofstream a_file("example.txt");
    unsigned long long int n,r,i,d;
    cout<<"enter d: ";
    cin>>d;
    for(n=2;n<=d;n++)
    {
    for(i=2;i<=sqrt(n);i++)
    {
    r=n%i;
    if (r==0)
    {
    break;
    }
    }
    if (r!=0)
    {
    a_file<<n<<" ";
    }
    }
    a_file.close();
    return 0;
    }
     
  11. Nov 24, 2014 #10

    jedishrfu

    Staff: Mentor

    There is a feature in the PF post where you can add [ code ] tags around your code to display it with the proper formatting

    Code (Text):

    #include <fstream>
    #include <iostream>
    #include<math.h>
    using namespace std;

    int main()
    {
        ofstream a_file("example.txt");
        unsigned long long int n,r,i,d;

        cout<<"enter d: ";
        cin>>d;

        for(n=2;n<=d;n++) {
            for(i=2;i<=sqrt(n);i++) {
                r=n%i;
                if (r==0) {
                    break;
                }
            }

            if (r!=0) {
                a_file<<n<<" ";
            }
        }

        a_file.close();

        return 0;
    }
     
    I reformatted it in an alternative style with { on the same line as the statement and added some blank lines for readability. You can add in the comments.
     
  12. Nov 24, 2014 #11

    Mark44

    Staff: Mentor

    It doesn't go in the code at all. This is what you type at the command prompt (the "DOS box") to start your program.
     
  13. Nov 24, 2014 #12

    jedishrfu

    Staff: Mentor

    Capturing output is a common task in the windows command line:
    Code (Text):


    dir *.* >dir.rpt

     
    will produce a file dir.rpt that is a list of your files in the current directory.
     
  14. Nov 24, 2014 #13
    Oh! Thanks.
    Okay, so the actual executable is in the \bin\debug and I typed out the entire path in cmd, but what about the output.txt it will have to be in the same directory and hence the debug folder, right? but I tried this and the .txt file only displays "enter d: " which is the state of my program before it actually starts running. Also shouldn't the name of the notepad file be output and not example if I'm writing in cmd program >output.txt?
     
  15. Nov 24, 2014 #14
    Thanks for this!
     
  16. Nov 24, 2014 #15
    Thanks. I will definitely try this tomorrow.
     
  17. Nov 24, 2014 #16
    I really must be sleeping now! Good Night Mark44 and jedishrfu. See you tomorrow!
     
  18. Nov 24, 2014 #17

    Mark44

    Staff: Mentor

    Unless you give a different path, the output file will be in the same directory as the executable.

    The output "enter d: " is NOT the state of your program before it starts running - the program IS already running. The reason you don't see anything else is that your program is waiting for you to enter the number d. Since you are redirecting output, you don't see the prompt.

    You should get rid of these lines:
    Code (Text):
    ofstream a_file("example.txt");
    a_file.close();
    When you enter the command program > xxx.txt a file named xxx.txt will be created, if it doesn't already exist, and any output from your program will go into it.
     
  19. Nov 25, 2014 #18
    So if I want to see the list of primes, I should not make the program ask me d, but enter the value of d in the code itself, right?
    EDIT:- Thanks! it worked.
     
  20. Nov 25, 2014 #19

    jedishrfu

    Staff: Mentor

    In programming there are many ways to solve a problem. A problem is more than just printing primes. It involves how you tell it the range of primes or how many you want or some other arcane limit. It involves whether you print the output to the terminal session or send it to a file or do both. It involves whether to use a GUI or to use the command line.

    We've shown you output redirection which means that now you don't see the prompt for input. You have a choice to hard code the input or to provide it via command line arguments or via an input file.... There are many ways. In this case with output redirection I'd use the command line argument to provide input which means you'll have to understand how to access the command line and how to convert a string to an integer.

    As you advance in your learning of C++ you'll discover many of these ways and know when, how and why to use them as there are always pros and cons to each scheme.

    And then there's Linux...
     
  21. Nov 25, 2014 #20

    Mark44

    Staff: Mentor

    Or just recognize that the program will be asking for input, but since output is being redirected, so you won't see the prompt.

    An alternative would be to send the prompt to stderr, the standard error device (the command prompt window).

    Instead of this:
    Code (Text):
     cout<<"enter d: ";
    you could do this:
    Code (Text):
     cerr<<"enter d: ";
    With this change, the prompt "enter d: " would go to the command window, so that you could see it, but any other output would go to the file you're redirecting other output to.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: CodeBlocks output window help in C++
  1. C++ Code Help (Replies: 3)

Loading...