Program runs, but I would like it to exit with a negative number input

This is a discussion on Program runs, but I would like it to exit with a negative number input within the C++ Programming forums, part of the General Programming Boards category; My first time posting. This program is/was a homework problem. It works as required, but I would like to eliminate ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    Question Program runs, but I would like it to exit with a negative number input

    My first time posting. This program is/was a homework problem. It works as required, but I would like to eliminate negative numbers and have it exit with the input of a negative rather than zero. All mods i have made just screwed it up and I know it's simple. What areas need to be changed to get this ?
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <cmath>
    using namespace std;
    
    class Numbers
    {
    private:
        int number;   
    
    public:
       
         Numbers(int a)
           {number=a;}
         void print();
    };
    
    
    void Numbers::print()
    {
    	int n;
    	string lessthan20[20]={"zero","one","two","three","four","five","six","seven",
                       "eight","nine","ten","eleven","twelve","thirteen",
                         "fourteen","fifteen","sixteen","seventeen",
                         "eighteen","nineteen"};
    
    	string tens[10]={"zero","ten","twenty","thirty","forty","fifty","sixty",
                     "seventy", "eighty", "ninety"};
    	if(number<0)
    	 cout<<"negative ";
    	 number=abs(number);
    	 n=number/1000;
        if(n>0)
         cout<<" "<<lessthan20[n]<<" thousand ";
    	 number%=1000;
    
    	 n=number/100;
    	if(n>0)
         cout<< lessthan20[n]<<" hundred ";
    	 number%=100;
    	if(number>=20)
        {
            n=number/10;           
            if(n>0)        
               cout<<tens[n]<<" ";
        }
             else if(number>=10)
        { 
             cout<< lessthan20[number]<<" ";
            return;
        }
    	 number%=10;
    	if(number>0)
         cout<<lessthan20[number];
         cout<<" ";
    
    }
    
    int main()
    {
        int n;
        cout<<" This program will take an input of up to four(4) numerals\n";
        cout<<" and change them to a written number.\n";
        cout<<" Enter up to a four-digit number(0 to exit): ";
        cin>>n;    
        while(n!=0)
       {
            Numbers number(n);
            number.print();
    
           cout<<"\nEnter a number(0 to exit): ";
           cin>>n;  
       }
    system("pause");
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,745
    Quote Originally Posted by msanak
    I would like to eliminate negative numbers and have it exit with the input of a negative rather than zero.
    Just change the loop condition from (n!=0) to (n >= 0).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,620
    Consistent and good indentation would help. I suspect you have lots of bugs where missing curly braces should be.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    Thanks! I knew i was looking at it too long.

    I knew it was a simple fix. When looking over my code after the response, I just gave a whack to the forhead and thought'stupid! It was staring at you- take out the ! when you change the loop, don't leave it there" Thanks for pointing out what i was missing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 05:16 AM
  3. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 02:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21