![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
|
|
|
|
#1 |
|
C++ help
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)<<tensPl aceWrite(tensPlaceNumber, onesPlaceNumberWrite)<<onesPlaceWrite(onesPlaceNum ber, 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 by legotack; 12/01/2009 at 05:44 PM. |
|
|
|
|
|
|
#2 |
|
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.
__________________
Team B-Side
|| cardmaker, competitive tcg player, competitive vg player. ||![]() || programmer, game designer; fantasy writer, amateur artist. || ![]() |
|
|
|
|
|
|
#3 |
|
^Thanks for the help, but that didn't seem to work. I can't think what the problem is.
__________________
|
|
|
|
|
|
|
#4 |
|
Why do you need help? C++ is a passing grade
|
|
|
|
|
|
|
#5 |
|
^Sig'd. Seriously, though, any help?
__________________
|
|
|
|
|
|
|
#6 |
|
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.
__________________
2008 North American Professor Cup Champion Minnesota Plasma Storm Prereleases Battle Road Spring 2013 If you know of any asynchronous multi-camera video recording software, please PM. Thank you! |
|
|
|
|
|
|
#7 |
|
^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!
__________________
|
|
|
|
|
|
|
#8 |
|
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...
__________________
2008 North American Professor Cup Champion Minnesota Plasma Storm Prereleases Battle Road Spring 2013 If you know of any asynchronous multi-camera video recording software, please PM. Thank you! |
|
|
|
|
|
|
#9 |
|
|
in kayles first post it looks like a bunch of text faces like:)and:9 and;0 ect.lol
__________________
I swear to god when my Magikarp evolves he's going to kill you all!
PokeNews:) |
|
|
|
|
|
#10 |
|
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!
__________________
|
|
|
|
|
|
|
#11 |
|
You're trying too hard. Just go from zero. The code can be modified from there.
__________________
2008 North American Professor Cup Champion Minnesota Plasma Storm Prereleases Battle Road Spring 2013 If you know of any asynchronous multi-camera video recording software, please PM. Thank you! |
|
|
|
|
|
|
#12 |
|
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) ![]() (another case for using i++ in loops for incrementing by ones, so you can easily tell when somethings wrong when glancing at it)
__________________
SLCPokemon - Part of Andrea's Army
Read the fanfic Skewed, the story of a twisted future for the world of Pokemon. (last updated 10-10-10) Regular Spelling (personal blog, updated often, more stories there) |
|
|
|
|
|
|
#13 |
|
Thanks for the help, now my code is much shorter and easier to follow
. 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 by legotack; 12/02/2009 at 03:59 AM. Reason: Doublepost Eliminator |
|
|
|
|
|
|
#14 |
|
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;
}
__________________
Stephen Clouse
Kansas City Pokémon |
|
|
|
|
|
|
#15 |
|
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
__________________
Team B-Side
|| cardmaker, competitive tcg player, competitive vg player. ||![]() || programmer, game designer; fantasy writer, amateur artist. || ![]() |
|
|
|
|
|
|
#16 | |
|
Quote:
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
__________________
Stephen Clouse
Kansas City Pokémon |
||
|
|
|
|
|
#17 |
|
Wheeee, Perl. That just looks... lovely.
But yeah. Is char*[] a special case or something that points to strings rather than characters? Like, if I did int*[], that would be an array of pointers to integers...? I need to just read about pointers myself.
__________________
Team B-Side
|| cardmaker, competitive tcg player, competitive vg player. ||![]() || programmer, game designer; fantasy writer, amateur artist. || ![]() |
|
|
|
|
|
|
#18 |
|
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.
__________________
2008 North American Professor Cup Champion Minnesota Plasma Storm Prereleases Battle Road Spring 2013 If you know of any asynchronous multi-camera video recording software, please PM. Thank you! |
|
|
|
|
|
|
#19 |
|
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\20 OWU<\13\40\"^!\10Za,.\2\6\4\5\f\35ikEmj<\10\6\33\0 t\33Zx\2<\22\0\22\6\13~\22\5\r\10\0011\1~\23";$o=o rd(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]}'
__________________
Stephen Clouse
Kansas City Pokémon Last edited by Chairman Kaga; 12/03/2009 at 05:54 AM. |
|
|
|
|
|
|
#20 | |
|
Master Trainer
![]() |
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.
__________________
-Have Fun!
Dylan "ExoByte" Mayo TPCi, R&D Quote:
|
|
|
|
|
|
|
#22 |
|
Thanks for all the help! Chairman Kaga, that is amazing. Too bad I can't use arrays
. Exobyte, sorry, but I don't know what a class is and I'm having trouble finding out what it is. Is there any way you can explain it to me? Chairman Kaga, I've got one question. Out of pure curiosity, how do you run Perl scripts on a mac? I hope I can one day learn Perl. I am working on the thousands right now, and I am having some success.Thanks again!
__________________
|
|
|
|
|
|
|
#23 | |
|
Quote:
Code:
class example
{
public:
int number;
string name;
int age;
};
Code:
void main()
{
example object1;
}
The object1 variable has three internal variables, similar to an array, but those variables are different types. You would refer to them using the dot operator - so if I wanted to set the "number" variable in object1, I'd use "object1.number = 9" or something like that. The same goes for calling them, etc. There's a lot more to it [constructors that will initialize your values, functions that relate to the specific instance of the class, public and private, etc;] but that's a good basic definition... I hope.
__________________
Team B-Side
|| cardmaker, competitive tcg player, competitive vg player. ||![]() || programmer, game designer; fantasy writer, amateur artist. || ![]() |
||
|
|
|
|
|
#24 | ||
|
Master Trainer
![]() |
Quote:
#include/using relevant stuff. Code:
Class BigInt
{
private:
int Trillionizer[13];
public:
BigInt()
{
int xx;
for (xx=0;xx>12;Trillionizer[xx++]=0)
}
void Increment()
{
int xx;
Trillionizer[0]++
for (xx=0;xx>12;xx++)
{
if (Trillionizer[xx] = 10)
{
Trillionizer[xx] = 0;
Trillionizer[xx+1]++;
}
}
}
void Output()
{
bool GroupFlag;
int Group
String stringamajig = "";
String Groups[4] = {"Billion","Million","Thousand",""};
String digits[10] = {"Nine","Eight", (Etc...}
If (Trillionizer[12] = 1)
Stringmajig = "1 Trillion. Shipoopi!";
else
for (group = 3; group<0; group--)
{
GroupFlag = FALSE;
for (THO = 2, THO<0; THO--)
{
if (Trillionizer[3*group+2])
Stringamajig += digits[Trillionizer[3*group+2]] + " hundred "
GroupFlag = TRUE;
if (Trillionizer[3*group+1])
Stringamajig += digits[Trillionizer[3*group+2]] + " ty "
GroupFlag = TRUE;
if (Trillionizer[3*group+0])
Stringamajig += digits[Trillionizer[3*group+2]] + " "
GroupFlag = TRUE;
}
If (Groupflag)
Stringmajig += Groups[group] + " ";
}
cout << stringmajig;
return
}
}
int main (void)
{
int x = 0;
BigInt TrillionLvX;
for (x;x>1000000000000;x++)
{
TrillionLvX.increment;
TrillionLVX.output;
}
return 0;
}
...although if you can't use arrays you probably REALLY can't use classes :P
__________________
-Have Fun!
Dylan "ExoByte" Mayo TPCi, R&D Quote:
|
||
|
|
|
|
|
#25 |
|
That looks more Java than C++, XO. I don't think that C++ has that concept of "public" and "private" (though it's been a while since I actually *coded* in C++)
__________________
2008 North American Professor Cup Champion Minnesota Plasma Storm Prereleases Battle Road Spring 2013 If you know of any asynchronous multi-camera video recording software, please PM. Thank you! |
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|