# CodeBlocks output window help in C++

• C/++/#
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.

Related Programming and Computer Science News on Phys.org
jedishrfu
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.

certainly
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.

Mark44
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.

certainly
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;
}

jedishrfu
Mentor
Did you remember to close the file you wrote to?

I forgot to say this before but my OS is Windows 7.

Yep.
Did you remember to close the file you wrote to?

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;
}

jedishrfu
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:
#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.

certainly
Mark44
Mentor
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.

certainly
jedishrfu
Mentor
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.

certainly
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?

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!

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.

I really must be sleeping now! Good Night Mark44 and jedishrfu. See you tomorrow!

Mark44
Mentor
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.

certainly
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.

jedishrfu
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...

Mark44
Mentor
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).

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.

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?

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!

Mark44
Mentor
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.

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)