Pokémon TCG: Sword and Shield—Brilliant Stars

C++ help

legotack

New Member
Please read before posting:
Do not give me the complete answer, just try to start me on the right track.

Here is my assignment: Make a program that counts from one to a trillion (it's okay if I just start out to 100 or something and get bigger) AND says each number in words.

Here is my program so far:

#include <iostream>
#include <string>

using namespace ***;

string onesPlaceWrite(int, int);
string tensPlaceWrite(int, int);
string hundredsPlaceWrite(int);

int main()
{
bool writeTens = false;
bool writeHundreds = false;
int onesPlaceNumberWrite;

for (int hundredsPlaceNumber = 0; hundredsPlaceNumber <= 9; hundredsPlaceNumber = hundredsPlaceNumber + 1)
{
if (hundredsPlaceNumber > 0)
{
writeHundreds = true;
}

for (int tensPlaceNumber = 0; tensPlaceNumber <= 9; tensPlaceNumber = tensPlaceNumber)
{
for (int onesPlaceNumber = 1; onesPlaceNumber <= 10; onesPlaceNumber = onesPlaceNumber + 1)
{
onesPlaceNumberWrite = onesPlaceNumber;

if (onesPlaceNumber == 10)
{
onesPlaceNumberWrite = 0;
tensPlaceNumber = tensPlaceNumber + 1;
writeTens = true;
}
if (writeHundreds == true)
{
cout<<hundredsPlaceNumber;
}
if (writeTens == true)
{
cout<<tensPlaceNumber;
}



cout<<onesPlaceNumberWrite<<" is "<<hundredsPlaceWrite(hundredsPlaceNumber)<<tensPlaceWrite(tensPlaceNumber, onesPlaceNumberWrite)<<onesPlaceWrite(onesPlaceNumber, tensPlaceNumber)<<endl;
}
}




}
}

string onesPlaceWrite(int onesPlaceNumber, int tensPlaceNumber)
{
string onesPlaceText;

if (onesPlaceNumber == 0)
{
onesPlaceText = "";
}
if (onesPlaceNumber == 1 && tensPlaceNumber != 1)
{
onesPlaceText = "one";
}
if (onesPlaceNumber == 2 && tensPlaceNumber != 1)
{
onesPlaceText = "two";
}
if (onesPlaceNumber == 3 && tensPlaceNumber != 1)
{
onesPlaceText = "three";
}
if (onesPlaceNumber == 4 && tensPlaceNumber != 1)
{
onesPlaceText = "four";
}
if (onesPlaceNumber == 5 && tensPlaceNumber != 1)
{
onesPlaceText = "five";
}
if (onesPlaceNumber == 6 && tensPlaceNumber != 1)
{
onesPlaceText = "six";
}
if (onesPlaceNumber == 7 && tensPlaceNumber != 1)
{
onesPlaceText = "seven";
}
if (onesPlaceNumber == 8 && tensPlaceNumber != 1)
{
onesPlaceText = "eight";
}
if (onesPlaceNumber == 9 && tensPlaceNumber != 1)
{
onesPlaceText = "nine";
}

return onesPlaceText;
}

string tensPlaceWrite(int tensPlaceNumber, int onesPlaceNumber)
{
string tensPlaceText;

if (tensPlaceNumber == 0)
{
tensPlaceText = "";
}

if (tensPlaceNumber == 1 && onesPlaceNumber == 0)
{
tensPlaceText = "ten";
}
if (tensPlaceNumber == 1 && onesPlaceNumber == 1)
{
tensPlaceText = "eleven";
}
if (tensPlaceNumber == 1 && onesPlaceNumber == 2)
{
tensPlaceText = "twelve";
}
if (tensPlaceNumber == 1 && onesPlaceNumber == 3)
{
tensPlaceText = "thirteen";
}
if (tensPlaceNumber == 1 && onesPlaceNumber == 4)
{
tensPlaceText = "fourteen";
}
if (tensPlaceNumber == 1 && onesPlaceNumber == 5)
{
tensPlaceText = "fifteen";
}
if (tensPlaceNumber == 1 && onesPlaceNumber == 6)
{
tensPlaceText = "sixteen";
}
if (tensPlaceNumber == 1 && onesPlaceNumber == 7)
{
tensPlaceText = "seventeen";
}
if (tensPlaceNumber == 1 && onesPlaceNumber == 8)
{
tensPlaceText = "eighteen";
}
if (tensPlaceNumber == 1 && onesPlaceNumber == 9)
{
tensPlaceText = "nineteen";
}

if (tensPlaceNumber == 2)
{
tensPlaceText = "twenty ";
}
if (tensPlaceNumber == 3)
{
tensPlaceText = "thirty ";
}
if (tensPlaceNumber == 4)
{
tensPlaceText = "forty ";
}
if (tensPlaceNumber == 5)
{
tensPlaceText = "fifty ";
}
if (tensPlaceNumber == 6)
{
tensPlaceText = "sixty ";
}
if (tensPlaceNumber == 7)
{
tensPlaceText = "seventy ";
}
if (tensPlaceNumber == 8)
{
tensPlaceText = "eighty ";
}
if (tensPlaceNumber == 9)
{
tensPlaceText = "ninety ";
}

return tensPlaceText;
}

string hundredsPlaceWrite(int hundredsPlaceNumber)
{
string hundredsPlaceText = "";

if (hundredsPlaceNumber == 0)
{
hundredsPlaceText = "";
}

if (hundredsPlaceNumber == 1)
{
hundredsPlaceText = "one hundred ";
}
if (hundredsPlaceNumber == 2)
{
hundredsPlaceText = "two hundred ";
}
if (hundredsPlaceNumber == 3)
{
hundredsPlaceText = "three hundred ";
}
if (hundredsPlaceNumber == 4)
{
hundredsPlaceText = "four hundred ";
}
if (hundredsPlaceNumber == 5)
{
hundredsPlaceText = "five hundred ";
}
if (hundredsPlaceNumber == 6)
{
hundredsPlaceText = "six hundred ";
}
if (hundredsPlaceNumber == 7)
{
hundredsPlaceText = "seven hundred ";
}
if (hundredsPlaceNumber == 8)
{
hundredsPlaceText = "eight hundred ";
}
if (hundredsPlaceNumber == 9)
{
hundredsPlaceText = "nine hundred ";
}

return hundredsPlaceText;
}






------------------------------------------

Check the last post for the problem.

Why is it like this? What am I doing wrong?

Any help would be appreciated.
 
Last edited:
The only issue I can see is this line:

for (int x = 0; x <= 9; x = x+ 1)
{
cout<<tensPlace(x);

If you only intend to count from 0 to 19, the tens place should only ever go to 1.
 
You've got three embeded for-loops. If I do my math right, you'll go through the inner loop 10*10*18=1800 times, significantly more than your "19" times. It seems like it's counting correctly (if 11 places off), just something's wrong with that loop. I think I've spotted it. Cut that +1 in your function calls. It's not necessary, and it's throwing your results off by 11 places.

So, try working with those for a little while (cut out that initial loop, or at least comment it out), and see what happens.
 
^Thanks a ton bullados, it now almost works from 1-99. The only problem is that it does not show up when the ones place is 0 (10, 20, 30, etc.). The OP has been updated with my newest code. I can't seem to find the problem. Thanks in advance!
 
your second for loop goes from 1 to 9. Typo much? LOL!

Also, have you tried using a Switch-Case type statement? It's much easier to read than endless if-then statements...
 
Sorry to bother you again, I just have one more problem. I'm working on the hundreds, but there is a problem. My code is in the OP. My problem is that the number that is big, bold, and in red changes many things for some reason. If it is at 9, the program starts at 705 and goes up to 999. If it is at 1 it starts at 1 and goes to 199. If it is at 2 it starts at 5 and goes to 299. Why is this and how do I fix it?

Thanks again!
 
You're trying too hard. Just go from zero. The code can be modified from there.
 
just a comment on the style, lego, change all your "whatever = whatever + 1"'s to "whatever++", it looks nicer, and also has less chance of doing weird things to your loops

second, I would try making all your loops uniform. at the moment, you have hundreds place going from 0 to 9, tens place going from 0 to nine, but ones place going from 0 to 10, it probably would be a little easier to control if you do them all the same

but more of a problem is this loop
Code:
for (int tensPlaceNumber = 0; tensPlaceNumber <= 9; tensPlaceNumber = tensPlaceNumber)
which is never going to change value ever the way it's written :wink:

(another case for using i++ in loops for incrementing by ones, so you can easily tell when somethings wrong when glancing at it)
 
Thanks for the help, now my code is much shorter and easier to follow:rolleyes:. I started out doing what Anaconda suggested, but it didn't work, and after trying it again, it still didn't. Although I start everything at 0, like bullados suggested, my program still just starts at 704. Am I doing something wrong?

Back to back posts merged. The following information has been added:

After trying with many things, I have found the problem. For some reason Windows puts a limit on the execution window (I'm not sure what it's called) and so 705 is the highest it goes. Thanks again!
 
Last edited:
This is more C than C++ (my C++ is quite rusty), but....

Code:
#include <iostream>
#include <string>

using namespace ***;

const char *onesText[10]  = {"","one","two","three","four","five","six","seven","eight","nine"};
const char *tysText[10]   = {"","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
const char *teensText[10] = {"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};

int main() {
    for (int hundreds = 0; hundreds <= 9; hundreds++) {
        for (int tens = 0; tens <= 9; tens++) {
            for (int ones = 0; ones <= 9; ones++) {
                if (hundreds == 0 && tens == 0 && ones == 0) continue; // Skip zero
                cout << (hundreds * 100 + tens * 10 + ones) << " is ";
                if (hundreds)
                    cout << onesText[hundreds] << " hundred ";
                if (tens > 1)
                    cout << tysText[tens] << " ";
                cout << ((tens == 1) ? teensText[ones] : onesText[ones]) << endl;
            }
        }
    }

    return 0;
}

You know this doesn't really go to a trillion, though :biggrin:
 
That's BEAUTIFUL, Stephen! I don't think I could've come up with anything prettier than that. Though, I think it might be above the coding level of OP with the use of pointers and the very advanced array structure you've used for this.

Find a C++ manual and look up the Switch and Case statements. Those will make your life easier.
 
That's BEAUTIFUL, Stephen!

Sorry, I'll fix that:

perl -e 'for(split//,"#defghilnorstuvwxy\40\n"){push@d,$_}@f=split//,"\0\f\2\10\0\0\t\10\2\0\2\7\2\16\26\30\f\17\t\24\17\36!\47\26\f\21\24\5\n\2\34\f\5\6\n\25\"46-\0\3\t\r\n=?72\27>7.\3\6*J\0038F\6N.\13\6\20\0T\20OWU<\13\40\"^!\10Za,.\2\6\4\5\f\35ikEmj<\10\6\33\0t\33Zx\2<\22\0\22\6\13~\22\5\r\10\0011\1~\23";$o=ord(shift@f);$t=$d[$o];while($_=shift@f){$n=ord($_);if($n<@d){$s=$d[$n];$t.=$s;$c=substr$s,0,1;push@d,$d[$o].$c;$o=$n}else{$s=$d[$o].$c}}@t=split/#/,$t;for(1..~~@t*30-21){split//,reverse;print$_,$t[31],$_[2]&&$t[$_[2]*3].$t[32],$_[1]>1&&$t[$_[1]*3+2].$t[30],($_[1]==1?$t[$_[0]*3+1]:$t[$_[0]*3]),$t[33]}'

Okay, the board is dumb and likes to insert random spaces in the above code (which screws it up), but it leaves
Code:
 blocks alone.  So:

[CODE]perl -e 'for(split//,"#defghilnorstuvwxy\40\n"){push@d,$_}@f=split//,"\0\f\2\10\0\0\t\10\2\0\2\7\2\16\26\30\f\17\t\24\17\36!\47\26\f\21\24\5\n\2\34\f\5\6\n\25\"46-\0\3\t\r\n=?72\27>7.\3\6*J\0038F\6N.\13\6\20\0T\20OWU<\13\40\"^!\10Za,.\2\6\4\5\f\35ikEmj<\10\6\33\0t\33Zx\2<\22\0\22\6\13~\22\5\r\10\0011\1~\23";$o=ord(shift@f);$t=$d[$o];while($_=shift@f){$n=ord($_);if($n<@d){$s=$d[$n];$t.=$s;$c=substr$s,0,1;push@d,$d[$o].$c;$o=$n}else{$s=$d[$o].$c}}@t=split/#/,$t;for(1..~~@t*30-21){split//,reverse;print$_,$t[31],$_[2]&&$t[$_[2]*3].$t[32],$_[1]>1&&$t[$_[1]*3+2].$t[30],($_[1]==1?$t[$_[0]*3+1]:$t[$_[0]*3]),$t[33]}'
 
Last edited:
OOPMAN says:
Create a class that has 16 ints, one for each digit. (16 right?) (or, for more flexibility, create one that can grow as needed w/ infinite precision)
give it a function "increment" that does the right thing (carrying, etc)
give it a function "output" that does the right thing for each digit. (you could even pull some wonkiness and make 3 digit subclasses to display "one/ten/hundred" depending on it's significance)
Loop increment/output 1 trillion times.
 
This is more C than C++

Wow. That was... awesome.

I have a couple questions about it though -

"const char *onesText[10]"

Why is the asterisk there? How are you storing strings (words) in an array allocated for char? I suppose that might have to do with pointers and advanced-array-structure (as Bullados said), and I haven't studied pointers yet, but I'm, uh, a quick study. And ambitious. :3
 
"const char *onesText[10]"

Why is the asterisk there? How are you storing strings (words) in an array allocated for char? I suppose that might have to do with pointers and advanced-array-structure (as Bullados said), and I haven't studied pointers yet, but I'm, uh, a quick study. And ambitious. :3

It can also be written as const char* onesText[10] (the space before the * is one of those old style habits that I can't seem to kill).

A string literal ("string") is a pointer to the specified string on the stack (which gets auto-allocated by the compiler).

char*[] defines an array of pointers to strings, which we then populate with our stack pointers.

Pretty basic pointer stuff.

Now try the Perl version :lol:
 
Back
Top