CodeBlocks output window help in C++

In summary: #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++) {
  • #1
certainly
156
36
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.
 
Technology news on Phys.org
  • #2
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.
 
  • Like
Likes certainly
  • #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.
 
  • #4
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.
 
  • Like
Likes certainly
  • #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
I forgot to say this before but my OS is Windows 7.
 
  • #8
Yep.
jedishrfu said:
Did you remember to close the file you wrote to?
 
  • #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;
}
 
  • #10
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:
#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.
 
  • Like
Likes certainly
  • #11
certainly said:
Does the "program>output.txt" come at the very beginning ?
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.
 
  • Like
Likes certainly
  • #12
Capturing output is a common task in the windows command line:
Code:
dir *.* >dir.rpt
will produce a file dir.rpt that is a list of your files in the current directory.
 
  • Like
Likes certainly
  • #13
Mark44 said:
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.
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?
 
  • #14
jedishrfu said:
There is a feature in the PF post where you can add [ code ] tags around your code to display it with the proper formatting
Thanks for this!
 
  • #15
jedishrfu said:
Capturing output is a common task in the windows command line:
Code:
dir *.* >dir.rpt
will produce a file dir.rpt that is a list of your files in the current directory.
Thanks. I will definitely try this tomorrow.
 
  • #16
I really must be sleeping now! Good Night Mark44 and jedishrfu. See you tomorrow!
 
  • #17
certainly said:
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?
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:
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.
 
  • Like
Likes certainly
  • #18
Mark44 said:
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.
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.
 
  • #19
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...
 
  • #20
certainly said:
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.
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:
 cout<<"enter d: ";
you could do this:
Code:
 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.
 
  • #21
jedishrfu said:
convert a string to an integer.
when we need to input really large integers, ones that exceed the range of commonly used data types then is this technique used?
 
  • #22
Hmmmmmm...It's very interesting how so many solutions could work for one single problem. at first I was surprised that I did not have to include any other header files, later saw here that it's included under iostream. Thanks to you and jedishrfu now, I am now a good deal ahead of my class. THANKS!
 
  • #23
certainly said:
when we need to input really large integers, ones that exceed the range of commonly used data types then is this technique used?
Or not necessarily even very large numbers. You can take input one character at a time and build an integer or floating point number from the input. A reason to do this would be input validation.

Keep in mind that virtually every key that you press on a keyboard is a character, and that includes the number keys 1, 2, 3, and so on. The cin object in C++ and standard library functions such as scanf() in C convert the input characters to numbers or whatever type is represented by the variable being input to.

For example, in this code...
Code:
int num;

cout >> "Enter a number: ";
cin << num;
... if you press 5 and 2, the characters '5' and '2' are converted into the integer value 52, and then stored in the num variable.
 
  • #24
certainly said:
when we need to input really large integers, ones that exceed the range of commonly used data types then is this technique used?

Adding to the answer already given. When you need to input "really large integers" you will not use any primitive type for sure.

There are a few arbitrary precision integer implementations out there if you need to get something like the first prime with more than one thousand digits for a problem (cool kids like big primes).

1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007 needs 3319 bits to be represented!

52 unsigned long (varies according to implementation).

If you pass not 5 and 2 as Mark suggested, but my prime, what do you get? (I am telling you to test it out if you don't already know the answer)
 

1. How do I access the CodeBlocks output window in C++?

In order to access the CodeBlocks output window in C++, you can either go to the "View" menu and select "Logs & others" and then "Build log", or you can press the "F2" key on your keyboard.

2. Why is my code not appearing in the CodeBlocks output window?

There could be several reasons for your code not appearing in the CodeBlocks output window. First, make sure that you have correctly included the necessary header files and have declared the main function. Also, check for any syntax errors in your code that may be preventing it from compiling.

3. How can I clear the CodeBlocks output window?

To clear the CodeBlocks output window, you can either click on the "Clear" button at the top of the window, or you can use the "Ctrl + F2" keyboard shortcut.

4. Is it possible to save the CodeBlocks output window to a file?

Yes, it is possible to save the CodeBlocks output window to a file. Simply right-click on the window and select "Save As". You can then choose the file name and location to save the output to.

5. Can I customize the appearance of the CodeBlocks output window?

Yes, you can customize the appearance of the CodeBlocks output window. You can change the font, font size, and color scheme by going to "Settings" > "Environment" > "Logs & others". You can also resize the window by dragging the edges with your mouse.

Similar threads

  • Programming and Computer Science
Replies
19
Views
3K
  • Programming and Computer Science
Replies
13
Views
6K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
4
Views
7K
  • Programming and Computer Science
Replies
29
Views
5K
  • Programming and Computer Science
Replies
30
Views
4K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top