Is my c++ code correct?

include<iostream.h>

#include<conio.h>

#include<math.h>

int main(){

int frost, flame;

clrscr();

cout<<"Enter a number (0-20,000): ";

cin>>frost;

if(frost==0)

cout<<"zero ";

else if(frost!==0){

flame = (frost/1000);}

if(flame==20){

cout<<"twenty thousand ";}

else if(flame==19){

cout<<"nineteen thousand ";}

else if(flame==18){

cout<<"eighteen thousand ";}

else if(flame==17){

cout<<"seventeen thousand ";}

else if(flame==16){

cout<<"sixteen thousand ";}

else if(flame==15){

cout<<"fifteen thousand ";}

else if(flame==14){

cout<<"fourteen thousand ";}

else if(flame==13){

cout<<"thirteen thousand ";}

else if(flame==12){

cout<<"twelve thousand ";}

else if(flame==11){

cout<<"eleven thousand ";}

else if(flame==10){

cout<<"ten thousand ";}

else if(flame==9){

cout<<"nine thousand ";}

else if(flame==8){

cout<<"eight thousand ";}

else if(flame==7){

cout<<"seven thousand ";}

else if(flame==6){

cout<<"six thousand ";}

else if(flame==5){

cout<<"five thousand ";}

else if(flame==4){

cout<<"four thousand ";}

else if(flame==3){

cout<<"three thousand ";}

else if(flame==2){

cout<<"two thousand ";}

else if(flame==1){

cout<<"one thousand ";}

else cout<<""; }

frost=(frost-(flame*1000));

flame=frost/100

if(flame==9){

cout<<" nine hundred ";}

else if(flame==8){

cout<<" eight hundred ";}

else if(flame==7){

cout<<" seven hundred ";}

else if(flame==6){

cout<<" six hundred ";}

else if(flame==5){

cout<<" five hundred ";}

else if(flame==4){

cout<<" four hundred ";}

else if(flame==3){

cout<<" three hundred ";}

else if(flame==2){

cout<<" two hundred ";}

else if(flame==1){

cout<<" one hundred ";}

frost=floor(frost);

flame=frost-flame*100;

flame=flame/10;

if(flame==9){

cout<<" ninety ";}

else if(flame==8){

cout<<" eighty ";}

else if(flame==7){

cout<<"seventy ";}

else if(flame==6){

cout<<"sixty ";}

else if(flame==5){

cout<<"fifty ";}

else if(flame==4){

cout<<"fourty ";}

else if(flame==3){

cout<<"thrirty ";}

else if(flame==2){

cout<<"twenty ";}

else if(flame==1){

frost=floor(flame);

flame=frost-(flame*10);

if(flame==9){

cout<<" nineteen";}

else if(flame==8){

cout<<" eighteen";}

else if(flame==7){

cout<<"seventeen";}

else if(flame==6){

cout<<"sixteen";}

else if(flame==5){

cout<<"fifteen";}

else if(flame==4){

cout<<"fourteen";}

else if(flame==3){

cout<<"thirteen";}

else if(flame==2){

cout<<"twelve";}

else if(flame==1){

cout<<"eleven";}}

if(flame!=0)

frost=floor(flame);

flame=frost-(flame*10);

if(flame==9){

cout<<" nine";}

else if(flame==8){

cout<<" eight";}

else if(flame==7){

cout<<" seven";}

else if(flame==6){

cout<<" six";}

else if(flame==5){

cout<<" five";}

else if(flame==4){

cout<<" four";}

else if(flame==3){

cout<<" three";}

else if(flame==2){

cout<<" two";}

else if(flame==1){

cout<<" one";}}

getch();

return 0;

}

i can't try it at this pc so pls help me or correct me with this

Update:

The problem is i can only use if,else if, and floor

Comments

  • What you have might work, but it may be the worst way to do it ... and a switch statement is no better. A lookup table is what you need, like this:

    #include <iostream>

    #include <string>

    #include <sstream>

    using namespace std;

    int getInt(void);

    const char *twoDigitLookup[3][10] =

        { { "zero","one","two","three","four",

                "five","six","seven","eight","nine" },

            { "ten","eleven","twelve","thirteen",

                "fourteen","fifteen","sixteen",

                "seventeen","eighteen","nineteen" },

            { " ", " ", "twenty ","thirty ",

                "forty ","fifty ","sixty ",

                "seventy ","eighty ","ninety "} };

    void printTwoDigit(unsigned);

    void printThreeDigit(unsigned);

    void printFourDigit(unsigned);

    void printFiveDigit(unsigned);

    unsigned m;

    int main(int argc, char *argv[]) {

        long int n = 0;

        cout << "Enter numbers in range [-99999 .. 99999]:" << endl;

        while (true) {

            cout << endl << "> ";

            n = getInt();

            if ((n < 0) && ((n *= -1) < 100000)) cout << "negative ";

            if (n < 100000) printFiveDigit(n);

        }

        return 0;

    }

    void printFiveDigit(unsigned n) {

        if (n < 100000) {

            if (n > 9999) {

                printTwoDigit(n / 1000);

                cout << " thousand ";

                if ((m = n % 1000) > 0) printThreeDigit(m);

            } else if (n > 999) {

                printFourDigit(n);

            } else if (n > 99) {

                printThreeDigit(n);

            } else {

                printTwoDigit(n);

            }

        }

    }

    void printFourDigit(unsigned n) {

        if (n < 10000) {

            if (n > 999) {

                cout << twoDigitLookup[0][n/1000] << " thousand ";

                if ((m = n % 1000) > 0) printThreeDigit(m);

            } else if (n > 99) {

                printThreeDigit(n);

            } else {

                printTwoDigit(n);

            }

        }

    }

    void printThreeDigit(unsigned n) {

        if (n < 1000) {

            if (n > 99) {

                cout << twoDigitLookup[0][n/100] << " hundred ";

                if ((m = n % 100) > 0) printTwoDigit(m);

            } else {

                printTwoDigit(n);

            }

        }

    }

    void printTwoDigit(unsigned n) {

        if (n < 100) {

            if (n > 19) {

                cout << twoDigitLookup[2][n/10];

                if ((n % 10) > 0) {

                    cout << twoDigitLookup[0][n%10];

                }

            } else if (n > 9) {

                cout << twoDigitLookup[1][n-10];

            } else {

                cout << twoDigitLookup[0][n];

            }

        }

    }

    int getInt() {

        stringstream ss;

        string line;

        bool inputOk;

        int n;

        do {

            getline(cin,line);

            ss.clear(); ss.str(line);

            if ((!(ss >> n)) || (ss.good())) {

                cout << endl << "invalid input, try again" << endl << "> ";

            } else {

                inputOk = true;

            }

        } while (inputOk == false);

        return n;

    }

    #if 0

    Sample run:

    Enter numbers in range [-99999 .. 99999]:

    > 3

    three

    > 12

    twelve

    > 123

    one hundred twenty three

    > 1234

    one thousand two hundred thirty four

    > 12345

    twelve thousand three hundred forty five

    > 16000

    sixteen thousand

    >

    #endif

  • It's correct you missed:

    #include<iostream.h> // the "#"

    flame=frost/100; // and a ";"

  • Please use the switch statement! 0.o

    But at first glimpse, it should work.

Sign In or Register to comment.