> I need help with a c++ programming code.?

I need help with a c++ programming code.?

Posted at: 2014-12-18 
You should include if you're using the time() function.

Instead of that long if/else if chain, try:

cout << "0123456789ABDEF"[n];

Now you can go about using nested loops, as EddieJ suggests.

Even better, add #include at the top and use the fact that hex conversion is already built into every ostream object. The is not needed for that, but you'll probably want leading zeroes padded out to 6 digits.

cout << hex << setfill('0'); // set hex format with 0 padding on the left

for (int i=0; i<10; ++i) {

int n = (rand() ^ (rand()<<12)) & 0xFFFFFF;

cout << setw(6) << n << endl;

}

The monkey business with the double rand() is to allow for Visual C++, which has a miserably small RAND_MAX value of 32767. Unfortunately, MinGW uses the VC++ runtime library and suffers from the same problem. ("Normal" 32-bit implementations of GNU g++ don't have that 16-bit anachronism.)

If you KNOW you need to use a loop within a loop, why didn't you include a loop within the loop?

for (int i = 0; i < 100; ++i) {

for (int j = 0; j < 6; ++j) {

int n = rand() % 16;

if (n == 15) cout << 'F';

else if (n ==14) cout << 'E';

else if (n ==13) cout << 'D';

else if (n ==12) cout << 'C';

else if (n ==11) cout << 'B';

else if (n ==10) cout << 'A';

else cout << n;

}

cout << endl;

}

Write a program that generates a different list of 100 random RGB color values using hexadecimal notation. The program should write something like the following into cout. 324AF3 6A3125 ... 98 additional RGB values.

the code that i have so far:

#include

#include

using namespace std;

int main () {

srand(time(0));

for (int i = 0; i < 100; ++i) {

int n = rand() % 16;

if (n == 15) {

cout << 'F';

} else if (n ==14) {

cout << 'E';

} else if (n ==13) {

cout << 'D';

} else if (n ==12) {

cout << 'C';

} else if (n ==11) {

cout << 'B';

} else if (n ==10) {

cout << 'A';

} else {

cout << n;

}

cout << endl;

}



cout << endl;

}

it is only printing a single digit. i need to use a loop within a loop to to get it to print 6 digits figures. please solve :)