我的C ++程序什么都不做

时间:2011-11-25 06:32:03

标签: c++

我是编程新手,尤其是C ++,所以我决定重新编写一个我编写的java程序,将一个数字(例如13)转换为单词(十三),它工作正常,但我尝试重新编写它C ++和编译后,启动程序,并输入数字,它什么都不做。如果像我的变量名这样的东西不寻常,我很抱歉。

这是java程序:

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 );
}

}

这是C ++重写不起作用:

//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;
}

我必须改变什么来让它像java程序一样运行?

1 个答案:

答案 0 :(得分:10)

这不符合你的想法:

bool finished = false;
while ( finished != true );
{
    // blah blah blah
    finished = true;
}

while行末尾的分号使其成为一个无限循环,后跟一个你永远不会到达的块(因为那个无限循环)。

删除分号,您将获得:

pax$ ./testprog
Enter your number: 1234
One Thousand Two Hundred thirty Four