Hello everyone,

So as usual, I'm trying another exercise in my C++ book, and the thing is, I think I got most of the logic of what I really want to convey via comments, and maybe 50% of the code, but its the logic near the end that's making me stuck for some reason.

Basically the core requirements for the program are as follows:

The goal itself is pretty clear, generate a number that's greater than 10,000 using a random number generator like last time, but this time, somehow go through each digit and print out it's text form as well.Code:/*5. Write a C++/CLI program that will generate a random integer greater than 10,000. Output the integer and then output the digits in the integer in words. For example, if the integer generated were 345678, then the output should be: The value is 345678 three four five six seven eight */

I thought maybe I could first declare the random number integer variable, use the random number generator and assign a value to that integer. Declare an integer array that will store each digit from random_number that's "n" numbers big because I don't know exactly what number the computer will generate.

Then, I was thinking to make a for loop that will assign each element of the integer array to each digit of random_number, and use a series of switch statements that will assign the current text version of the digit that's being examined.

My main logic flaw is in that final part, because I'm just stuck in terms of how to deal with numbers that are infinitely big, because I could probably code up to the billionth digit place holder, but there's always the possibility of dealing with random numbers that are greater than 1 billion.

So I'm basically stuck on how to actually do the math for some reason...

Anyway, here's my code thus far with a bunch of comments for myself as I go along, so forgive the mass of text between simple lines:

Code:// Chapt4_Exercise5.cpp : main project file. #include "stdafx.h" #include "math.h" // This math.h header will allow me to use the absolute and floor functions just // to determine the amount of digits that are present within the random_number using namespace System; int main(array<System::String ^> ^args) { /*5. Write a C++/CLI program that will generate a random integer greater than 10,000. Output the integer and then output the digits in the integer in words. For example, if the integer generated were 345678, then the output should be: The value is 345678 three four five six seven eight */ // Create the random number generator: Random^ random_num_gen = gcnew Random; // Create the integer variable that we will use: int random_number; // Assign random number to be a random number greater than 10,000, which I // am assuming means to literally multiply the random number we // generate by 10,000: random_number = 10000 * random_num_gen->NextDouble(); // Declare an integer array which we'll use to store each digit later on in // a for loop that will examine each digit of random_number so far // I don't know how many integers we'll actually need, so declare a place // holder variable called n, so we can later determine the amount of digits // in the actual random_number, and just store that in n // All I can think of is to use Length-> but obviously, that won't work // because I'm NOT DEALING WITH STRINGS! // Notice how I'm using the absolute and log10 function from the math.h // header file // As an aside, I came to this conclusion by looking at a similar question // on StackOverflow in which the user asked about how to determine the // amount of digits present using the C programming language: // Here is the link: // http://stackoverflow.com/questions/3068397/c-how-to-find-the-length-of-an-integer int n = floor(log10(abs(random_number))) + 1; array<int>^ int_array = gcnew array<int>(n); // Declare an accumulator string which I'll use later in a series of switch // statements to add to, so that I can get a running total of the // text versions of each digit String ^text; // Logic thus far, though it's really shaky honestly: // Make a for loop that will output the current digit, so I have to // strip each number out of an entire number, maybe put that into an array // Then, I want to use switch statements that will output 1 through 9 for // the current digit in text form, or at least add that text version of // the digit to a string that acts like an accumulator: // Maybe the only way to get each every separate digit on its own from // random_number is to divide each number by 10, or use the mod operator // to determine the remainder (much like an odd or even number generator // program would act) // The following is a skeletal for loop with switch statements, but my // confidence is reaching its limits for some weird reason, for I feel // this may or may not be the right approach: for(int i = 0; i < int_array->Length; i++) { // Examine each place holder... oh wait, this might be impossible // unless I make another arbitrary variable that will look at each and // every possible placeholder... // Exactly, what if this number I generate IS HUGE? What then? I would // have to keep modding it by 1 million, then 1 billion, etc, until it // would become impossible to actually predict // So the question is, do we impose a placeholder limit on how many // digits could exist? // Stuck... if(random_number % 10) { switch(x): case 0: text = text + "Zero"; break; case 1: text = text + "One"; break; case 2: text = text + "Two"; break; case 3: text = text + "Three"; break; case 4: text = text + "Four"; break; case 5: text = text + "Five"; break; case 6: text = text + "Six"; break; case 7: text = text + "Seven"; break; case 8: text = text + "Eight"; break; case 9: text = Text + "Nine"; break; } } return 0; }

Tweet This+ 1 thisPost To Linkedin