English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories
1

what's the code to make this happen:

x=0

cout << x;

the display should show
0000001

2006-10-26 15:07:32 · 5 answers · asked by ifoam 3 in Computers & Internet Programming & Design

what im trying to do is generate 500,000 unique numbers

so, im doing:

for(int x=0;x<=500000;x++)
cout << x << endl;

the problem with this is that it shows:
1
2
3
4
as the output.

i want it to show:
0000001
0000002
0000003
0000004
.............
4999999
5000000

2006-10-26 15:39:12 · update #1

5 answers

You could do this:

cout << setfill('0');

for(int x=0;x<=500000;x++)
cout << setw(6) cout << x << endl;

setfill sets the character used to pad your number.
setw(6) is volatile, and needs to be used each time.

Haven't tried it, but should work.

2006-10-26 16:37:13 · answer #1 · answered by Deirdre H 7 · 1 0

By default, the standard output of a program is the screen, and the C++ stream object defined to access it is cout.
cout is used in conjunction with the insertion operator, which is written as <<.

cout << "Output sentence"; // prints Output sentence on screen
cout << 120; // prints number 120 on screen
cout << x; // prints the content of x on screen

The << operator inserts the data that follows it into the stream preceding it. In the examples above it inserted the constant string Output sentence, the numerical constant 120 and variable x into the standard output stream cout. Notice that the sentence in the first instruction is enclosed between double quotes (") because it is a constant string of characters. Whenever we want to use constant strings of characters we must enclose them between double quotes (") so that they can be clearly distinguished from variable names. For example, these two sentences have very different results:

cout << "Hello"; // prints Hello
cout << Hello; // prints the content of Hello variable

The insertion operator (<<) may be used more than once in a single statement:

cout << "Hello, " << "I am " << "a C++ statement";

This last statement would print the message Hello, I am a C++ statement on the screen. The utility of repeating the insertion operator (<<) is demonstrated when we want to print out a combination of variables and constants or more than one variable:

cout << "Hello, I am " << age << " years old and my zipcode is " << zipcode;

If we assume the age variable to contain the value 24 and the zipcode variable to contain 78130 the output of the previous statement would be:

Hello, I am 24 years old and my zipcode is 78130.

It is important to notice that cout does not add a line break after its output unless we explicitly indicate it, therefore, the following statements:

cout << "This is a sentence.";
cout << "This is another sentence.";

will be shown on the screen one following the other without any line break between them:

This is a sentence.This is another sentence.

even though we had written them in two different insertions into cout. In order to perform a line break on the output we must explicitly insert a new-line character into cout. In C++ a new-line character can be specified as \n

cout << "First sentence.\n ";
cout << "Second sentence.\nThird sentence.";

This produces the following output:

First sentence.
Second sentence.
Third sentence.

Additionally, to add a new-line, you may also use the endl manipulator. For example:

cout << "First sentence." << endl;
cout << "Second sentence." << endl;

would print out:

First sentence.
Second sentence.

The endl manipulator produces a newline character, exactly as the insertion of '\n' does, but it also has an additional behavior when it is used with buffered streams: the buffer is flushed. Anyway, cout will be an unbuffered stream in most cases, so you can generally use both the \n escape character and the endl manipulator in order to specify a new line without any difference in its behavior.

There that should help.

2006-10-26 15:29:57 · answer #2 · answered by rabbit0102030 3 · 0 0

Here's the code for adding zeroes (0) to a cout stream:

int x = 42;
cout.width(7);
cout.fill(‘0’);
cout << x << ‘\n’; // Outputs 0000042

But in your example, you want to assign x=0, and that your result should be 0000001. Here's the code (just in case) for that, although it is redundant to show the same code but modified...

int x = 0; //Now we have to add 1 to 'x' somewhere before cout.

cout.width(7);
cout.fill(‘0’);
x++;
cout << x; // Outputs 0000001

2006-10-26 15:54:02 · answer #3 · answered by OverClocked 2 · 0 0

Question has lack of info

WOuld help if we new what 0000001 was suppose to be.
I assume octal...in that case add 1 and display it in octal.

or add one , change to a string and add 6 zero's to the begin od the string.

2006-10-26 15:34:28 · answer #4 · answered by capp 2 · 0 0

there R many answers for Ur case one of them has been said and the other is to make a condition in Ur loop just checking the number of digits in x and prints in a loop the number of remaining digits as zeros and then print x

2006-10-26 16:32:24 · answer #5 · answered by I.M. 2 · 0 0

fedest.com, questions and answers