Reputation: 33
I am new to programming and especially C++ so I decided to re-write a java program I wrote to convert a number (for example 13) to words (thirteen), and it worked fine but I tried re-writing it in C++ and after compiling, starting the program, and entering the number it does nothing. I am sorry if thing like my variable's names are unusual.
This is the java program:
public class Say
{
//AAAARRRRRR!!!! Here be Arrrrrrays!
static String first[] =
{
"" , "One " , "Two " , "Three ", "Four ", "Five " , "Six " ,
"Seven " , "Eight " , "Nine " , "Ten " , "Eleven " , "Twelve " ,
"Thirteen " , "Fourteen " , "Fifteen " , "Sixteen " , "Seventeen " ,
"Eighteen " , "Nineteen "
};
static String second[] =
{
"" , "" , "Twenty " , "Thirty " , "Fourty " , "Fifty " ,
"Sixty ", "Seventy " , "Eighty " , "Ninety "
};
static String sections[] =
{
"" , "Hundred " , "Thousand " , "Million " , "Billion "
};
//Number stuff ho!
public static void main( String[] args )
{
String origin = ( args[0] );
int original = Integer.parseInt( origin );
int orlength = origin.length();
int remaindr = ( orlength % 3 );
int legroups;
if ( remaindr != 0 )
{
legroups = ( orlength / 3 + 1 );
}
else
{
legroups = ( orlength / 3 );
}
//Groups AAAARRR here matey!
int groupone = ( original % 1000 );
int grouptwo = ( ( ( original % 1000000 ) - groupone ) / 1000 );
int groupthr = ( ( ( original % 1000000000 ) - grouptwo ) / 1000000 );
//[Pirate themed comment on this being a loop]
boolean finished = false;
int takestep = 0;
while ( finished == false )
{
takestep ++;
int numinact;
if ( takestep == 1 )
{
numinact = groupthr;
}
else if ( takestep == 2 )
{
if ( groupthr != 0 )
{
System.out.print( sections[ 3 ] );
}
numinact = grouptwo;
}
else
{
if ( grouptwo != 0 )
{
System.out.print( sections[ 2 ] );
}
numinact = groupone;
finished = true;
}
if ( numinact > 99 )
{
int hundreds = ( ( numinact - ( numinact % 100 ) ) / 100 );
System.out.print( first [ hundreds ] + sections [ 1 ] );
numinact = ( numinact % 100 );
}
if ( numinact <= 19 )
{
System.out.print( first [ numinact ] );
}
else if ( numinact <= 29 )
{
int digitact = ( numinact - 20 );
System.out.print( second[ 2 ] + first[ digitact ] );
}
else if ( numinact <= 39 )
{
int digitact = ( numinact - 30 );
System.out.print( second[ 3 ] + first[ digitact ] );
}
else if ( numinact <= 49 )
{
int digitact = ( numinact - 40 );
System.out.print( second[ 4 ] + first[ digitact ] );
}
else if ( numinact <= 59 )
{
int digitact = ( numinact - 50 );
System.out.print( second[ 5 ] + first[ digitact ] );
}
else if ( numinact <= 69 )
{
int digitact = ( numinact - 60 );
System.out.print( second[ 6 ] + first[ digitact ] );
}
else if ( numinact <= 79 )
{
int digitact = ( numinact - 70 );
System.out.print( second[ 7 ] + first[ digitact ] );
}
else if ( numinact <= 89 )
{
int digitact = ( numinact - 80 );
System.out.print( second[ 8 ] + first[ digitact ] );
}
else if ( numinact <= 99 )
{
int digitact = ( numinact - 90 );
System.out.print( second[ 9 ] + first[ digitact ] );
}
}
//Yarrr! Debug be what this is!
//System.out.println( " original is " + original + ", orlength is " +
// orlength + ", remaindr is " + remaindr + ", legroups is " +
// legroups + ", groupone is " + groupone + ", grouptwo is " +
// grouptwo + ", groupthr is " + groupthr );
}
}
And this is the C++ re-write that does not work:
//C++ port of the Say.java program.
//I hope to extend to longer numbers in the future.
#include <iostream>
#include <string>
using namespace std;
static string digit [20] =
{
"" , "One " , "Two " , "Three ", "Four ", "Five " , "Six " , "Seven " ,
"Eight " , "Nine " , "Ten " , "Eleven " , "Twelve " , "Thirteen " ,
"Fourteen " , "Fifteen " , "Sixteen " , "Seventeen " , "Eighteen " ,
"Nineteen "
};
int main()
{
int original; //declare int
cout << "Enter your number: "; //Requests user input
cin >> original; //Recieves user input assigns value to previous variable
//Groups of 3 digits
int groupone = ( original % 1000 );
int grouptwo = ( ( original / 1000 ) % 1000);
int groupthr = ( original / 1000000 );
//Intense loop, almost direct from java version.
bool finished = false;
int takestep = 0;
while ( finished != true );
{
takestep ++;
int numinact;
if ( takestep == 1 )
{
numinact = groupthr;
}
else if ( takestep == 2 )
{
if ( groupthr != 0 )
{
cout << "Million ";
}
numinact = grouptwo;
}
else
{
if ( grouptwo != 0 )
{
cout << "Thousand ";
}
numinact = groupone;
finished = true;
}
if ( numinact > 99 )
{
int hundreds = ( ( numinact - (numinact % 100 ) ) / 100 );
cout << digit[ hundreds ] << "Hundred ";
numinact = ( numinact % 100 );
}
if ( numinact <= 19 )
{
cout << digit[ numinact ];
}
else if ( numinact <= 29 )
{
int digitact = ( numinact - 20 );
cout << "twenty " << digit[ digitact ];
}
else if ( numinact <= 39 )
{
int digitact = ( numinact - 30 );
cout << "thirty " << digit[ digitact ];
}
else if ( numinact <= 49 )
{
int digitact = ( numinact - 40 );
cout << "fourty " << digit[ digitact ];
}
else if ( numinact <= 59 )
{
int digitact = ( numinact - 50 );
cout << "fifty " << digit[ digitact ];
}
else if ( numinact <= 69 )
{
int digitact = ( numinact - 60 );
cout << "sixty " << digit[ digitact ];
}
else if ( numinact <= 79 )
{
int digitact = ( numinact - 70 );
cout << "seventy " << digit[ digitact ];
}
else if ( numinact <= 89 )
{
int digitact = ( numinact - 80 );
cout << "eighty " << digit[ digitact ];
}
else if ( numinact <= 99 )
{
int digitact = ( numinact - 90 );
cout << "ninety " << digit[ digitact ];
}
}
return 0;
}
What must I change to get it to run like the java program?
Upvotes: 3
Views: 1020
Reputation: 882496
This does not do what you think it does:
bool finished = false;
while ( finished != true );
{
// blah blah blah
finished = true;
}
The semicolon at the end of that while
line makes it an infinite loop followed by a block which you never reach (because of that infinite loop).
Remove the semicolon and you will get:
pax$ ./testprog
Enter your number: 1234
One Thousand Two Hundred thirty Four
Upvotes: 10