Thread: Cheque Printing Program Query

  1. #1
    c++ beginner so be nice! karlawarla's Avatar
    Join Date
    Nov 2006
    Location
    West London, UK
    Posts
    33

    Question Cheque Printing Program Query

    Hello, I'm quite new to c++ programming and I'm doing a module in it for uni and there is a question I have to complete and I'm at my wits end trying to figure it out, so here goes,

    I have to write a cheque printing program that will read in a number and will produce the textual equivalent and I have to do it from 0-999.

    So far I have got up to 20 by writing code like this...

    Code:
    void units (int u);
    
         cout <<"Please Enter a Number: ";
         cin >> n;
    
        {
        if (n==0)
        {
            cout <<"Zero\n";
        }
    and all has been working fine but I need to be able to go up to 999 without having up to 999 of that code for each number, so I've been trying to link numbers up for example if 35 is entered it would link the text 'thirty and 'five', but i'm having no luck.

    If anyone could give me some advice on how to go about it, I would greatly appreciate it,

    Thank you,

    Karla xXx

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I get the feeling you didn't search the boards before asking that question.
    My best code is written with the delete key.

  3. #3
    c++ beginner so be nice! karlawarla's Avatar
    Join Date
    Nov 2006
    Location
    West London, UK
    Posts
    33
    sorry, I am very new to this type of programming and programming in general, so on the posts I have been looking at have confused me, and i just thought writing my specific problem out i may get a reply that would help me and I could understand.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Code:
    //works up to 99
    void printTo99(int num)
    {
      switch(num/10)
      {
        case 0:
        if(num)
            printDigit();
        else
           printf("zero");
        break;
    
        case 1:
        printTeen();
        break;
     
        case 2:
        cout<<"twenty";
        printDigit();
        break;
    
        case 3:
        cout<<"thirty";
        printDigit();
        break;
       //continued
    
      }
    }
    Where printDigit() prints values 0-9, except 0 and printTeen prints values 10-19. These could actually be combined into one function.

    Using simmilar meathods you create a function to print values over 100,
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Try this:
    Code:
    #include <iostream>
    
    char numbers[][10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
    char numbers2[][10]={"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eightteen","nineteen"};
    char numbers3[][10]={"","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
    void units(int u);
    
    int main(){
        units(983);
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
    }
    
    void units(int u){
        if(u==0){
            std::cout<<numbers[0];
            return;
        }
        int nums[3]={0,0,0};
        bool plusand=false;
        nums[0]=(u/100)%10;
        nums[1]=(u/10)%10;
        nums[2]=u%10;
        for(int x=0;x<3;x++){
            if(nums[x]>0){
                if(plusand){
                    std::cout<<"and ";
                    plusand=false;
                }
                if(x==0){
                    std::cout<<numbers[nums[0]]<<" hundred ";
                    plusand=true;
                }
                else if(x==1){
                    if(nums[1]==1){
                        std::cout<<numbers2[nums[2]];
                        break;
                    }
                    else{
                        std::cout<<numbers3[nums[1]]<<" ";
                    }
                }
                else{
                    std::cout<<numbers[nums[2]];
                }
            }
        }
        return;
    }
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Maybe if you pick one of the solutions already provided and ask for clarification? To be honest, I can't think of a simpler solution than some of the ones that have already been posted by either myself or others.
    My best code is written with the delete key.

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Try my code.
    And if you get any errors, you should post your code, because otherwise we don't know what you have in your code.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  8. #8
    c++ beginner so be nice! karlawarla's Avatar
    Join Date
    Nov 2006
    Location
    West London, UK
    Posts
    33
    thank you for all your replys, have tried your code and there are some errors that i have not seen before.
    At the moment im only on the basics of c++, and theres quite alot in the code I haven't learnt yet, so i'm still a little confused on what the code is actually doing, is there a different way for a beginner to write a similar program?

    sorry if my questions have obvious answers but i've only been learning c++ really for about 4-5weeks, thanks again. Karla

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    What errors did you get?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  10. #10
    c++ beginner so be nice! karlawarla's Avatar
    Join Date
    Nov 2006
    Location
    West London, UK
    Posts
    33
    '::numeric_limits' undeclared (first use here)

    parse error before >

    implicit declaration of function 'int max()'

    parse error before ')'

  11. #11
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Replace that line with something like this:
    Code:
    	std::cin.ignore(10000,'\n');
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  12. #12
    c++ beginner so be nice! karlawarla's Avatar
    Join Date
    Nov 2006
    Location
    West London, UK
    Posts
    33
    ah ha it works! lol thank you :-)

  13. #13
    c++ beginner so be nice! karlawarla's Avatar
    Join Date
    Nov 2006
    Location
    West London, UK
    Posts
    33
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
        void main (int n)
    
    
    
    {
    
        void units (int u);
    
         cout <<"Please Enter a Number: ";
         cin >> n;
    
        {
        if (n==0)
        {
            cout <<"Zero\n";
        }
        if (n==1)
        {
            cout <<"One\n";
        }
        if (n==2)
        {
            cout <<"Two\n";
        }
        if (n==3)
        {
            cout <<"Three\n";
        }
        if (n==4)
        {
            cout <<"Four\n";
        }
        if (n==5)
        {
            cout <<"Five\n";
        }
        if (n==6)
        {
            cout <<"Six\n";
        }
        if (n==7)
        {
            cout <<"Seven\n";
        }
        if (n==8)
        {
            cout <<"Eight\n";
        }
        if (n==9)
        {
            cout <<"Nine\n";
        }
    
    
        void tens (int t);
    
    
        if (n==10)
        {
            cout <<"Ten\n";
        }
        if (n==11)
        {
            cout <<"Eleven\n";
        }
        if (n==12)
        {
            cout <<"Twelve\n";
        }
        if (n==13)
        {
            cout <<"Thirteen\n";
        }
        if (n==14)
        {
            cout <<"Fourteen\n";
        }
        if (n==15)
        {
            cout <<"Fifteen\n";
        }
        if (n==16)
        {
            cout <<"Sixteen\n";
        }
        if (n==17)
        {
            cout <<"Seventeen\n";
        }
        if (n==18)
        {
            cout <<"Eighteen\n";
        }
        if (n==19)
        {
            cout <<"Nineteen\n";
        }
    
    
    
    
    }
        }
    This was the code I had before, is there a way to make a program like you posted in that way?
    or does there need to variables like 'ten' 'twenty' etc? as alot of the stuff in your program I haven't come across yet, the furthest I have got up to in c++ is the switch statement & loops.

  14. #14
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Yeah but as you can see I am using those variables I declared as global variables several times. So it would be a terrible waste of time and space.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  15. #15
    c++ beginner so be nice! karlawarla's Avatar
    Join Date
    Nov 2006
    Location
    West London, UK
    Posts
    33
    ah okay, thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing a program
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-09-2006, 07:25 AM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Crossword Puzzle Program
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2006, 11:08 PM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM