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

  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
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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, 06:16 AM
  3. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 02:06 PM